github连接失败解决办法

GitHub连接问题示意图

📋 问题描述

当您尝试在本地使用 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 可以绕过网络限制
操作步骤:
  1. 创建配置文件 📝

    ~/.ssh 文件夹内新建 config 文件

  2. 添加配置内容 ⚙️

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

    尝试重新连接 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 服务器。

操作步骤:
  1. 检查 DNS 解析 🔎

    nslookup ssh.github.com
    

    如果结果显示 127.0.0.1::1,说明 DNS 被污染。

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

    📡 方案 A:修改 DNS 服务器

    使用 Google DNS (8.8.8.8) 或国内 DNS (114.114.114.114)

    📝 方案 B:修改 hosts 文件

    C:\Windows\System32\drivers\etc\hosts 中添加:

    140.82.113.4 github.com
    140.82.114.36 ssh.github.com
    
    ⚙️ 方案 C:修改 SSH 配置
    Host github.com
      Hostname 140.82.114.36
      Port 443
    
  3. 刷新 DNS 缓存 🔄

    ipconfig /flushdns
    

✅ 验证连接

完成上述步骤后,运行以下命令测试连接:

ssh -T git@github.com

如果看到类似以下消息,表示连接成功!🎉

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

📚 参考资源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值