解决 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.comHostname ssh.github.comUser gitPort 443PreferredAuthentications publickeyIdentityFile ~/.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
命令查看问题是否解决。