- Published on
GitHub Connection Failure Solutions
- Authors

- Name
- Kto
📋 问题描述 | Problem Description
当您尝试在本地使用 Git 连接 GitHub 时,可能会遇到以下错误: When you try to use Git to connect to GitHub locally, you may encounter the following error:
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.
⚠️ 注意:即使使用了代理工具,仍然可能无法正常拉取代码。 ⚠️ Note: Even with proxy tools, you may still be unable to pull code normally.
🔍 常见原因 | Common Causes
| 原因 | 说明 |
|---|---|
| 🚧 端口封锁 | 许多网络环境会封锁 22 端口(SSH 默认端口),导致连接被拒绝 |
| 🔒 协议限制 | 部分网络只允许 HTTPS(443 端口)流量通过 |
| 🔑 认证问题 | SSH 密钥未正确配置或权限不足也会导致此错误 |
| Cause | Description |
|---|---|
| 🚧 Port Block | Many network environments block port 22 (default SSH port), causing connection refusal |
| 🔒 Protocol Limit | Some networks only allow HTTPS (port 443) traffic to pass through |
| 🔑 Auth Issue | SSH keys not properly configured or insufficient permissions can also cause this error |
🛠️ 解决方案 | Solutions
方法一:修改 SSH 端口 | Method 1: Modify SSH Port
💡 为什么有效? 💡 Why it works?
- GitHub 同时支持 SSH over HTTPS(443 端口)
- GitHub supports SSH over HTTPS (port 443)
- 443 端口通常用于 HTTPS 流量,很少被封锁
- Port 443 is typically used for HTTPS traffic and rarely blocked
- 通过 SSH over HTTPS 可以绕过网络限制
- SSH over HTTPS can bypass network restrictions
操作步骤:| Operation Steps:
创建配置文件 📝
Create configuration file 📝
在
~/.ssh文件夹内新建config文件 Create a newconfigfile in the~/.sshfolder添加配置内容 ⚙️
Add configuration content ⚙️
Host github.com Hostname ssh.github.com Port 443测试连接 🔌
Test connection 🔌
尝试重新连接 GitHub Try to reconnect to GitHub
如果仍然报错: If still getting error:
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.👉 请尝试方法二。 👉 Please try Method 2.
方法二:解决 DNS 污染问题 | Method 2: Resolve DNS Pollution Issue
❓ 什么是 DNS 污染? ❓ What is DNS pollution?
DNS 污染会导致某些网络返回错误的 IP 地址(如
127.0.0.1),使您无法连接到正确的 GitHub 服务器。 DNS pollution causes some networks to return incorrect IP addresses (such as127.0.0.1), preventing you from connecting to the correct GitHub server.
操作步骤:| Operation Steps:
检查 DNS 解析 🔎
Check DNS resolution 🔎
nslookup ssh.github.com如果结果显示
127.0.0.1或::1,说明 DNS 被污染。 If the result shows127.0.0.1or::1, DNS is polluted.选择以下任一解决方案 🔀
Choose one of the following solutions 🔀
📡 方案 A:修改 DNS 服务器 📡 Solution A: Modify DNS server
使用 Google DNS (
8.8.8.8) 或国内 DNS (114.114.114.114) Use Google DNS (8.8.8.8) or domestic DNS (114.114.114.114)📝 方案 B:修改 hosts 文件 📝 Solution B: Modify hosts file
在
C:\Windows\System32\drivers\etc\hosts中添加: Add toC:\Windows\System32\drivers\etc\hosts:140.82.113.4 github.com 140.82.114.36 ssh.github.com⚙️ 方案 C:修改 SSH 配置 ⚙️ Solution C: Modify SSH configuration
Host github.com Hostname 140.82.114.36 Port 443刷新 DNS 缓存 🔄
Flush DNS cache 🔄
ipconfig /flushdns
✅ 验证连接 | Verify Connection
完成上述步骤后,运行以下命令测试连接: After completing the above steps, run the following command to test the connection:
ssh -T git@github.com
如果看到类似以下消息,表示连接成功!🎉 If you see a message similar to the following, the connection is successful! 🎉
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
📚 参考资源 | Reference Resources
GitHub 状态页面 - 检查 GitHub 服务是否正常运行
GitHub Status Page - Check if GitHub services are running normally