Remote SSH报错“ProxyCommand failed”本质是OpenSSH无法执行代理命令,需确保本地安装系统自带OpenSSH、ProxyCommand中ssh用绝对路径、跳板机私钥免密登录目标机,并正确配置嵌套Host段的~/.ssh/config。

Remote SSH 连接报错 ProxyCommand failed 怎么办
跳板机配置失败最常见的就是这个错误,本质是 VSCode 底层的 OpenSSH 无法执行你写的代理命令。不是插件问题,而是 ssh_config 里 ProxyCommand 的路径、参数或权限出错了。
- 确保本地已安装 OpenSSH(Windows 10/11 建议用系统自带的 OpenSSH Client,别用 Git Bash 的 ssh;macOS/Linux 默认有)
-
ProxyCommand中调用的ssh必须是绝对路径,比如/usr/bin/ssh(macOS/Linux)或C:\Windows\System32\OpenSSH\ssh.exe(Windows)——相对路径会静默失败 - 跳板机用户和目标机用户可以不同,但跳板机上的私钥必须能免密登录目标机(即你在跳板机上执行
ssh user@target不输密码) - 如果跳板机需要密钥登录,别把私钥路径写死在
ProxyCommand里,改用IdentityFile配置段控制更安全
如何写一个可复用的 ~/.ssh/config 跳转配置
VSCode Remote SSH 完全依赖系统 SSH 配置,不识别插件内填的“Host”“Port”等字段。所有跳转逻辑必须落在 ~/.ssh/config 里,且需满足嵌套 Host 段结构。
- 先定义跳板机 Host(比如叫
bastion),指定HostName、User、IdentityFile - 再定义目标机 Host(比如叫
prod-server),设置HostName为目标内网地址(如192.168.10.5),User为目标用户名,并用ProxyCommand调用跳板机连接:ProxyCommand ssh -W %h:%p bastion - 务必加上
ForwardAgent yes(如果目标机还需用你本地密钥访问其他服务)或RequestTTY no(避免交互式提示阻塞连接) - 不要在
ProxyCommand中加-t参数——它会申请伪终端,导致 VSCode 启动失败
Host bastion HostName jump.example.com User alice IdentityFile ~/.ssh/bastion_key Host prod-server HostName 192.168.10.5 User bob ProxyCommand ssh -W %h:%p bastion ForwardAgent yes
VSCode 里选不到 prod-server?检查 SSH 配置加载是否生效
VSCode 不会实时重载 ~/.ssh/config,改完后必须重启 Remote SSH 连接流程,否则旧缓存还在用。
- 点击左下角远程连接状态(显示
SSH TARGET或断开图标),选择Remote-SSH: Connect to Host...,再选prod-server—— 不要直接点“打开远程窗口”快捷入口 - 如果列表为空,按
Ctrl+Shift+P(Win/Linux)或Cmd+Shift+P(macOS),运行Remote-SSH: Kill VS Code Server on Host...清掉残留服务端进程 - 连接时观察 VSCode 右下角通知:若出现
Failed to fetch remote environment,大概率是ProxyCommand执行卡在跳板机登录环节,此时手动在终端跑一遍ssh prod-server看真实报错 - Windows 用户注意:WSL2 下 VSCode Desktop 默认不读取 WSL 的
~/.ssh/config,得把配置复制到 Windows 的%USERPROFILE%\.ssh\config
跳板机是 Windows 主机(如 OpenSSH Server for Windows)怎么配
Windows 跳板机对 ssh -W 支持不稳定,尤其老版本 OpenSSH Server(ProxyCommand 容易卡住或报 Write failed: Broken pipe。
- 优先升级跳板机 OpenSSH Server 到最新版(PowerShell 运行
Get-OpenSSHServerVersion查看) - 降级方案:改用
netcat替代-W,前提是跳板机装了nc(如choco install netcat):ProxyCommand C:\tools\ncat.exe -w 10 %h %p - 更稳妥的做法是启用跳板机的 SSH 网关模式(
GatewayPorts yes+AllowTcpForwarding yes),然后本地用ssh -D建 SOCKS 代理,再配 VSCode 的remote.ssh.useLocalServer和代理设置——但这已脱离纯 SSH 跳转范畴,复杂度陡增
跳板链路越长,SSH 配置里每一层的超时、密钥路径、用户上下文就越容易错位;宁可多拆成两个独立 Host 段,也别在一个 ProxyCommand 里堆三层 ssh 嵌套。











