VSCode使用Remote SSH连接远程服务器

1 前言

最近在搭建一套 VSCode 的远端开发环境,主要想解决 C/C++ 代码不能 100% 跳转的问题。

闲言少叙,安装过程奉上。

2 操作步骤

2.1 安装 SSH

由于我们是使用 SSH 连接远程服务器,因此 Windows 需要支持 SSH 。

可以通过安装 Git 来获取 SSH 功能,如何验证?使用 Win + R ,输入 cmd 打开控制窗口,直接输入 ssh ,如下提示,则代表 SSH 已经安装成功;

2.2 添加插件

使用 VSCode 最方便的一点,就是有很多插件可供选择。

本次我们需要使用 Remote - SSH 插件,长这个样子,不要搞错了

安装完插件后,在侧边栏可以看到 “远程资源管理器” 图标。

2.3 配置SSH密钥

目的:将本机添加到远程服务器连接白名单,让服务器知道是已认证的电脑在连接。过程类似于 GitHub 网站添加本地电脑的 SSH 公钥。

1、使用如下命令,生成 SSH 公钥文件。如果已经生成,则可直接使用,跳到步骤二。

复制

# 方法一 
ssh-keygen 
 
# 方法二,参考使用Git生成密钥 
ssh-keygen -t rsa -C "youremail@example.com" 

2、一般生成的密钥文件,路径为:C:\Users\user\.ssh ,找到公钥文件 id_rsa.pub ,复制到远程服务器 根目录 的 .ssh 文件夹中。

(1)根目录,不一定非要是 /.ssh 路径,可以是自己的用户目录,类似这样:/zhaochen/.ssh。

(2).ssh 文件夹没有怎么办?新建一个文件夹,命名为 .ssh 即可。同时要确认远程服务器是否支持 SSH ,如果此时正是通过 SSH 方式连接的,那肯定是支持了。

3、生成 authorized_keys 文件。这样后续在使用 Remote 插件时,不需要密码,就可以直接登录到服务器。

(1)连接到远程服务器

  • 方法一:通过 Windows 的 cmd 命令框,使用如下命令 + 密码,连接到远程服务器

复制

# 标准命令,输入后会提示输入密码 
ssh username@ip -p port 
 
# 示例,注:SSH默认为22端口 
ssh zhaochen@192.168.1.1 -p 22 
  • 方法二:已经使用 SSH 方式连接到服务器

(2)进入 .ssh 目录,使用如下命令,生成 authorized_keys 文件。

复制

cat id_rsa.pub > authorized_keys 
  • 1.

4、在 cmd 窗口,再次尝试如下,可以看到无需密码,直接连上服务器

如果已使用 SSH 方式连接到远程服务器,小二再分享一种更简单的方法:

1、在 .ssh 文件夹下,新建 authorized_keys 文件;

2、将 id_rsa.pub 文件中的内容全部复制到 authorized_keys 文件即可;

2.4 添加配置文件

目的:配置 VSCode 连接远程服务器的一些基本信息。

1、点击左侧的 “远程资源管理器” 图标,点击右上角的小齿轮(设置)

2、在弹出来的窗口中,选择第一个 config 文件打开,参考下图,填写对应信息

(1)参考上述配置,释义如下

复制

Host <远程主机名称> 
    HostName <远程主机IP> 
    User <用户名> 
    Port <ssh端口,默认22> 
    IdentityFile <本机SSH私钥路径> 
    ForwardAgent yes <VSCode 自己添加的,不用管> 
  • Host :连接的主机名称,可自定义;
  • Hostname :远程主机的 IP 地址;
  • User :用于登录远程主机的用户名;
  • Port :用于登录远程主机的端口,SSH 默认为 22 ;
  • IdentityFile :本地的私钥文件 id_rsa 路径;

(2)如果需要连接多个远程服务器,可参考如上内容,配置多个即可;

复制

Host <远程主机名称1> 
    HostName <远程主机1 IP> 
    User <用户名1> 
    Port <ssh端口,默认22> 
    IdentityFile <本机SSH私钥路径> 
    ForwardAgent yes <VSCode 自己添加的,不用管> 
Host <远程主机名称2> 
    HostName <远程主机2 IP> 
    User <用户名2> 
    Port <ssh端口,默认22> 
    IdentityFile <本机SSH私钥路径> 
    ForwardAgent yes <VSCode 自己添加的,不用管> 

2.5 连接测试

1、进入 “远程资源管理器” 选项,右键点击主机名;

2、选择一种方式,连接到远程服务器,进行测试;

3、如果连接成功,左下角则会显示当前已连接的主机名。

参考资料

1、VS Code Remote SSH配置 - 知乎 (zhihu.com)(https://zhuanlan.zhihu.com/p/68577071)

3 问题汇总

3.1 插件安装

当我们搭建好 Remote SSH 环境后,VSCode 支持在 远程服务器 安装插件。

此时就可以根据自己的开发习惯,将插件安装到不同地方了。

3.2 连接报错

如果错误提示如下:

复制

could not establish connection to “XXX“.Connecting was canceled. 
 
VScode remote '_workbench.downloadResource' failed 
 
vscode could not establish _workbench.downloadResource 

可直接参考这篇文章:VSCode Remote 报错,无法连接??

3.3 扩展ssh连接失败

SSH 连接远程服务器失败,有如下错误提示:

复制

could not establish connection to XXX, the path argument must be of type string, received type undefined。 
  • 1.

问题原因:找不到 SSH 可执行程序。

解决方法如下:

1、按下 F1 ,输入 Remote-SSH: Settings ,找到 remote.SSH.path ,修改此路径为本地 Git 安装程序中的 ssh.exe 文件即可;

2、参考下图:

参考资料

1、Visual studio code 扩展 ssh 连接失败:The "path" argument must be of type string. Received type undefined_的米-漠石's Blog-CSDN博客(https://blog.csdn.net/mostone/article/details/103023476)

3.4 C/C++环境错误

1、错误截图如下:

2、错误日志如下:

正在更新 C/C++ 依赖项...正在下载程序包“C/C++ language components (Linux / x86_64)” 失败。正在重试... 失败。正在重试... 失败。正在重试...正在等待 8 秒... 失败。正在重试...正在等待 16 秒... 未能下载 https://go.microsoft.com/fwlink/?linkid=2164295在阶段 downloadPackages 失败Error: connect ECONNREFUSED 23.41.74.108:443at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)如果在脱机环境中工作或反复看到此错误,请尝试从 https://github.com/microsoft/vscode-cpptools/releases 下载预包含了所有依赖项的扩展版本,然后使用 VS Code 中的“从 VSIX 安装”命令来安装它。

3、问题原因:小二猜测,是由于远程服务器脱机使用插件,导致 C/C++ 的扩展安装失败;

4、解决方法,错误日志中已经比较详细。简要描述如下:

(1)手动下载 cpptools-linux.vsix 文件,放到远程服务器中;网址:Releases · microsoft/vscode-cpptools (github.com)

(2)进入插件选项卡,点击 ··· ,选择 从 VSIX 安装... ,选中刚刚下载的文件;

(3)最后等待安装完毕,点击重载窗口,就大功告成了。

3.5  channel 3: open failed: administratively prohibited: open failed

解决办法:(需要管理员权限)

修改/etc/ssh/sshd_config文件中:

AllowTcpForwarding yes

重启linux

一个参考的sshd_config配置文件:

#    $OpenBSD: ssh_config,v 1.33 2017/05/07 23:12:57 djm Exp $

# This is the ssh client system-wide configuration file.  See
# ssh_config(5) for more information.  This file provides defaults for
# users, and the values can be changed in per-user configuration files
# or on the command line.

# Configuration data is parsed as follows:
#  1. command line options
#  2. user-specific file
#  3. system-wide file
# Any configuration value is only changed the first time it is set.
# Thus, host-specific definitions should be at the beginning of the
# configuration file, and defaults at the end.

# Site-wide defaults for some commonly used options.  For a comprehensive
# list of available options, their meanings and defaults, please see the
# ssh_config(5) man page.

# Host *
#   ForwardAgent no
#   ForwardX11 no
#   PasswordAuthentication yes
#   HostbasedAuthentication no
#   GSSAPIAuthentication no
#   GSSAPIDelegateCredentials no
#   GSSAPIKeyExchange no
#   GSSAPITrustDNS no
#   BatchMode no
#   CheckHostIP yes
#   AddressFamily any
#   ConnectTimeout 0
#   StrictHostKeyChecking ask
#   IdentityFile ~/.ssh/id_rsa
#   IdentityFile ~/.ssh/id_dsa
#   IdentityFile ~/.ssh/id_ecdsa
#   IdentityFile ~/.ssh/id_ed25519
#   Port 22
#   Protocol 2
#   Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc
#   MACs hmac-md5,hmac-sha1,umac-64@openssh.com
#   EscapeChar ~
#   Tunnel no
#   TunnelDevice any:any
#   PermitLocalCommand no
#   VisualHostKey no
#   ProxyCommand ssh -q -W %h:%p gateway.example.com
#   RekeyLimit 1G 1h
#
# To modify the system-wide ssh configuration, create a  *.conf  file under
#  /etc/ssh/ssh_config.d/  which will be automatically included below

# Follow system-wide Crypto Policy, if defined:

Ciphers aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.com,aes128-ctr,aes128-cbc
MACs hmac-sha2-256-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha2-256,hmac-sha1,umac-128@openssh.com,hmac-sha2-512
GSSAPIKexAlgorithms gss-gex-sha1-,gss-group14-sha1-,gss-group1-sha1-
KexAlgorithms curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
PubkeyAcceptedKeyTypes rsa-sha2-256,ecdsa-sha2-nistp256,ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384,ecdsa-sha2-nistp384-cert-v01@openssh.com,rsa-sha2-512,ecdsa-sha2-nistp521,ecdsa-sha2-nistp521-cert-v01@openssh.com,ssh-ed25519,ssh-ed25519-cert-v01@openssh.com,ssh-rsa,ssh-rsa-cert-v01@openssh.com

# Uncomment this if you want to use .local domain
# Host *.local
#   CheckHostIP no

Host *
    GSSAPIAuthentication yes

# If this option is set to yes then remote X11 clients will have full access
# to the original X11 display. As virtually no X11 client supports the untrusted
# mode correctly we set this to yes.
    ForwardX11Trusted yes

# Send locale-related environment variables
    SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
    SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
    SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE
    SendEnv XMODIFIERS

KexAlgorithms ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1
VerifyHostKeyDNS ask

参考资料

1、vscode 解决正在下载程序包“C/C++ language components (Linux / x86_64)” 失败引起的符号找不到问题_chenwr2018的博客-CSDN博客(https://blog.csdn.net/chenwr2018/article/details/115774007)

2、Releases · microsoft/vscode-cpptools (github.com)(https://github.com/microsoft/vscode-cpptools/releases)

4 总结

1、详细总结 VSCode 搭建远程开发环境的过程;

2、对插件的安装、Remote SSH 相关问题做了补充完善,相对具有参考意义;

  • 1
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值