本文最后更新于:2024年1月14日 晚上

VS code 是微软的轻量级但功能强大的IDE,可扩展的插件众多,基本可以满足所有开发需求。本文介绍使用VS code进行远程调试代码的方法。

环境

安装插件

  • 安装 Remote Development 插件

    会自动安装 Remote-WSL / Containers / SSH 等插件。

配置主机信息

ctrl + shift + p

1
2
3
4
5
6
Host alias
HostName 192.168.10.12
Port 3721
User root
IdentityFile ~\.ssh\id_rsa
IdentitiesOnly yes

vs code 连接远程主机

之后选择目标主机的操作系统。

  • 成功连接到远程主机:

  • 打开文件夹运行程序时,选择使用的Python环境:

  • 插入断点调试程序,与本机无异:

填坑

Bad owner or permissions on C:\Users\Admin/.ssh/config

解决方案参考: https://zixizixi.cn/windows_vscode_ssh_error_bad-owner-or-permissions

原因:在 Windows 系统下的 VSCode 安装 Remote - SSH 扩展后,使用扩展配置 SSH 并进行远程连接,可能会发生 Bad owner or permissions on C:\Users\Administrator/.ssh/config 错误,造成无法进行 SSH 远程连接的问题。

原因是由于使用 Remote - SSH 扩展所依赖的 Remote - SSH: Editing Configuration Files 扩展编辑了 C:\Users\Administrator.ssh\config 文件后,此文件的权限发生了改变。

这之后不但在 VSCode 中由于配置文件权限问题而无法进行 SSH 远程连接,就连使用系统的 PowerShell 进行 SSH 连接时也会报此错误,而把此配置文件删除后,使用 PowerShell 即可正常进行远程连接。但 VSCode 的 SSH 连接又依赖此配置文件,所以就产生了冲突,要么只有 PowerShell 能用,要么就都不能用。

解决方案 一
  • 下载 openssh-portable项目:

    1
    git clone https://github.com/PowerShell/openssh-portable.git

    加速链接:

    1
    git clone git@git.zhlh6.cn:PowerShell/openssh-portable.git
  • 进入项目中的 contrib\win32\openssh 目录中,在该目录使用管理员权限打开powershell窗口,执行:

    1
    .\FixUserFilePermissions.ps1 -Confirm:$false

    执行此命令时若提示 无法加载文件 FixUserFil ePermissions.ps1,因为在此系统上禁止运行脚本 错误,则先执行以下命令,然后输入 Y 回车确认后再重新执行(执行完毕后可以再执行以下命令输入 N 恢复默认配置):

    1
    Set-ExecutionPolicy RemoteSigned
  • 随后在powershell与vs code远程过程中都不会再出现上述错误.

解决方案 二

将config绝对路径填在SSH设置里:

open failed:administratively prohibited:open failed

清理/root/.vscode-server目录(注意:.vscode-server的安装位置vscode第一次远程连接时会显示在vscode终端,如果找不到可以find / -name .vscode-server),vs代码将在再次连接到远程计算机时创建一个新目录。

1
vim /etc/ssh/sshd_config

AllowTcpForwarding yes #注释去掉,改成 yes

1
重启 sshd服务:systemctl restart sshd

参考资料:



文章链接:
https://www.zywvvd.com/notes/environment/vs-code/vs-code-remote-debugging/vs-code-remote-debugging/


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

VS code 远程调试配置
https://www.zywvvd.com/notes/environment/vs-code/vs-code-remote-debugging/vs-code-remote-debugging/
作者
Yiwei Zhang
发布于
2020年11月2日
许可协议