You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法通过家庭WiFi在VS Code中远程SSH连接AWS EC2实例

解决家用WiFi下VS Code Remote SSH无法连接AWS EC2实例的问题

从日志里的ssh: connect to host port 22: Connection timed out可以明确,问题出在WiFi环境下的网络层面,而非VS Code配置本身。以下是针对性的解决步骤:

  • 检查家用路由器的端口限制
    多数家用路由器会默认限制对外的SSH(22端口)连接,进入路由器管理后台,找到「防火墙设置」「端口过滤」或「安全规则」选项,确保允许设备向外发起TCP 22端口的连接请求。

  • 直接测试SSH基础连接
    跳过VS Code,用系统自带的SSH命令验证网络连通性:

    ssh -i "你的密钥文件路径.pem" ec2-user@EC2实例公网IP
    

    如果同样超时,确认是网络问题;如果能连接,再排查VS Code的Remote SSH配置。

  • 更新EC2安全组的IP白名单
    AWS EC2的安全组需要允许来源IP访问22端口。移动网络和WiFi的公网IP不同,可能你之前只添加了移动网络的IP。登录AWS控制台,找到对应EC2实例的安全组,添加当前WiFi的公网IP(可通过curl ifconfig.me获取)到入站规则的22端口允许列表。

  • 明确VS Code SSH配置
    打开VS Code的SSH配置文件(通过Remote SSH扩展的「Open SSH Configuration File」选项),给目标主机添加明确参数:

    Host test-crilic
      HostName EC2实例公网IP
      User ec2-user
      IdentityFile C:\本地密钥文件路径.pem
      Port 22
    

    避免依赖本地SSH配置的潜在问题。

  • 验证DNS解析
    如果是用主机名连接EC2,可能WiFi下DNS无法正确解析域名。尝试直接用EC2公网IP替换主机名进行连接,排查解析问题。

WiFi环境下连接日志

[10:53:20.718] Log Level: 2
[10:53:20.722] remote-ssh@0.92.0
[10:53:20.722] win32 x64
[10:53:20.728] SSH Resolver called for "ssh-remote+test-crilic", attempt 1
[10:53:20.728] "remote.SSH.useLocalServer": false
[10:53:20.728] "remote.SSH.showLoginTerminal": false
[10:53:20.729] "remote.SSH.remotePlatform": {"crilc_vikralya":"linux","crilc_bob":"linux","test_ec2":"linux","test-crilic":"linux"}
[10:53:20.729] "remote.SSH.path": undefined
[10:53:20.729] "remote.SSH.configFile": undefined
[10:53:20.729] "remote.SSH.useFlock": true
[10:53:20.729] "remote.SSH.lockfilesInTmp": false
[10:53:20.729] "remote.SSH.localServerDownload": auto
[10:53:20.729] "remote.SSH.remoteServerListenOnSocket": false
[10:53:20.730] "remote.SSH.showLoginTerminal": false
[10:53:20.730] "remote.SSH.defaultExtensions": []
[10:53:20.730] "remote.SSH.loglevel": 2
[10:53:20.730] "remote.SSH.enableDynamicForwarding": true
[10:53:20.730] "remote.SSH.enableRemoteCommand": false
[10:53:20.730] "remote.SSH.serverPickPortsFromRange": {}
[10:53:20.730] "remote.SSH.serverInstallPath": {}
[10:53:20.734] SSH Resolver called for host: test-crilic
[10:53:20.734] Setting up SSH remote "test-crilic"
[10:53:20.741] Using commit id "6261075646f055b99068d3688932416f2346dd3b" and quality "stable" for server
[10:53:20.745] Install and start server if needed
[10:53:20.749] Checking ssh with "ssh -V"
[10:53:20.793] > OpenSSH_for_Windows_8.6p1, Libr
[10:53:20.794] > eSSL 3.4.3

[10:53:20.799] Running script with connection command: ssh -T -D 53829 "test-crilic" bash
[10:53:20.802] Terminal shell path: C:\WINDOWS\System32\cmd.exe
[10:53:42.844] > ssh: connect to host port 22: Connection timed out
> The process tried to write to a nonexistent pipe.
> ]0;C:\WINDOWS\System32\cmd.exe
[10:53:42.845] Got some output, clearing connection timeout
[10:53:44.137] "install" terminal command done
[10:53:44.138] Install terminal quit with output: ]0;C:\WINDOWS\System32\cmd.exe
[10:53:44.138] Received install output: ]0;C:\WINDOWS\System32\cmd.exe
[10:53:44.140] Failed to parse remote port from server output
[10:53:44.142] Resolver error: Error: 
    at g.Create (c:\Users\Lenovo\.vscode\extensions\ms-vscode-remote.remote-ssh-0.92.0\out\extension.js:1:583821)
    at t.handleInstallOutput (c:\Users\Lenovo\.vscode\extensions\ms-vscode-remote.remote-ssh-0.92.0\out\extension.js:1:582473)
    at t.tryInstall (c:\Users\Lenovo\.vscode\extensions\ms-vscode-remote.remote-ssh-0.92.0\out\extension.js:1:679811)
    at process.processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async c:\Users\Lenovo\.vscode\extensions\ms-vscode-remote.remote-ssh-0.92.0\out\extension.js:1:642614
    at async t.withShowDetailsEvent (c:\Users\Lenovo\.vscode\extensions\ms-vscode-remote.remote-ssh-0.92.0\out\extension.js:1:645932)
    at async t.resolve (c:\Users\Lenovo\.vscode\extensions\ms-vscode-remote.remote-ssh-0.92.0\out\extension.js:1:643664)
    at async c:\Users\Lenovo\.vscode\extensions\ms-vscode-remote.remote-ssh-0.92.0\out\extension.js:1:722342
[10:53:44.153] ------

内容的提问来源于stack exchange,提问作者Rishabh Mishra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 15:50:20