VSCode 透過 SSH 進行遠端開發 = ###### tags: `Tutorial` `Ubuntu` `VScode` `SSH` ``` Server: Ubuntu 22.04 / Windows (SSH server) User: Windows 11/MAC OS (SSH client) ``` Server Setting - #### <span style="color:IndianRed;">{ }內文字需視個人設定更改</span> ### Add a New User(Optional) 開啟root,使用下列兩者其中一種 ```powershell= sudo su - sudo -i ``` #### <span style="color:LightCoral;">Windows version</span> 新增使用者,設定密碼及填寫基本資料(optional) ```powershell= adduser {username} ``` 確認是否成功新增使用者 ```powershell= cut -d: -f1 /etc/passwd ``` #### <span style="color:LightCoral;">macOS version</span> 新增使用者 ```powershell= sudo dscl . -create /Users/{username} ``` 設定密碼 ```powershell= sudo dscl . -passwd /Users/{username} ``` 管理員權限 ```powershell= sudo dscl . -append /Groups/admin GroupMembership {username} ``` 確認是否成功新增使用者,查看使用者 ```powershell= dscl . -read /Users/{username} ``` ### Install OpenSSH #### <span style="color:LightCoral;">macOS version(mac好像內建就有,所以我沒有特別安裝)</span> 檢查是否安裝OpenSSH ```powershell= ssh -V ``` 我的是mac12.5,顯示版本為 <span style="color:DarkSalmon;">`OpenSSH_8.6p1, LibreSSL 3.3.6`</span> 若沒有可能會顯示 ``` ssh: Could not resolve hostname -V: nodename nor servname provided, or not known ``` 但這部分我還沒有研究 #### <span style="color:LightCoral;">Windows version</span> 確認目前有支援ssh的軟體 ```powershell= dpkg -l | grep ssh ``` 更新apt ```powershell= sudo apt update && sudo apt upgrde ``` 視情況安裝ssh ```powershell= sudo apt-get install ssh sudo apt-get install openssh-server ``` 啟動ssh ```powershell= sudo systemctl enable ssh ``` 修改ssh的設定 ```powershell= sudo vim /etc/ssh/sshd_config ``` 重新啟動ssh ```powershell= sudo systemctl restart sshd ``` 若restart失敗,可用下兩行指令重新啟動 ```powershell= sudo systemctl stop sshd sudo systemctl start sshd ``` 確認ssh server有無正常運行,正常運行會顯示綠色的Activate。有修改Port須記得確認是否有從`22`改為新的指定數字。 ```powershell= sudo systemctl status ssh ``` [建立.ssh/authorized_keys](https://askubuntu.com/questions/466549/bash-home-user-ssh-authorized-keys-no-such-file-or-directory) ```powershell= mkdir .ssh touch .ssh/authorized_keys ``` ### Setting ufw 防火牆設定允許使用ssh ```powershell= sudo ufw allow OpenSSH sudo ufw enable sudo ufw status ``` Connecting with SSH - 無論內網外網,都必須使用固定IP才能連線 * 內網: 查詢Server的IP(192.168.x.x)以連線 #### <span style="color:LightCoral;">macOS version</span> ```powershell= ipconfig getifaddr en0 ``` #### <span style="color:LightCoral;">Windows version</span> ```powershell= hostname -I ``` * 外網: 通過設定port forwarding的方式,以WAN IP連入內網電腦 User使用PowerShell連線進入Server,{hostIP}為前面所查到的Server IP或WAN IP ```powershell= ssh {username@hostIP} ``` 若有指定port number,使用下列的指令 ```powershell= ssh {username@hostIP} -p {port_number} ``` User Setting - ### Install OpenSSH using PowerShell 確認是否有OpenSSH可供使用 #### <span style="color:LightCoral;">macOS version</span> ```powershell= ssh -V ``` #### <span style="color:LightCoral;">Windows version</span> ```powershell= 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。 ```powershell= ssh-keygen ``` 視情況決定是否要改變key的存檔位置或設置passphrase 完成後會生成\ 私鑰:`/home/{username}/.ssh/id_rsa`\ 公鑰:`/home/{username}/.ssh/id_rsa.pub` 將公鑰放到Server上 * macOS/Ubuntu/WSL ```powershell= ssh-copy-id -i {your_key_path} -p {port_number} {username@hostIP} ``` * Windows(PowerShell) ```powershell= type $env:USERPROFILE\.ssh\id_rsa.pub | ssh {username@hostIP -p port_number} ``` 測試是否可免密碼用key登入 ```powershell= ssh {username@hostIP -p port_number} ``` ### VScode Remote Development 安裝VSCode以及擴充套件 * Remote Development  * Remote - SSH  設定完成後點擊左邊的Remote-SSH圖案,connection下就會有剛剛設定好的遠端主機,點擊右邊的已新視窗開啟  資料上傳至server ```powershell= scp /path/to/local/file username@remote_host:/path/to/remote/destination ``` 資料夾上傳至server ```powershell= scp -r /path/to/local/file username@remote_host:/path/to/remote/destination ``` 資料下載到本地端 ```powershell= scp username@remote_host:/path/to/remote/file /path/to/local/destination ``` 資料夾下載到本地端 ```powershell= scp -r username@remote_host:/path/to/remote/file /path/to/local/destination ``` Reference - [VSCode SSH Remote settings](https://code.visualstudio.com/docs/remote/ssh)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up