# VScode 使用 SSH 進行遠端程式開發 ###### tags: `Tutorial`, `Ubuntu`, `VScode`, `SSH` --- Server: Ubuntu 22.04 (SSH server) User: Windows 11 (SSH client) --- :::danger *`{}`內文字需視個人設定更改* ::: ## Server Setting ### Add a New User(Optional) 開啟root,使用下列兩者其中一種 ```bash=1 sudo su - sudo -i ``` 新增使用者,設定密碼及填寫基本資料(optional) ```bash=+ adduser {username} ``` 確認是否成功新增使用者 ```bash=+ cut -d: -f1 /etc/passwd ``` ### Install OpenSSH 確認目前有支援ssh的軟體 ```bash=+ dpkg -l | grep ssh ``` 更新apt ```bash=+ sudo apt update && sudo apt upgrde ``` 視情況安裝ssh ```bash=+ sudo apt-get install ssh sudo apt-get install openssh-server ``` 啟動ssh ```bash=+ sudo systemctl enable ssh ``` 修改ssh的設定 ```bash=+ sudo vim /etc/ssh/sshd_config ``` :::spoiler + vim 快捷鍵 `Esc` - Normal Mode `i` - Insert Mode `/[searchword]` - search `:q!` - exit without saving `:x!` or `:wq!` - save and exit + ssh一般設定 ```vim= Port {22} PassworAuthentication Yes PermitRootLogin no #禁止使用root登入 ``` + ssh允許特定使用者 ```vim= AllowUsers {username} ``` ::: \ 重新啟動ssh ```bash=11 sudo systemctl restart sshd ``` 若restart失敗,可用下兩行指令重新啟動 ```bash=+ sudo systemctl stop sshd sudo systemctl start sshd ``` 確認ssh server有無正常運行,正常運行會顯示綠色的Activate。有修改Port須記得確認是否有從`22`改為新的指定數字。 ```bash=+ sudo systemctl status ssh ``` [建立`.ssh/authorized_keys`](https://askubuntu.com/questions/466549/bash-home-user-ssh-authorized-keys-no-such-file-or-directory) ```bash=+ mkdir .ssh touch .ssh/authorized_keys ``` ### Setting ufw 防火牆設定允許使用ssh ```bash=+ sudo ufw allow OpenSSH sudo ufw enable sudo ufw status ``` :::spoiler + 允許使用ssh的替代方法: ssh或指定port ```bash=0 sudo ufw allow ssh ``` ```bash=0 sudo ufw allow {port} ``` + 防火牆重新設定 ```bash=0 sudo ufw --force disable sudo ufw --force reset sudo ufw enable sudo ufw status ``` + 查看防火牆規則號碼 ```bash=0 sudo ufw status numbered ``` + 刪除防火牆特定規則:使用號碼 ```bash=0 sudo ufw delete {rule-number} ``` 更多防火牆設定指令參見[How To Set Up a Firewall with UFW on Ubuntu 18.04](https://www.digitalocean.com/community/tutorials/how-to-set-up-a-firewall-with-ufw-on-ubuntu-18-04) ::: ## Connecting with SSH *無論內網外網,都必須使用固定IP才能連線。* + 內網: 查詢Server的IP(192.168.x.x)以連線 ```bash=0 hostname -I ``` + 外網: 通過[設定port forwarding](https://adrianwangdev.medium.com/%E9%9B%BB%E8%85%A6%E5%B0%8F%E7%9F%A5%E8%AD%98-%E9%80%8F%E9%81%8E-ssh-%E9%81%A0%E7%AB%AF%E9%80%A3%E7%B7%9A%E9%9A%A8%E6%99%82%E7%AE%A1%E7%90%86%E5%AE%B6%E4%B8%AD%E9%9B%BB%E8%85%A6%E6%96%87%E4%BB%B6-%E8%B6%85%E8%A9%B3%E7%9B%A1%E6%AD%A5%E9%A9%9F%E7%A4%BA%E7%AF%84-f6d7c7192c52)的方式,以WAN IP連入內網電腦。 User使用PowerShell連線進入Server,`{hostIP}`為前面所查到的Server IP或WAN IP。 ```bash=0 ssh {username@hostIP} ``` 若有指定port number,使用下列的指令 ```bash=0 ssh {username@hostIP} -p {port_number} ``` ## User Setting ### Install OpenSSH using PowerShell 以<font color="#f00">**系統管理員**</font>執行PowerShell,確認是否有OpenSSH可供使用 ```PowerShell=1 Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*' ``` 視需求安裝OpenSSH server或OpenSSH Client ```PowerShell=+ # 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 ``` 確認是否可連線進入Server ```PowerShell=+ ssh {username@hostIP -p port_number} ``` ### Connecting without password using ssh key User端的WSL或PowerShell產生ssh key。WSL和PowerShell指令相同,但公私鑰不通用。 ```PowerShell=+ ssh-keygen ``` 視情況決定是否要改變key的存檔位置或設置passphrase。 完成後會生成一組私鑰`/home/{username}/.ssh/id_rsa`和 公鑰`/home/{username}/.ssh/id_rsa.pub` 將公鑰放到Server上 + Ubuntu/WSL ```bash=+ ssh-copy-id -i {your_key_path} -p {port_number} {username@hostIP} ``` + Windows(PowerShell) ```PowerShell=9 type $env:USERPROFILE\.ssh\id_rsa.pub | ssh {username@hostIP -p port_number} "cat >> .ssh/authorized_keys" ``` 測試使否可免密碼用key登入 ```PowerShell=+ ssh {username@hostIP -p port_number} ``` ### VScode Remote Development 先安裝VScode,再安裝擴充功能Remote Development。 ![RemDev](https://i.imgur.com/g7ENJp6.png) 點擊左下角綠色的open a remote window按鈕 ![><](https://i.imgur.com/zmZYe0V.png) 選擇Remote-SSH: Connect to Host… ![ConnectHost](https://i.imgur.com/fhmIL7L.png) 輸入`ssh {username@hostIP}`或`ssh {username@hostIP} -p {port_number}` ![ip](https://code.visualstudio.com/assets/docs/remote/ssh/ssh-user@box.png) 選擇Server對應的作業系統(Linux/Windows/macOS) ![](https://code.visualstudio.com/assets/docs/remote/ssh/ssh-select-platform.png) 待成功連線與完成設定後即可順利使用。 ### 透過SSH遠端啟動Anydesk或其他服務 與啟動SSH連線或防火牆(ufw)類似,若要遠端重啟可於SSH連線後透過command line啟動相關服務。 以Anydesk為例,若要重啟、停止、開始: ```bash=0 sudo systemctl restart anydesk sudo systemctl stop anydesk sudo systemctl start anydesk ``` 若要確認服務當前狀態: ```bash=+ sudo systemctl status anydesk ``` ### Server - User 檔案複製與交換 使用VScode連線後,可直接「選取檔案>右鍵download」即可將server上的檔案傳輸至正在使用的電腦。而要將檔案傳送到server端則需要搭配scp。 #### 從 Windows 複製檔案到 Linux Server 上: scp + 進入cmd,輸入下列指令: ```powershell=0 scp -P {port} {file/path} {username@hostIP:./target/path} ``` 不指定 target path 至少也需要加上`./`使其複製到 home directory 下。 + 若要複製整個資料夾,則在資料夾路徑前加上`-r`(遞迴): ```powershell=+ scp -P {port} -r {folder/path} {username@hostIP:target/path} ``` #### 大型檔案傳輸 rsync Windows 需下載 [cwRsync client](https://itefix.net/cwrsync/client) 1. 解壓縮後,在~/cwrsync/bin的目錄下右鍵開啟cmd 2. 以 rsync 指令傳輸檔案。 - `-e "./ssh -p {port}"`: 使用cwRsync版本的ssh連線,而非使用Windows之OpenSSH - `-avzr`: 壓縮且有效率的傳輸檔案。`r`代表遞迴,所以可以傳輸資料夾。 - `/cygdrive/c/`: 避免將Windows`C:\`誤讀為遠端系統。 ```cmd rsync -avzr -e "./ssh -p {port}" /cygdrive/c/{folder/path} {username@hostIP:target/path} ``` ## Reference + Ubuntu + [How to Add a User with SSH Access on Ubuntu[Youtube]](https://www.youtube.com/watch?v=COp6JtP45o8) + [Ubuntu Linux install OpenSSH server](https://www.cyberciti.biz/faq/ubuntu-linux-install-openssh-server/) + [Linux SCP Command Explained](https://phoenixnap.com/kb/linux-scp-command) + Windows + [[教學] 產生SSH Key並且透過KEY進行免密碼登入](https://xenby.com/b/220-%E6%95%99%E5%AD%B8-%E7%94%A2%E7%94%9Fssh-key%E4%B8%A6%E4%B8%94%E9%80%8F%E9%81%8Ekey%E9%80%B2%E8%A1%8C%E5%85%8D%E5%AF%86%E7%A2%BC%E7%99%BB%E5%85%A5) + [Install OpenSSH in Windows](https://docs.microsoft.com/zh-tw/windows-server/administration/openssh/openssh_install_firstuse) + [Windows 10 OpenSSH Equivalent of ssh-copy-id](https://www.chrisjhart.com/Windows-10-ssh-copy-id/) + VScode + [[教學] 使用 Visual Studio Code 透過 SSH 進行遠端程式開發](https://xenby.com/b/221-%E6%95%99%E5%AD%B8-%E4%BD%BF%E7%94%A8-visual-studio-code-%E9%80%8F%E9%81%8E-ssh-%E9%80%B2%E8%A1%8C%E9%81%A0%E7%AB%AF%E7%A8%8B%E5%BC%8F%E9%96%8B%E7%99%BC) + [Remote Development using SSH in VScode](https://code.visualstudio.com/docs/remote/ssh)