Vscode ssh macOS 失败解决方法
TL; DR
mac 通过 vsode ssh 连接目标 mac 会无法完成连接. 以至于 vscode ssh 出现 timeout 的错误. 但在 termianl 中 ssh 目标 mac 是能够正常工作的. 这就很奇怪了, 好在最终解决了. 故写下本文.
但是我也尝试过 mac 连接目标 linux, vscode ssh 能够正常连接且工作...
但是模拟 vscode ssh 行为之后, 会由于目标 mac 的 sshd error: [com.apple.network.libinfo:si_destination_compare] send failed: Invalid argument 导致连接 timeout
正文
在平时, 我个人都是通过 SSH 的方式去连接开发机, 从而可以方便的在本地开发各种系统下的程序, 比如我的机器是 Windows, 然后就可以通过 ssh 到我的虚拟机 Linux 里, 进行 linux 开发. 避免了繁重的系统切换. 以至于我一直认为 ssh 过程就是一个配置公钥到目标机器的 ~/.ssh/authorized_keys
中简单过程.
但是最近, 更新了主机设备为 mac, 并且要 ssh 连接的目标机器也是 macOS. 却出现了 terminal 能够成功连接, 而 vscode ssh 却无法连接的问题. 这个问题值得探讨和记录. 这也正是当前这篇博客会出现的缘由.
场景复现:
Terminal 连接
~/.ssh/config 内容如下:
# snip ...
Host 100.90.90.2
Port 22
User yunique
IdentityFile ~/.ssh/id_ed25519
打开 vscode, 并且在 vscode 的 terminal 中输入以下内容:
yunique@Mac ~/d/p/r/bbb ((c9734a6d))> ssh 100.90.90.2
Last login: Sat Sep 13 17:28:36 2025 from 100.90.90.1
Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish
⋊> yunique@targetMac ⋊> ~
# 成功链接, 工作正常
Vscode remote (ssh) 连接
[16:57:20.478] ---------- [Session Summary] -----------
[16:57:20.479] [Timeout]: Error: Timeout (Connecting with SSH timed out)
[16:57:20.479] ----------------------------------------
[16:57:20.481] Local server exit: 15
[16:57:20.893] Opening exec server for ssh-remote+7b22686f73744e616d65223a22756e6963416972227d
[16:57:20.901] Initizing new exec server for ssh-remote+7b22686f73744e616d65223a22756e6963416972227d
[16:57:20.901] Acquiring local install lock: /var/folders/pk/fx3xyzn17r706msgvh9v36q40000gn/T/vscode-remote-ssh-84ad7bba-install.lock
[16:57:20.901] Looking for existing server data file at /Users/yunique/Library/Application Support/Code/User/globalStorage/ms-vscode-remote.remote-ssh/vscode-ssh-host-84ad7bba-f220831ea2d946c0dcb0f3eaa480eb435a2c1260-0.120.0-es/data.json
[16:57:20.902] No existing data file
[16:57:20.902] Using commit id "f220831ea2d946c0dcb0f3eaa480eb435a2c1260" and quality "stable" for server
[16:57:20.902] Extensions to install:
[16:57:20.904] Install and start server if needed
[16:57:20.907] askpass server listening on /var/folders/pk/fx3xyzn17r706msgvh9v36q40000gn/T/vscode-ssh-askpass-4f333376bddb19ac9d1e4a7ca0f401bedb5bcecb.sock
[16:57:20.908] Spawning local server with {"serverId":2,"ipcHandlePath":"/var/folders/pk/fx3xyzn17r706msgvh9v36q40000gn/T/vscode-ssh-askpass-ae47768992e934a3b995d82adf93ef7b79c103f7.sock","sshCommand":"ssh","sshArgs":["-v","-T","-D","53017","-o","ConnectTimeout=15","targetMac"],"serverDataFolderName":".vscode-server","dataFilePath":"/Users/yunique/Library/Application Support/Code/User/globalStorage/ms-vscode-remote.remote-ssh/vscode-ssh-host-84ad7bba-f220831ea2d946c0dcb0f3eaa480eb435a2c1260-0.120.0-es/data.json"}
[16:57:20.908] Local server env: {"SSH_AUTH_SOCK":"/private/tmp/com.apple.launchd.cF3xkOgGOS/Listeners","SHELL":"/bin/zsh","DISPLAY":"1","ELECTRON_RUN_AS_NODE":"1","SSH_ASKPASS":"/Users/yunique/.vscode/extensions/ms-vscode-remote.remote-ssh-0.120.0/out/local-server/askpass.sh","VSCODE_SSH_ASKPASS_NODE":"/Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper (Plugin).app/Contents/MacOS/Code Helper (Plugin)","VSCODE_SSH_ASKPASS_EXTRA_ARGS":"","VSCODE_SSH_ASKPASS_MAIN":"/Users/yunique/.vscode/extensions/ms-vscode-remote.remote-ssh-0.120.0/out/askpass-main.js","VSCODE_SSH_ASKPASS_HANDLE":"/var/folders/pk/fx3xyzn17r706msgvh9v36q40000gn/T/vscode-ssh-askpass-4f333376bddb19ac9d1e4a7ca0f401bedb5bcecb.sock"}
[16:57:20.908] Spawned 84918
[16:57:20.908] Using connect timeout of 17 seconds
[16:57:21.012] > local-server-2> Running ssh connection command: ssh -v -T -D 53017 -o ConnectTimeout=15 yuniqueAir
[16:57:21.014] > local-server-2> Spawned ssh, pid=84924
[16:57:21.020] stderr> OpenSSH_9.9p2, LibreSSL 3.3.6
[16:57:21.402] stderr> Authenticated to 100.90.90.2 ([100.90.90.2]:22) using "publickey".
# 重点为上面这行, 说明成功连接上了, 但是由于一些特殊的原因, 导致 vscode ssh 的会话被 hang 住了, 以至于在限定时间内没有得到回复而导致 vscode 报错: timeout
[16:57:37.913] Terminating local server
[16:57:37.915] Exec server for ssh-remote+7b22686f73744e616d65223a22756e6963416972227d failed: Error: Connecting with SSH timed out
[16:57:37.915] Error opening exec server for ssh-remote+7b22686f73744e616d65223a22756e6963416972227d: Error: Connecting with SSH timed out
[16:57:37.916] ---------- [Session Summary] -----------
[16:57:37.916] [Timeout]: Error: Timeout (Connecting with SSH timed out)
[16:57:37.916] ----------------------------------------
[16:57:37.924] Local server exit: 15
[16:57:21.402] stderr> Authenticated to 100.90.90.2 ([100.90.90.2]:22) using "publickey".
# 重点为上面这行, 说明成功连接上了, 但是由于一些特殊的原因, 导致 vscode ssh 的会话被 hang 住了, 以至于在限定时间内没有得到回复而导致 vscode 报错: timeout
那么我们就应该手动模拟 vscode 的 ssh 连接方式, 并且在 目标 mac 上也开启一个会话用于监控这 ssh 连接行为, 来找出目标 mac 为什么会无法通过 vscode ssh 的连接.
模拟 vscode ssh, 进而分析行为找出问题
先在 terminal 中
ssh 100.90.90.2
执行
log stream --predicate 'process == "sshd"'
追踪 sshd 的日志在我的主机上使用
ssh -vv -N -D 8080 100.90.90.2
来模拟 vscode ssh 连接.分析第 2 步拿到的目标 mac 上的 sshd 日志.
我的主机上输入 ssh -vv -N -D 8080 100.90.90.2
:
yunique@Mac ~/d/p/r/lifit ((c9734a6d))> ssh -vv -N -D 8080 yuniqueAir
OpenSSH_9.9p2, LibreSSL 3.3.6
debug1: Reading configuration data /Users/yunique/.ssh/config
debug1: /Users/yunique/.ssh/config line 16: Applying options for yuniqueAir
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Reading configuration data /etc/ssh/ssh_config.d/100-macos.conf
debug1: /etc/ssh/ssh_config.d/100-macos.conf line 1: Applying options for *
debug1: Reading configuration data /etc/ssh/crypto.conf
debug2: resolve_canonicalize: hostname 100.90.90.2 is address
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to 100.90.90.2 [100.90.90.2] port 22.
debug1: Connection established.
debug1: identity file /Users/yunique/.ssh/id_ed25519 type 3
debug1: identity file /Users/yunique/.ssh/id_ed25519-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_9.9
debug1: Remote protocol version 2.0, remote software version OpenSSH_9.9
debug1: compat_banner: match: OpenSSH_9.9 pat OpenSSH* compat 0x04000000
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to 100.90.90.2:22 as 'yunique'
debug1: load_hostkeys: fopen /Users/yunique/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: local client KEXINIT proposal
# snip ...
debug1: Local forwarding listening on 127.0.0.1 port 8080.
debug2: fd 6 setting O_NONBLOCK
debug1: channel 1: new port-listener [port listener] (inactive timeout: 0)
debug2: fd 3 setting TCP_NODELAY
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: filesystem
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: client_input_hostkeys: searching /Users/yunique/.ssh/known_hosts for 100.90.90.2 / (none)
debug1: client_input_hostkeys: searching /Users/yunique/.ssh/known_hosts2 for 100.90.90.2 / (none)
debug1: client_input_hostkeys: hostkeys file /Users/yunique/.ssh/known_hosts2 does not exist
debug1: client_input_hostkeys: no new or deprecated keys from server
debug1: pledge: network
debug1: Remote: /Users/yunique/.ssh/authorized_keys:1: key options: agent-forwarding port-forwarding pty user-rc x11-forwarding
debug1: Remote: /Users/yunique/.ssh/authorized_keys:1: key options: agent-forwarding port-forwarding pty user-rc x11-forwarding
^Cdebug1: channel 0: free: port listener, nchannels 2
debug1: channel 1: free: port listener, nchannels 1
# 卡住了.. 只能使用 ctrl + c 来退出... 成功模拟 vscode ssh 连接的行为, 并且复现问题. 那么此时就应该去
ssh 成功 connected + authorized, 成功模拟 vscode ssh 连接的行为, 并且复现问题. 那么此时就应该去分析对应的 sshd 日志了.
目标 mac 的 sshd 日志:
⋊> yunique@targetMac ⋊> ~ log stream --predicate 'process == "sshd"'
Filtering the log data using "process == "sshd""
Timestamp Thread Type Activity PID TTL
2025-09-13 17:28:40.142590+0800 0x970d58 Error 0x0 96881 0 sshd: (libsystem_info.dylib) [com.apple.network.libinfo:si_destination_compare] send failed: Invalid argument
2025-09-13 17:28:40.142985+0800 0x970d58 Error 0x0 96881 0 sshd: (libsystem_info.dylib) [com.apple.network.libinfo:si_destination_compare] send failed: Invalid argument
2025-09-13 17:28:40.143008+0800 0x970d58 Error 0x0 96881 0 sshd: (libsystem_info.dylib) [com.apple.network.libinfo:si_destination_compare] send failed: Invalid argument
2025-09-13 17:28:40.150773+0800 0x970d58 Activity 0x10bc950 96881 0 sshd: (libsystem_info.dylib) Retrieve User by Name
由上面的日志能够看到, 有多个 Error: [com.apple.network.libinfo:si_destination_compare] send failed: Invalid argument
这个便是问题所在.
而这个问题再进一步分析:
这不是一个SSH的配置错误,也不是VSCode的错误,而是一个非常底层的、发生在远程服务器 100.90.90.2
上的 macOS 系统级错误。
错误解读
libsystem_info.dylib
: 这是macOS的一个核心系统库,负责处理用户、网络等系统信息的查询。si_destination_compare
:sshd
在处理网络相关的请求时,调用了这个系统库里的一个函数,该函数用于“比较网络目标地址”。send failed: Invalid argument
: 这个函数在尝试和系统底层服务通信时失败了,因为它传递的某个参数被系统认为是“无效的”。
翻译成人话就是: 当VSCode请求端口转发时,远程服务器上的 sshd
进程为了设置这个转发,向macOS系统查询或比较某些网络地址信息,但这个查询动作触发了macOS系统自身的一个Bug或不稳定问题,导致 sshd
进程出错,后续流程无法继续,连接最终超时。
解决办法
既然问题出在远程macOS系统的一个底层Bug上,我们无法直接修复 macOS 本身,但我们可以尝试改变 VSCode 连接的行为,来绕过这个Bug。
最有效的方法是改变VSCode管理SSH连接的引擎。
打开 Visual Studio Code。
进入设置。可以通过
Cmd + ,
快捷键,或者点击左下角齿轮图标 -> "Settings"。在设置搜索框中,搜索
useLocalServer
。您会找到一个名为 "Remote.SSH: Use Local Server" 的选项。
取消勾选这个选项。确保它处于关闭状态。
为什么要这样做?
Use Local Server
是VSCode较新的一个特性,它使用一个本地服务来管理和优化SSH连接。禁用它会使VSCode回退到一种更传统、更直接地调用系统ssh
命令的方式来建立连接。这种“老办法”发出的请求很可能就和 terminal 简单 ssh 连接时一样,从而绕过触发macOS系统Bug 的那个特定的代码路径。
再次通过 vscode ssh 尝试连接
100.90.90.2
。成功连接并且正常工作