git访问github失败Connection refused解决方案
问题描述
问题1:在git bash中,输入git clone等访问github远程仓库命令,报Connection refused错误:
Administrator@xiaogu-PC MINGW64 ~/Desktop/test_git_clone
$ git clone https://github.com/qinguoyi/TinyWebServer.git
$ git clone https://github.com/qinguoyi/TinyWebServer.git
Cloning into 'TinyWebServer'...
fatal: unable to access 'https://github.com/qinguoyi/TinyWebServer.git/':
Failed to connect to github.com port 443 after 2042 ms:
Connection refused
尝试方案:
- 1、更新本机和github中
ssh key
,确保更新后,问题依旧; - 2、在
~/.ssh/config
文件中添加目的ssh远程主机信息(User改为自己的github邮箱地址),确保config
文件无误后,问题依旧; - 3、考虑git代理问题,然而我并没有设置https.proxy,问题依旧;
- 4、在凭据管理器中删除或修改与github有关
windows凭据
后,问题依旧; - 5、卸载重装
git
后,还是没解决... - 6、重装系统,你们可以试试...[狗头],应该管用...[滑稽]
- ...
- i、排查期间真的是尝试了很多种网络上的方案...花了一整天时间,不管用啊~
- ...
- n、终极方案,没用你打死我
言归正传 --> 原因分析:
半夜1点多还睡不着,为啥会Connection Refused呢?打开手机再尝试查一查...
直到...看到下面这两篇讲解github DNS污染的博客,令我眼前一亮,真是amazing啊
- segmentfault.com/q/101000004…
- getiot.tech/github/gith…
- 在此对以上两篇博客表示感谢!
学习一下:什么是DNS污染?
百度百科解释:
- 网域服务器缓存污染(DNS cache pollution),又称域名服务器缓存投毒(DNS cache poisoning),是指一些刻意制造或无意中制造出来的域名服务器数据包,把域名指往不正确的IP地址。
- 某些网络运营商为了某些目的,对DNS进行了某些操作,导致使用ISP的正常上网设置无法通过域名取得正确的IP地址。
- 某些国家或地区出于某些目的为了防止某网站被访问,而且其又掌握部分国际DNS根目录服务器或镜像,也会利用此方法进行屏蔽。
接下来就是激动人心的bug解决过程了!
- 1、在git bash中输入
ssh -vT git@github.com
命令,debug一下git访问github网站的ssh连接过程:
$ ssh -vT git@github.com
OpenSSH_9.0p1, OpenSSL 1.1.1o 3 May 2022
debug1: Reading configuration data /c/Users/Administrator.DESKTOP-XXX/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to github.com [::1] port 22.
debug1: connect to address ::1 port 22: Connection refused
debug1: Connecting to github.com [127.0.0.1] port 22.
debug1: connect to address 127.0.0.1 port 22: Connection refused
ssh: connect to host github.com port 22: Connection refused
分析上面的debug记录,咦?DNS怎么会将github.com解析到本机ip(127.0.0.1)了?
引用前文提到的博客文章的一句话:“这应该是 DNS 解析出问题了!把github.com 解析到了本机环回地址,于是产生了屏蔽效果。这种现象被称为“DNS 解析污染”,可能是由于 DNS 解析被运营商劫持了,或者使用了科学上网工具等原因造成的。”
- 2、 接下来我们就来查询一下
github.com
真正的ip地址,通过IPAddress.com网站或者在终端使用nslookup
命令查询本区域关于github.com网站的真正域名ip信息地址。下面通过nslookup
命令进行查询:
# 注:8.8.8.8表示显式通过谷歌家的DNS服务器对github.com进行域名解析
$ nslookup github.com 8.8.8.8
Non-authoritative answer:
Server: dns.google
Address: 8.8.8.8
Name: github.com
Address: 20.205.243.166 # 这个就是此时此地的github.com域名解析到的真正ip
- 3、同样,再查询一下
ssh.github.com
的ip地址:
# 注:8.8.8.8表示显式通过谷歌家的DNS服务器对ssh.github.com进行域名解析
$ nslookup ssh.github.com 8.8.8.8
Non-authoritative answer:
Server: dns.google
Address: 8.8.8.8
Name: ssh.github.com
Address: 20.205.243.160
-
4、修改本机hosts文件,hosts 文件在每个系统的位置不一,详情如下:
- Windows 系统:C:\Windows\System32\drivers\etc\hosts
- Linux 系统:/etc/hosts
- Mac(苹果电脑)系统:/etc/hosts
- Android(安卓)系统:/system/etc/hosts
- iPhone(iOS)系统:/etc/hosts
在hosts文件的末尾添加以下两行,表示手动进行DNS域名解析。(注意文件需要有写入权限)
# GitHub
20.205.243.166 github.com
20.205.243.160 ssh.github.com
- 5、修改hosts文件后,重新执行
ssh -vT git@github.com
命令,发现已经可以成功连接github了:
$ ssh -vT git@github.com
OpenSSH_9.0p1, OpenSSL 1.1.1o 3 May 2022
debug1: Reading configuration data /c/Users/Administrator.DESKTOP-S16IQNP/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to github.com [20.205.243.166] port 22.
debug1: Connection established.
debug1: identity file /c/Users/Administrator.DESKTOP-XXX/.ssh/id_rsa type 0
...
# ...此处省略很多行
...
Hi AC-xiaogu! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 3216, received 2704 bytes, in 0.6 seconds
Bytes per second: sent 5571.3, received 4684.3
debug1: Exit status 1
- 6、此时git clone等命令已经可以正常使用了:
Administrator@XXX-PC MINGW64 ~/Desktop/test
$ git clone https://github.com/qinguoyi/TinyWebServer.git
Cloning into 'TinyWebServer'...
remote: Enumerating objects: 816, done.
remote: Counting objects: 100% (219/219), done.
remote: Compressing objects: 100% (73/73), done.
remote: Total 816 (delta 165), reused 146 (delta 146), pack-reused 597
Receiving objects: 100% (816/816), 55.97 MiB | 3.88 MiB/s, done.
Resolving deltas: 100% (457/457), done.
大功告成!
- 之前一直都是看别人的博客文章,自己却没有正式写过技术文章,这算是我的第一篇博客吧哈哈哈,居然是bug解决方案分享文...
- 同志们,要继续加油啊!!!