📋 问题描述
当您尝试在本地使用 Git 连接 GitHub 时,可能会遇到以下错误:
ssh: connect to host ssh.github.com port 22: Connection refused
Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
⚠️ 注意:即使使用了代理工具,仍然可能无法正常拉取代码。
🔍 常见原因
原因 | 说明 |
---|---|
🚧 端口封锁 | 许多网络环境会封锁 22 端口(SSH 默认端口),导致连接被拒绝 |
🔒 协议限制 | 部分网络只允许 HTTPS(443 端口)流量通过 |
🔑 认证问题 | SSH 密钥未正确配置或权限不足也会导致此错误 |
🛠️ 解决方案
方法一:修改 SSH 端口
💡 为什么有效?
- GitHub 同时支持 SSH over HTTPS(443 端口)
- 443 端口通常用于 HTTPS 流量,很少被封锁
- 通过 SSH over HTTPS 可以绕过网络限制
操作步骤:
-
创建配置文件 📝
在
~/.ssh
文件夹内新建config
文件 -
添加配置内容 ⚙️
Host github.com Hostname ssh.github.com Port 443
-
测试连接 🔌
尝试重新连接 GitHub
如果仍然报错:
ssh: connect to host ssh.github.com port 443: Connection refused Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
👉 请尝试方法二。
方法二:解决 DNS 污染问题
❓ 什么是 DNS 污染?
DNS 污染会导致某些网络返回错误的 IP 地址(如
127.0.0.1
),使您无法连接到正确的 GitHub 服务器。
操作步骤:
-
检查 DNS 解析 🔎
nslookup ssh.github.com
如果结果显示
127.0.0.1
或::1
,说明 DNS 被污染。 -
选择以下任一解决方案 🔀
📡 方案 A:修改 DNS 服务器使用 Google DNS (
📝 方案 B:修改 hosts 文件8.8.8.8
) 或国内 DNS (114.114.114.114
)在
C:\Windows\System32\drivers\etc\hosts
中添加:
⚙️ 方案 C:修改 SSH 配置140.82.113.4 github.com 140.82.114.36 ssh.github.com
Host github.com Hostname 140.82.114.36 Port 443
-
刷新 DNS 缓存 🔄
ipconfig /flushdns
✅ 验证连接
完成上述步骤后,运行以下命令测试连接:
ssh -T git@github.com
如果看到类似以下消息,表示连接成功!🎉
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
📚 参考资源
- GitHub 官方文档:使用 SSH 连接到 GitHub
- GitHub 官方文档:排查 SSH 连接问题
- GitHub 状态页面 - 检查 GitHub 服务是否正常运行