背景

错误出现在笔者使用idea开发工具,提交新的代码分支内容,idea报错

 ssh: connect to host github.com port 22: Connection timed out
 fatal: Could not read from remote repository.
 Please make sure you have the correct access rights
 and the repository exists.
 15:19:08.718: [..\..\API-GateWay] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false revert f99bf26088d26ff4132df37488f7dd698f37a4c7
 15:20:34.097: [..\..\API-GateWay] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false reset --soft f99bf26088d26ff4132df37488f7dd698f37a4c7
 15:20:53.383: [..\..\API-GateWay] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/250827-zhaoshunbao-Container-shutdown-monitoring-and-exception-management:refs/heads/250827-zhaoshunbao-Container-shutdown-monitoring-and-exception-management --set-upstream
 ssh: connect to host github.com port 22: Connection timed out
 fatal: Could not read from remote repository.
 Please make sure you have the correct access rights
 and the repository exists.
 15:24:41.582: [..\..\API-GateWay] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/250827-zhaoshunbao-Container-shutdown-monitoring-and-exception-management:refs/heads/250827-zhaoshunbao-Container-shutdown-monitoring-and-exception-management --set-upstream
 ssh: connect to host github.com port 22: Connection timed out
 fatal: Could not read from remote repository.
 Please make sure you have the correct access rights
 and the repository exists.
 15:31:38.582: [..\..\API-GateWay] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/250827-zhaoshunbao-Container-shutdown-monitoring-and-exception-management:refs/heads/250827-zhaoshunbao-Container-shutdown-monitoring-and-exception-management --set-upstream
 ssh: connect to host github.com port 22: Connection timed out
 fatal: Could not read from remote repository.
 Please make sure you have the correct access rights
 and the repository exists.
 ​

超时原因

 ssh: connect to host github.com port 22: Connection timed out
 ​

说明网络环境(运营商 / 公司 / 防火墙 / VPN)把 SSH 的 22 端口给拦了。这个情况在国内和公司网络里很常见。

询问一下GPT为什么昨天22端口就还可以,但是今天就不行了的原因

🔹 1. 运营商 / 网络环境的策略变化

  • 在中国大陆,很多运营商、校园网、企业网默认会阻断或限流 SSH 的 22 端口,原因主要是:

    • 防止用户绕过他们的网络管理(22 端口常用来开隧道、内网穿透)。

    • 减少滥用(22 端口容易被黑客爆破扫描,安全部门会要求统一封掉)。

  • 有时候不是永久封,而是“动态拦截”:比如晚上/白天不同策略,或者临时升级网关规则 → 于是就出现昨天能连、今天不行的情况。


🔹 2. 公司/校园网络的防火墙

  • 内部网络安全组可能会统一封掉 22 端口出站,避免员工直接 SSH 出去建隧道。

  • 这种情况下,只允许常见端口(80、443)对外。


🔹 3. VPN/代理模式变化

  • 如果你开过 VPN,昨天用的是 全局模式,SSH 可以走代理 → 22 可用。

  • 今天可能切成了 仅浏览器模式ssh.exe 走不了代理,就直接连公网 → 被运营商拦截。


🔹 4. GitHub 侧没有问题

GitHub 的 github.com:22 是正常开放的,全世界都能连。我们这边日志显示 IP 能解析 (20.205.243.166),但超时,就是本地到目标的链路被阻断,而不是 GitHub 拒绝。

解决方案

解决过程

而笔者此次提交的使用的是校园网,且昨天提交都还好好的

于是在本地windows的PowerShell 执行

 PS C:\Users\hp-pc> Test-NetConnection ssh.github.com -Port 443                                                                                                                ComputerName     : ssh.github.com                                                     RemoteAddress    : 20.205.243.160                                                      RemotePort       : 443                                                                 InterfaceAlias   : 以太网                                                               SourceAddress    : 10.16.33.78
 TcpTestSucceeded : True
 ​
 ​
 ​
 PS C:\Users\hp-pc> Test-NetConnection github.com -Port 22
 警告: TCP connect to (20.205.243.166 : 22) failed
 警告: Ping to 20.205.243.166 failed with status: TimedOut
 ​
 ​
 ComputerName           : github.com
 RemoteAddress          : 20.205.243.166
 RemotePort             : 22
 InterfaceAlias         : 以太网
 SourceAddress          : 10.16.33.78
 PingSucceeded          : False
 PingReplyDetails (RTT) : 0 ms
 TcpTestSucceeded       : False
 ​

发现443端口可行,而22端口不行

但是笔者也怀疑是VPN的问题,于是尝试将VPN

  • 从规则模式切换为全局模式,不行

  • 将VPN取消系统代理,再退出,仍然不行

所以如上,笔者发现自己可能只能参考github官方文档所写的

配置SSH 443端口

  1. 打开(或新建)文件:

     C:\Users\xxx\.ssh\config

    (注意:文件名就叫 config,没有后缀)

  2. 写入以下内容:

     Host github.com
       HostName ssh.github.com
       Port 443
       User git
       ServerAliveInterval 60
       ServerAliveCountMax 3
       TCPKeepAlive yes
  3. 保存后,在 PowerShell 测试:

     ssh -T [email protected]

    键入命令后可能出现

     The authenticity of host '[ssh.github.com]:443 ([20.205.243.160]:443)' can't be established.
     ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
     This host key is known by the following other names/addresses:
         C:\Users\hp-pc/.ssh/known_hosts:1: github.com
     Are you sure you want to continue connecting (yes/no/[fingerprint])? 

    这是因为

    • 电脑之前通过 22 端口连接的是 github.com:22,对应的 host key 已经存在 ~/.ssh/known_hosts 文件里。

    • 而现在我们走的是 ssh.github.com:443,虽然它本质上还是 GitHub,但在 SSH 客户端看来是新主机地址+端口组合,所以要确认一次。

    这里直接输入yes,然后回车。 SSH 会把这台新主机 (ssh.github.com:443) 的指纹存到你的:

     C:\Users\xxx\.ssh\known_hosts

    以后再连就不会再问了。

    如果配置成功,会看到:

     Hi xxxxx! You've successfully authenticated, but GitHub does not provide shell access.

最后由于我们使用的idea进行push,因此这里我们需要检查idea的版本

  • 老版本,设置->版本控制->Git->SSH executable / SSH 可执行文件->选项有 Built-inNative->请选择 Native(这样才能读取你 C:\Users\xxxx\.ssh\config 里“走 443 端口”的配置)

  • 版本较新,设置中没有上述老版本的配置选项,直接重新提交即可