你有没有遇到过,Github 用的好好的,突然就无法通过 ssh 提交代码了,遇到

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

遇到这个问题,很可能是你用 ssh 访问 github.com 的网络链路中,某个节点的 22 端口被封了,
但是通常情况下 443 端口不会被封,幸运的是 GitHub 提供了 ssh.github.com 域名通过 443 端口进行 ssh 连接的方式,可参考Use SSH over HTTPS port
这样就可以绕过封 22 端口(即使走 443 端口,其实也是可以区分 SSH 流量和 TLS 流量的)😉

所以解决方案就是改变 ssh 连接 Github 的配置

这里有两种方式:

  1. 修改.ssh/config 文件,里边添加/修改 Github 的连接信息,修改为
Host github.com
  Hostname ssh.github.com
  Port 443
  1. 修改 git 的配置
git config --global "url.ssh://[email protected]:443/.insteadOf" [email protected]:

其实就是在 ~/.gitconfig 添加如下内容:

[url "ssh://[email protected]:443/"]
insteadOf = [email protected]:

我更建议使用方法 2,原因是此设置指定了 Git 使用 SSH 的方式,而不是修改 SSH 通用配置。

参考: https://github.com/orgs/community/discussions/54558