VS Code編輯遠程服務器文件

VS Code編輯遠程服務器文件

使用擴展Remote Development,連接遠程服務器。

Remote Development是VS Code的官方插件用於連接遠程文件夾。
This Remote Development extension pack includes three extensions:

Remote - SSH - Work with source code in any location by opening folders on a remote machine/VM using SSH. Supports x86_64 and ARMv7l (AArch32) glibc-based Linux hosts. VS Code Insiders also supports ARMv8l (AArch64) glibc-based Linux hosts.
Remote - Containers - Work with a sandboxed toolchain or container based application by opening any folder mounted into or inside a container.
Remote - WSL - Get a Linux-powered development experience from the comfort of Windows by opening any folder in the Windows Subsystem for Linux.

安裝插件

直接在VS Code插件中搜索Remote Development然後直接安裝就可以。

連接遠程文件夾

需要先在服務器上開啓SSH服務

配置

  1. 在VS Code設置中添加"remote.SSH.showLoginTerminal": true

  2. 點擊VS Code左下角 長得像><>^<的按鈕,然後選擇 Remote-SSH:Connect to Host...,然後按照提示配置就可以了,期間會提示輸入linux賬戶密碼,輸入就可以了。

遇到的問題

遇到報錯 Downloading VS Code Server failed. Please try again later.

Running remote connection script
Installing to /home/xxx/.vscode-server/bin/2213894ea0415ee8c85c5eea0d0ff81ecc191529...
Downloading with wget
.... 
"install" terminal command done
Received install output: b7a67c2b-5d04-47c6-8be8-999adf02ef0c##25##
Server download failed
Downloading VS Code Server failed. Please try again later.
------

解決方案:
手動在服務器上下載文件,並替換。

cd ~/.vscode-server/bin/*/
wget https://update.code.visualstudio.com/commit:2213894ea0415ee8c85c5eea0d0ff81ecc191529
#commit: 後面那串數字就是剛剛提示安裝失敗信息裏的那串
tar -zxvf vscode-server-linux-x64.tar.gz -C ./
mv vscode-server-linux-x64/* .
touch 0
#再重新連接一下就好了

不想一直都要輸入密碼

解決方案,將本地SSH公鑰添加到Linux服務器的信任裏面

  1. 進入本地的 C://Users/YourName/.ssh/ 文件夾
  2. 查看是否存在 id_rsa/ 如果不存在使用 ssh-keygen生成
  3. id_rsa/id_rsa.pub複製到 服務器 的 ~/.ssh/, 這裏可以爲這個公鑰文件起一個新的名字
  4. /etc/ssh/sshd_config中的 PubkeyAuthentication yesAuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2的註釋,並將剛剛拷過來的文件的名字加在.ssh/authorized_keys後面。
  5. 重啓ssh服務 ubuntu用 sudo /etc/init.d/ssh restart,centos用systemctl restart sshd.service
  6. 可以試一下,用ssh name@server_ip在本地連接一下服務器,如果沒有提示輸入密碼就說明修改成功了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章