Kto-Blog
Published on

GitHub Connection Failure Solutions

Authors
  • avatar
    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 密钥未正确配置或权限不足也会导致此错误
CauseDescription
🚧 Port BlockMany network environments block port 22 (default SSH port), causing connection refusal
🔒 Protocol LimitSome networks only allow HTTPS (port 443) traffic to pass through
🔑 Auth IssueSSH 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:

  1. 创建配置文件 📝

  2. Create configuration file 📝

    ~/.ssh 文件夹内新建 config 文件 Create a new config file in the ~/.ssh folder

  3. 添加配置内容 ⚙️

  4. Add configuration content ⚙️

    Host github.com
      Hostname ssh.github.com
      Port 443
    
  5. 测试连接 🔌

  6. 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 as 127.0.0.1), preventing you from connecting to the correct GitHub server.

操作步骤:| Operation Steps:

  1. 检查 DNS 解析 🔎

  2. Check DNS resolution 🔎

    nslookup ssh.github.com
    

    如果结果显示 127.0.0.1::1,说明 DNS 被污染。 If the result shows 127.0.0.1 or ::1, DNS is polluted.

  3. 选择以下任一解决方案 🔀

  4. 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 to C:\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
    
  5. 刷新 DNS 缓存 🔄

  6. 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

Comments

Join the discussion and share your thoughts

Sort after loading
Sign in to post comments and replies