【Git】连接github时的疑难杂症(DNS解析失败)

大家好,我是jstart千语。最近在将项目推送到github的时候,突然github就拒绝访问了,即使挂了VPN,网页也进不去,通过git也不能把代码推送上去。

即使后面看别人的一些解决方案,比如取消代理啊、更换ssh的方式、该22端口号为443等等,通通都解决不了。

具体报错如下:

使用https的方式推送:

bash 复制代码
fatal: unable to access 'https://github.com/js***018/qi*********ackend.git/': Failed to connect to github.com port 443 after 21031 ms: Couldn't connect to server

改用SSH的方式推送:

bash 复制代码
ssh: connect to host ssh.github.com port 22: Connection refused
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

改用443端口也报了这个错误:

bash 复制代码
ssh: connect to host ssh.github.com port 443: Connection refused
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

后面通过更改host文件

添加了一些git配置,结果:github网页可以正常访问,但本地代码还是推送不上去

秘钥正常运行情况

使用命令

bash 复制代码
ssh-add -l  # 查看已加载密钥
ssh-add ~/.ssh/id_rsa  # 手动加载密钥

报错:

bash 复制代码
Could not open a connection to your authentication agent.

这表明 SSH 认证代理(ssh-agent)没有正常运行。ssh-agent 用于管理 SSH 私钥,在使用 ssh-add 命令时需要它处于运行状态。

使用命令解决:

bash 复制代码
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa

但最终还是不能成功推送代码上去


连接github的debug日志

使用命令:

bash 复制代码
ssh -T git@github.com -v  # -v参数显示详细过程

报错信息:

bash 复制代码
OpenSSH_9.7p1, OpenSSL 3.2.1 30 Jan 2024
debug1: Reading configuration data /c/Users/28435/.ssh/config
debug1: /c/Users/28435/.ssh/config line 1: Applying options for github.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to ssh.github.com [127.0.0.1] port 443.
debug1: connect to address 127.0.0.1 port 443: Connection refused
debug1: Connecting to ssh.github.com [::1] port 443.
debug1: connect to address ::1 port 443: Connection refused
ssh: connect to host ssh.github.com port 443: Connection refused

调试信息来看,问题出在 SSH 客户端尝试连接 ssh.github.com 的 443 端口时,却连接到了本地回环地址 127.0.0.1(IPv4)和 ::1(IPv6),并且连接被拒绝,这通常意味着 DNS 解析出现了错误


解决:

一、清除host相关错误配置

hosts 文件位于 C:\Windows\System32\drivers\etc\hosts。你可以使用文本编辑器(需以管理员身份运行)打开该文件,查看是否存在关于 ssh.github.com 的错误配置。若有,将其删除或注释掉。

二、更改 DNS 服务器

使用公共 DNS 服务器,如 Google 的 DNS(8.8.8.8 和 8.8.4.4)或者阿里云的 DNS(223.5.5.5 和 223.6.6.6)。
Windows系统:

打开 "控制面板",选择 "网络和 Internet" -> "网络连接"。(或直接搜索"网络连接")

右键点击你正在使用的网络连接,选择 "属性"。

在 "此连接使用下列项目" 列表中,选中 "Internet 协议版本 4 (TCP/IPv4)",然后点击 "属性"。

选择 "使用下面的 DNS 服务器地址",输入公共 DNS 服务器地址。
macOS 系统

  • 打开 "系统偏好设置",选择 "网络"。
  • 选择你正在使用的网络连接,点击 "高级"。
  • 在 "DNS" 选项卡中,点击 "+" 号添加公共 DNS 服务器地址。

总之,进入类似这样的界面:

具体更改情况:


验证是否解决:

再次输入命令:

bash 复制代码
ssh -T git@github.com -v  # -v参数显示详细过程

日志:


尝试推送代码:成功

相关推荐
凯子坚持 c11 小时前
Git 多人协作深度解析:从工作流模拟到仓库维护
git
JustHappy12 小时前
「chrome extensions🛠️」我写了一个超级简单的浏览器插件Vue开发模板
前端·javascript·github
阿里嘎多学长13 小时前
2025-12-16 GitHub 热点项目精选
开发语言·程序员·github·代码托管
要站在顶端13 小时前
克隆大型仓库卡住(7%每次就卡住了)
git
五月底_14 小时前
上传大量文件到github repo
git·github
KnowFlow企业知识库16 小时前
KnowFlow v2.3.0 重磅发布:适配 RAGFlow v0.22.1 和 MinerU v2.6.5、新增支持多模态视频解析,让知识库"看见"更多
linux·github
逛逛GitHub20 小时前
一周狂揽 4500 的 Star!这个 AI 流程图开源项目火了。
github
这儿有一堆花21 小时前
软件世界的契约:理解开源协议的逻辑与边界
github·开源协议
CoderJia程序员甲21 小时前
GitHub 热榜项目 - 日榜(2025-12-18)
ai·开源·大模型·github·ai教程
rannn_1111 天前
【Git教程】概述、常用命令、Git-IDEA集成
java·git·后端·intellij-idea