解决GitHub SSH连接超时问题及分析
作者:全都是泡饃
解决GitHub SSH连接超时(端口22)问题,可改用备用端口443,操作步骤:修改SSH配置文件,指定Port 443并设置IdentityFile路径(Windows需完整路径),保存后测试连接,最后重新克隆仓库
GitHub SSH连接超时
遇到报错:
ssh: connect to host github.com port 22: Connection timed out
通常意味着网络无法通过默认的 SSH 端口(22)连接到 GitHub。
GitHub 为 SSH 连接提供了一个备用端口(443),这通常能解决端口 22 被防火墙或网络策略阻止的问题。
这是最可能快速解决问题的方案。
操作步骤
1. 修改 SSH 配置文件
打开或创建 SSH 配置文件:
在文件中添加以下内容:
- Linux/macOS:
~/.ssh/config
- Windows:
C:\Users\<你的用户名>\.ssh\config
Host github.com Hostname ssh.github.com User git Port 443 PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa # 如果密钥是其他名称或路径,请修改此处,例如 id_ed25519
对于 Windows 用户,IdentityFile
路径可能需要写全,例如 IdentityFile C:\Users\<你的用户名>\.ssh\id_rsa
2. 验证连接
保存配置文件后,打开终端(Terminal/Git Bash),运行以下命令测试连接:
ssh -T git@github.com
如果配置成功,会看到类似的成功验证消息:
Hi <your-username>! You've successfully authenticated, but GitHub does not provide shell access.
3. 再次克隆
再次运行git clone
命令查看问题是否解决。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。