使用VS Code配合Remote Development插件連接遠程服務器(Mac/Linux+Windows) | Using VS Code with Remote Development Connect to Remote Server (Mac/Linux+Windows)


最新版VS Code(2019年6月)出了一系列新的插件,包括Remote Development,Remote SSH等,使得用戶可以使用VS Code遠程連接服務器寫代碼,方便了協同工作。具體配置(Mac/Linux, Windows)操作如下:

1.首先,在插件市場中搜索這些插件,安裝Remote Development即可將一系列插件,包括Remote SSH等一同安裝。

2.接下來需要生成ssh的key。
打開命令行,輸入:
ssh-keygen -t rsa -b 4096
接下來,分別輸入保存路徑(為空則默認為./.ssh/id_rsa)和密碼.

3.生成ssh key了之后,需要將ssh key放到遠程服務器上。
對於Mac/Linux,輸入下面的命令:

ssh-copy-id your-user-name-on-host@host-fqdn-or-ip-goes-here

對於windows,則分別輸入下面的命令:

SET REMOTEHOST=your-user-name-on-host@host-fqdn-or-ip-goes-here

scp %USERPROFILE%\.ssh\id_rsa.pub %REMOTEHOST%:~/tmp.pub

ssh %REMOTEHOST% "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat ~/tmp.pub >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys && rm -f ~/tmp.pub"

4.接下來,就可以配置ssh的文件並且登錄遠程服務器了。如下圖,點擊Configure a SSH host:

選擇剛才生成的ssh key的路徑(./.ssh/config)

輸入相應的內容,包括Host, User, HostName(替換%%里的文字). 如果ssh不是保存在默認目錄下,還可以輸入IdentityFile來指明具體的保存路徑。

Host %name%
    HostName %ip%
    User %user%
    IdentityFile %path%

配置好之后就可以連接了!

注意:
對於Windows,如果沒能連接上,可以在管理員權限下打開powershell,輸入下面的命令配置ssh服務。

Set-Service ssh-agent -StartupType Automatic
Start-Service ssh-agent
Get-Service ssh-agent

參考文獻:
https://code.visualstudio.com/docs/remote/troubleshooting#_configuring-key-based-authentication


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM