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在本地连接一下服务器,如果没有提示输入密码就说明修改成功了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章