VS Code远程开发-SSH模式

Visual Studio Code Remote - SSH 扩展可以让你打开任何使用SSH服务的主机,虚拟机或容器上的一个远程目录,并且能够使用所有VS Code的功能。一旦连接服务器,就可以访问远程服务器文件系统中任何文件和目录。

本地不需要保存任何代码,扩展是在远程服务器上运行命令。最新的VS Code提供了和本地一样的开发体验,包括所有的智能会话()

../../../_images/vscode_remote_dev_ssh.png

系统要求

  • 本地: 安装 OpenSSH 兼容 SSH 客户端

  • 远程SSH服务器: 运行 SSH Server,硬件配置 1GB 内存,2-core CPU 。可以是x86_64或ARMv7l (AArch32), 和 ARMv8l (AArch64)

安装

备注

远程服务器上可以安装 Visual Studio Code 或 Visual Studio Code Insiders (Insiders是beta版本)

  • 在远程服务器 z-dev 的 VS Code中,安装 Remote Development extension pack

    • 使用 VS Code 的 Extension 管理安装

  • (可选)对于Linux和macOS的SSH服务器,可以同时有多个用户访问,建议激活 VS Code的 Remote.SSH: Remote Server Listen On Socket

  • (可选)虽然也支持密码认证,但是建议SSH服务器采用 ssh密钥 认证增强安全性

连接远程服务器

  • 现在可以在本地,例如我在 macOS 客户端,先验证 ssh 登陆远程SSH服务器( 私有云SSH访问 ):

    ssh z-dev
    
  • 在本地macOS上启动一个VS Code,然后按下 F1⇧⌘P 启动命令行面板,然后选择 Remote-SSH: Connect to Host... ,然后输入需要访问的SSH服务器 z-dev

此时VS Code会弹出一个新的窗口连接到远程SSH服务器,开始初始化开发环境,完成后虽然看上去是本地的IDE,但是仔细观察就会看到左下角标识了远程服务器 SSH: z-dev :

../../../_images/vscode_remote_ssh_init.png
  • 此时 File > Open...File > Open Workspace... 打开的是远程服务器上目录,并且安装插件都是在远程VS Code中安装,就可以非常方便完成开发

备注

使用 VS Code Remote SSH 方式开发的优点是比 X持久化远程应用Xpra 更为接近本地原生程序体验,使用更为流畅和方便。

连接远程服务器 Alpine Linux

我在 Alpine Docker镜像 构建的Docker容器上尝试使用 remote ssh 开发模式,发现实际上是失败的:

远程端口启动失败
[22:08:42.130] > Installing to /home/admin/.vscode-server...
> 3698e90ce798%%1%%
> Downloading with wget
[22:08:42.134] > wget is from busybox: yes
[22:08:42.134] stderr> Program 'wget' is provided by busybox
[22:09:10.101] > Download complete
[22:09:10.108] > 3698e90ce798%%2%%
[22:09:10.110] > tar --version: tar (busybox) 1.37.0
[22:09:10.665] > code 1.105.1 (commit 7d842fb85a0275a4a8e4d7e040d2625abbf7f084)
[22:09:10.668] > Starting VS Code CLI...
[22:09:10.675] > Spawned remote CLI: 418
[22:09:10.681] > Waiting for server log...
[22:09:10.721] > Waiting for server log...
[22:09:10.761] > 3698e90ce798: start
...
[22:09:10.762] Remote server is listening on port 43563
[22:09:10.762] Parsed server configuration: {"serverConfiguration":{"remoteListeningOn":{"port":43563},"osReleaseId":"alpine","arch":"x86_64","sshAuthSock":"","display":"","tmpDir":"/tmp","platform":"linux","execServerToken":"aa11aa11-11aa-1aaa-a111-aaa1111a1aa1"},"downloadTime":0,"installTime":0,"serverStartTime":0,"installUnpackCode":"success"}
[22:09:10.763] Persisting server connection details to /Users/admin/Library/Application Support/Code/User/globalStorage/ms-vscode-remote.remote-ssh/vscode-ssh-host-d73e0d00-7d842fb85a0275a4a8e4d7e040d2625abbf7f084-0.120.0-es/data.json
[22:09:10.771] Starting forwarding server. local port 50995 -> socksPort 50961 -> remotePort 43563
[22:09:10.771] Forwarding server listening on port 50995
[22:09:10.772] Waiting for ssh tunnel to be ready
[22:09:10.773] [Forwarding server port 50995] Got connection 0
[22:09:10.775] Tunneled port 43563 to local port 50995
[22:09:10.775] Resolved "ssh-remote+alpine-dev" to "port 50995"
[22:09:10.785] Initizing new exec server for ssh-remote+alpine-dev
[22:09:10.785] Resolving exec server at port 50995
[22:09:10.786] [Forwarding server port 50995] Got connection 1
[22:09:10.797] Failed to set up socket for dynamic port forward to remote port 43563: Socket closed. TCP port forwarding may be disabled, or the remote server may have crashed. See the VS Code Server log above for details.
[22:09:10.800] Failed to set up socket for dynamic port forward to remote port 43563: Socket closed. TCP port forwarding may be disabled, or the remote server may have crashed. See the VS Code Server log above for details.

这个报错是因为默认 sshd_config 关闭了TCP端口转发,所以需要修改 Alpine Docker镜像 运行容器的 /etc/ssh/sshd_config ,设置

调整 AllowTcpForwarding
AllowTcpForwarding yes

然后重启容器,再次执行 remote ssh 连接服务器,就能够完成vscode远程开发环境初始化,并在安装不同语言插件之后进行开发。

重启服务端

远程服务端可能需要重启来修正一些问题,则使用 Remote-SSH: Kill VS Code Server on Host ,可以移除服务器端的Code Server。然后再次重联到新的会话。

参考