手动配置vscode linux远程开发环境
-
windwos系统 vscode 上安装 remote ssh 插件
-
生成秘钥对
ssh-keygen -t rsa -b 4096 (-b 4096表示生成的rsa秘钥对的长度是4096个bit)
在输入这个指令后,会让我们指定秘钥文件的生成目录和名称
下一步是输入密码 //PS:此处最好不输入密码,直接回车(否则VS Code 以后每次远程连接时,都需要输入此密码)
最后会得到两个文件,以默认路径为例,会生成 id-rsa和id-rsa.pub两个文件, 第一个是私钥,第二个是公钥
默认路径为:
- 将客户端的公钥文件 /c/Users/User/.ssh/id_rsa.pub 拷贝到远程的linux服务器,
将id_rsa.pub中的内容添加到 /root/.ssh/authorized_keys中(没有就创建一个)
cat id_rsa.pub >> /root/.ssh/authorized_keys
- 修改配置文件:在 /etc/ssh/sshd_config 中,把PubkeyAuthentication和RSAAuthentication 配置为 yes,配置AuthorizedKeysFile 为 /root/.ssh/authorized_keys(公钥路径)
PubkeyAuthentication yes
RSAAuthentication yes
# Expect .ssh/authorized_keys2 to be disregarded by default in future.
AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
- 设置访问权限
chmod 755 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
- 配置vscode ssh客户端, 修改C:\Users\User.ssh\config 配置文件并保存
C:\Users\User.ssh\id_rsa 为第二步生成的私钥路径
# Read more about SSH config files: https://linux.die.net/man/5/ssh_config
Host 10.226.13.10
HostName 10.226.13.10
Port 22
User root
IdentityFile "C:\Users\User\.ssh\id_rsa"
- 连接远程开发环境
如果开环境不能连接外网,则此时可以看到vscode终端窗口输出下载vscode-server失败的信息,如下:
[16:54:54.345] Log Level: 2
[16:54:54.345] remote-ssh@0.51.0
[16:54:54.345] win32 x64
[16:54:54.347] SSH Resolver called for "ssh-remote+10.226.13.10", attempt 1
[16:54:54.347] SSH Resolver called for host: 10.226.13.10
[16:54:54.347] Setting up SSH remote "10.226.13.10"
[16:54:54.369] Using commit id "a0479759d6e9ea56afa657e454193f72aef85bd0" and quality "stable" for server
[16:54:54.371] Install and start server if needed
[16:54:54.376] Checking ssh with "ssh -V"
[16:54:54.473] > OpenSSH_for_Windows_7.7p1, LibreSSL 2.6.5
[16:54:54.476] Running script with connection command: ssh -T -D 60668 10.226.13.10 bash
[16:54:54.479] Terminal shell path: C:\Windows\System32\cmd.exe
[16:54:54.745] >
[16:54:54.746] Got some output, clearing connection timeout
[16:54:55.752] > 16169c6c936b: running
> Acquiring lock on /root/.vscode-server/bin/a0479759d6e9ea56afa657e454193f72aef85bd0/vscode-remote-lock.root.a0479759d6e9ea56afa657e454193f72aef85bd0
> \ln /root/.vscode-server/bin/a0479759d6e9ea56afa657e454193f72aef85bd0/vscode-remote-lock.root.a0479759d6e9ea56afa657e454193f72aef85bd0.target /root/.vscode-server/bin/a0479759d6e9ea56afa657e454193f72aef85bd0/vscode-remote-lock.root.a0479759d6e9ea56afa657e454193f
> 72aef85bd0
> Installing to /root/.vscode-server/bin/a0479759d6e9ea56afa657e454193f72aef85bd0...
> 16169c6c936b%%1%%
> Downloading with wget
>
[16:55:02.768] > wget download failed
此时我们需要手动安装vscode-server
(1)从日志中获取commit id :
a0479759d6e9ea56afa657e454193f72aef85bd0
(2)下载vscode-server:
https://update.code.visualstudio.com/commit:${commit_id}/server-linux-x64/stable
(3)创建vscode-server目录
mkdir -p ~/.vscode-server/bin/${commit_id}
(4) 将下载的vscode-server-linux-x64.tar.gz解压到~/.vscode-server/bin/${commit_id} 路径下
tar -zxvf /tmp/vscode-server-linux-x64.tar.gz -C ~/.vscode-server/bin/${commit_id} --strip 1
(5) 重新连接远程环境