git push failed: ssh: connect to host github.com port 22: Connection timed out fatal: Could not read from remote repository.
报错:
$ git push -u origin main |
ssh: connect to host github.com port 22: Connection timed out
是因为 SSH 使用端口 22 连接 GitHub,而该端口被防火墙或网络策略阻止了。这种情况在校园网、公司内网或中国大陆环境中很常见。
解决方法
✅ 改用 HTTPS 协议(推荐)
现在是使用 SSH 协议连接(git@github.com
),可以改成 HTTPS 协议,这样就不用走端口 22 了。
🔍 原因分析
1. SSH 默认使用的 22 端口被防火墙/网络限制了
这是最常见的原因。SSH 协议连接 GitHub 默认走端口 22,而很多校园网、公司网络或者酒店 Wi-Fi 会出于安全考虑屏蔽这个端口。
ssh: connect to host github.com port 22: Connection timed out |
就表明客户端根本连不上这个端口。
2. 网络提供商/运营商干预(尤其是在中国大陆)
GitHub 的 SSH 服务可能受到干扰或降速,尤其是端口 22。这在中国大陆网络环境下也很常见。