# Set Open SSH and Use VScode connect to RHEL 2022-10-27
###### tags: `Open SSH` `Remote SSH` `Web server` `Back end` `後端心得`
---
## 先參考微軟教學並使用 PowerShell 安裝 Open SSH
說明及教學:
https://learn.microsoft.com/zh-tw/windows-server/administration/openssh/openssh_install_firstuse
### 1. 以系統管理員的身分執行 PowerShell 和以下指令:
在 VScode 輸入指令來開啟系統管理員身分:
> start-process powershell -verb runas
並且執行:
> Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
尚未安裝的話會回傳出:
```
Name : OpenSSH.Client~~~~0.0.1.0
State : Installed
Name : OpenSSH.Server~~~~0.0.1.0
State : NotPresent
```
### 2. 然後安裝伺服器及用戶端元件
```
# Install the OpenSSH Client
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
# Install the OpenSSH Server
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
```
兩個應該會回傳以下輸出:
```
Path :
Online : True
RestartNeeded : False
```
---
## 先參考微軟教學並啟動及設定 OpenSSH 伺服器
一樣以系統管理員身分開啟 PowerShell
並且執行:
```
# Start the sshd service
Start-Service sshd
# OPTIONAL but recommended:
Set-Service -Name sshd -StartupType 'Automatic'
# Confirm the Firewall rule is configured. It should be created automatically by setup. Run the following to verify
if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) {
Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..."
New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
} else {
Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists."
}
```
成功的話接著進行連線 OpenSSH Server
## 進行連線 OpenSSH Server
一樣以系統管理員身分開啟 PowerShell
並且執行:
```
ssh root@"內網的位址"
```
成功後應該可以進入機器~

其他教學:
https://hackmd.io/@Chieh997/VScodeSSH
https://hackmd.io/@brick9450/vscode-remote
https://ithelp.ithome.com.tw/articles/10245726
---
## 接著在 VScode 內安裝套件 Remote-SSH
安裝這個 Remote - SSH 套件 ↓

安裝 Extension 後會出現兩個 extension:
**Remote - SSH**
**Remote - SSH: Editing Configuration**
接著按照 Extension 套件的說明, 按下 F1 開始設定
假如你 `C:\Users\user\.ssh` (通常 .ssh 會產生在這) 內 `.ssh` 內的 `config` 檔案 有設定錯誤 或是 user 被預設為 `user@'內網位址'` 可以試著在 config 內設定, 或是刪除 config, 例如:
```
Host 192.168.66.xx
HostName 192.168.66.xx
```
↑ 可以看到缺了 User
接著, 這是 config 內設定好的樣子
```
Host 192.168.66.xx
HostName 192.168.66.xx
User root
```
----
## 開始在 VScode 內修改
可以選擇在當前視窗或是開一個新的視窗來進行遠端連線

接著會要你選你遠端的平台, 如果是 Linux 就選 Linux
同時確認指紋 (finger print), 並按下 continue
輸入密碼後進入~
成功後可能會跳一些套件版本不一致的提示, 因為本機與 Linux 虛擬機使用的有落差, 這時要排除

然後跳提示你選擇的 Linux 作為 remote 平台, 這會被保存在 Remote - SSH Extension 的擴充套件中, 之後如果有要變更可以來套件的設定變更

---
## 這時候你可以使用 terminal 去使用指令, 但~
這時 Remote Explorer 的平台內會沒有東西, 但你只能使用 Terminal 去作指令輸入~
如果要用到類似 Window OS 或 MobaXterm 這種有視覺化呈現 "操作資料夾" 的 UI 的話
對著你的 SSH Targets 使用右鍵~
> 
↑ 會長得像這樣
呈現熟悉的視覺化資料夾介面~~~