TANKENQI.cn

February 11, 2025

Git SSH 连接 GitHub 超时?一文搞定!🚀

Terminal2.2 min to read

在使用 SSH 方式克隆 GitHub 仓库时,你可能遇到如下错误:

git clone git@github.com:VCityTeam/py3dtilers.gitCloning into 'py3dtilers'...ssh: connect to host github.com port 22: Operation timed outfatal: Could not read from remote repository.Please make sure you have the correct access rightsand the repository exists.

这通常是 SSH 端口 22 被封锁,或者 SSH 配置不正确。本篇文章将手把手教你如何解决!💡

1 🔹 SSH vs HTTPS 方式的区别

Git 提供了 两种拉取代码的方式

方式传输协议端口认证方式
HTTPSHTTPS(基于 TLS)443需要输入用户名+密码或使用 Personal Access Token (PAT)
SSHSSH(基于 OpenSSH)22(或自定义端口)使用 SSH Key 进行公钥认证(免密码)

为什么 SSH 更推荐?

部分公司/学校/网络环境可能屏蔽了 SSH 端口 22,导致 git clone 失败。

2 解决方案

2.1 使用 GitHub SSH 备用端口 443

GitHub 提供了 SSH 443 端口,可以绕过端口 22 的封锁:

git clone ssh://git@ssh.github.com:443/VCityTeam/py3dtilers.git

如果这样可以正常拉取,说明 你的网络封锁了 SSH 22 端口

💡 让 Git 永久使用 443 端口

git config --global url."ssh://git@ssh.github.com:443".insteadOf "ssh://git@github.com"

然后你可以直接:

git clone git@github.com:VCityTeam/py3dtilers.git

Git 会自动走 443 端口 连接。

2.2 测试 SSH 连接

运行:

ssh -T git@github.com

可能的输出:

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

说明 SSH 正常工作,问题可能是网络屏蔽了端口 22

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

说明端口 22 被封,请尝试 SSH 走 443 端口(见 方法 1)。

2.3 检查 SSH 配置

🔹 确保 SSH Key 已添加到 GitHub

  1. 检查本地 SSH Key 是否存在
ls -l ~/.ssh/id_rsa.pub
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  1. 添加公钥到 GitHub
cat ~/.ssh/id_rsa.pub
  1. 测试 SSH 是否正常
ssh -T git@github.com

2.3 配置 ~/.ssh/config 走端口 443

如果你不想每次都手动指定端口,可以配置 SSH:

nano ~/.ssh/config

添加:

Host github.com  Hostname ssh.github.com  User git  Port 443

保存后,测试:

ssh -T git@github.com

如果成功,Git 以后就会默认走 443 端口

2.4 使用 HTTPS(临时解决方案)

如果你只是临时拉取代码,可以使用 HTTPS:

git clone https://github.com:VCityTeam/py3dtilers.git

HTTPS 方式每次推送都需要输入 GitHub Token,不如 SSH 方便。

3 🔹 总结

问题解决方案
端口 22 被封使用 ssh://git@ssh.github.com:443/... 方式拉取
SSH 配置错误确保 SSH Key 已添加到 GitHub,ssh -T git@github.com 测试
全局使用 SSH 443 端口~/.ssh/config 里设置 Port 443

如果是公司/学校封了端口 22最推荐使用 SSH 443 端口 🚀

4 💬 你的 SSH 现在能用了么?

如果你遇到问题,欢迎在评论区交流!🎯