【问题标题】:VSCode Remote SSH Connection FailedVSCode 远程 SSH 连接失败
【发布时间】:2026-01-03 17:45:02
【问题描述】:

我正在尝试使用 Remote-SSH 扩展来编辑 Debian 服务器上的文件。 SSH 连接已正确建立,但随后我收到一条错误消息:

Failed to connect to the remote extension host server

还有日志:

[13:19:04.182] Remote server is listening on port 51569
[13:19:04.182] Parsed server configuration: {"agentPort":51569,"osReleaseId":"debian","arch":"x86_64","webUiAccessToken":"","sshAuthSock":"","tmpDir":"/tmp"}
[13:19:04.184] Starting forwarding server. localPort 59828 -> socksPort 59825 -> remotePort 51569
[13:19:04.185] Forwarding server listening on 59828
[13:19:04.185] Waiting for ssh tunnel to be ready
[13:19:04.186] Tunneled remote port 51569 to local port 59828
[13:19:04.186] Resolved "ssh-remote+home-debian.web-data.host" to "127.0.0.1:59828"
[13:19:04.187] [Forwarding server 59828] Got connection 0
[13:19:04.195] ------
[13:19:04.208] [Forwarding server 59828] Got connection 1
[13:19:04.208] [Forwarding server 59828] Got connection 2
[13:19:04.217] Failed to set up socket for dynamic port forward to remote port 51569: Socket closed. Is the remote port correct?
[13:19:04.227] > channel 3: open failed: administratively prohibited: open failed
[13:19:04.235] Failed to set up socket for dynamic port forward to remote port 51569: Socket closed. Is the remote port correct?
[13:19:04.237] Failed to set up socket for dynamic port forward to remote port 51569: Socket closed. Is the remote port correct?
[13:19:04.241] > channel 4: open failed: administratively prohibited: open failed
> channel 5: open failed: administratively prohibited: open failed

我在其他几台服务器上使用远程 ssh 连接,并且从未出现此错误。我已经测试了一些从网上收集的东西,但到目前为止没有任何效果。

有人可以告诉我问题的原因或可能的解决方案吗?谢谢!

【问题讨论】:

    标签: ssh visual-studio-code remote-debugging vscode-remote


    【解决方案1】:

    在 sshd_config 中将 AllowTcpForwardingno 更改为 yes 并重新启动 sshd 对我有用。

    Raspbian GNU/Linux 10 (buster)上测试

    【讨论】:

    • 在 debian 9 上测试过,有一个部分:AllowAgentForwarding no,在 /etc/ssh/sshd_config 中,但我只将 AllowTcpForwarding no 更改为 yes,它可以工作!非常感谢!
    【解决方案2】:
    Delete '~/.vscode-server' folder on your server, and try reconnect.
    

    不确定是否适用于 Debian 服务器,但这适用于 Ubuntu 18.04

    【讨论】:

    • 与其提供一个推测性的答案,不如提供一个您已经测试过并且知道有效的答案(在 Debian 上),或者可能已被其他人确认。
    • 谢谢——我至少有两次使用 VSCode 的 Remote-SSH 要么完全无法操作,要么工作不正常,这解决了它。我不能说它为什么起作用,我想更好地理解它。在后一种“工作不正确”的情况下,它似乎没有运行我的 ~/.profile,并且在删除了我不再需要的 ~/.bash_profile 之后,新的远程会话没有选择 ~/.profile,但是在杀死远程 vscode-server 并删除 ~/.vscode-server 文件夹后,它又回来了,现在可以正常工作了。
    • 不知道为什么,但这适用于 Ubuntu 18.04。
    • 您能详细解释一下如何做到这一点吗?我应该在哪里输入?我应该换什么?
    【解决方案3】:

    按照这些步骤进行

    1. 更新主机中的sshd_config文件。按照这个命令 nano /etc/ssh/sshd_config 然后将AllowTcpForwarding 设置为yes
    2. 在主机上重启ssh sudo systemctl restart ssh
    3. 删除主机rm -rf /home/<user_name>/.vscode-server中的.vscode-server
    4. 现在再次通过 VS Code 连接。希望它会工作

    【讨论】:

      【解决方案4】:
      [13:19:04.227] > channel 3: open failed: administratively prohibited: open failed
      

      您要连接的远程服务器拒绝为您执行端口转发。假设远程服务器是 OpenSSH,远程服务器上有两个地方可以配置:

      1. 服务器可能配置为不允许通过sshd_config 选项PermitOpenDisableForwarding 选项进行端口转发。
      2. 如果您使用 ssh 密钥进行身份验证,则可以通过 authorized_keys 选项 PermitOpenrestrict 禁用转发。

      【讨论】:

      • 感谢您的帮助!我认为是因为我不使用root用户连接到ssh服务器。会不会是这个问题?
      【解决方案5】:

      FWIW;我遇到了同样的问题,重新启动似乎可以解决问题。

      【讨论】:

      • 我同意。在某些情况下,连接在编码会话中间中断,然后尝试重新启动 VSCode 会导致远程连接挂起,或者导致连接成功但无法同步工作区。服务器端守护进程肯定也会挂起,ssh 配置只是故事的一部分。重启remote主机确实可以解决某些问题。
      【解决方案6】:

      我收到了这个错误:

      Could not establish connection to "workspace": Port forwarding is disabled.

      我收到此错误是因为我的远程主机 vm 已重建,导致本地系统上的 known_hosts 文件包含无效密钥。从 known_hosts 中删除旧条目解决了这个问题。

      【讨论】:

        【解决方案7】:

        就我而言,ssh 主机标识已更改 - 我在 ssh-ing 到远程主机时看到此消息:

        @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        @    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
        @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        

        解决方法是首先解决此问题 - 有关详细信息,请参阅ssh remote host identification has changed 的答案

        ssh-keygen -R <host>
        

        【讨论】:

          最近更新 更多