Github Ping不通的解决办法

今天想从github仓库拉取Operating System: Threee Pieces 的教程代码到本地运行
结果迟迟拉不过来,卡在那里。猜测是网络问题

 

 

ping 223.5.5.5 正常

ping www.bing.com 也正常

说明网络是没问题的。
ping github.com 卡住长时间没有反应,也没有报错

image

猜测是DNS被污染了,具体是运营商投毒还是伟大的防火墙进行的污染不得而知。
DNS污染就是在系统发起DNS查询请求查询域名对应的ip地址时,由于某种"众所周知的原因",DNS服务器故意返回一个错误的ip地址。

我们有几种方法可以解决:=

  1. 指定干净的未被投毒的DNS服务器
  2. 修改hosts文件,将github的正确ip写死
  3. 通过代理工具,也就是番蔷来访问。

综合考虑,方案2这种办法只能github这一个网络地址管用,以后遇到其他被DNS污染的网站还得再做配置,而且还要去找github的ip。方案3,代理工具有点大炮打蚊子的感觉,暂时没有必要用,如无必要,勿增实体。使用方案1,简单有效。
系统连上网后,通常从路由器会自动获得DNS服务器
查看系统当前的DNS服务器:

cat /etc/resolv.conf

image
是国内两个常用的DNS服务器,既然被污染了,那么我们使用谷歌的DNS服务器8.8.8.8
首先ping 一下确定能否连通

ping 8.8.8.8

image
这时我们可以选择修改路由器里的DNS,也可以选择修改Linux虚拟机的DNS。
我们这里修改虚拟机的DNS.因为路由器分配什么DNS是不可控的,如果我们去到别的地方联网,路由器我们不一定有修改的权利。
因此修改本机DNS。

既然学,就要稍微深入一些,这样以后遇到相关的问题时解决起来才能游刃有余,那么我们就来了解一下Archlinux这一个典型的Systemd Linux系统的DNS配置吧.
以下内容摘自ArchWiki:

The Name Service Switch (NSS) facility is part of the GNU C Library (glibc) and backs the getaddrinfo(3) API, used to resolve domain names. NSS allows system databases to be provided by separate services, whose search order can be configured by the administrator in nsswitch.conf(5). The database responsible for domain name resolution is the hosts database, for which glibc offers the following services:
files: reads the /etc/hosts file, see hosts(5)
dns: the glibc resolver which reads /etc/resolv.conf, see resolv.conf(5)
systemd provides three NSS services for hostname resolution:
nss-resolve(8) — a caching DNS stub resolver, described in systemd-resolved
nss-myhostname(8) — provides local hostname resolution without having to edit /etc/hosts
nss-mymachines(8) — provides hostname resolution for the names of local systemd-machined(8) containers

个人翻译:
名字服务转换设施(NSS)是CNU C Library(glibc)(即glic标准库)的一部分,并且支持着getaddrinfo API, 用来解析域名。NSS允许系统数据库被由分开的服务提供,它的搜索顺序可以被管理员在nsswitch.conf里配置。这个对域名解析负责的数据库是主机数据库,对于它glibc提供了以下服务:
文件:读取/etc/hosts文件,参见hosts
dns:读取/etc/resolv.conf的glibc解析器,参见resolv.conf

systemd为主机名解析提供了三种NSS服务:
nss-resolve(8)-一个缓存DNS根解析器,在systemd-resolved里有描述。
nss-myhostname(8)- 无需编辑/etc/hosts提供本地主机名解析
nss-mymachines(8) - 为本地systemd-machined(8) 容器提供主机名解析

ArchWiki上的资料有时候比较零散,所以看完这里我们还是不清楚正确且最优雅的DNS配置方式是什么?

我们找到一段:

The glibc resolver reads /etc/resolv.conf for every resolution to determine the nameservers and options to use.

glibc resolver 为每个解析读取/etc/resolv.conf来决定使用的域名服务器以及选项。
ok,既然glibc resolver是标准库,那么git应该是调用标准库的接口函数咯,所以我们只需要修改/etc/resolv.conf文件里的域名服务器就可以啦。

当然以前我们也是这么改,但是没有理论依据,现在有了。
因为/etc/resolv.conf是Linux的glibc标准库里的域名解析器要读取的文件。而Linux上的应用程序必然要调用glibc标准库来执行网络连接操作。
另外ArchWiki还提到,其他网络管理程序例如NetworkManager等会修改或者覆写/etc/resolv.conf,为了防止我们在/etc/resolv.conf里的域名服务器被覆盖,可以通过chattr命令来防止这个文件被修改。

Network managers tend to overwrite /etc/resolv.conf, for specifics see the corresponding section:
dhcpcd#/etc/resolv.conf
Netctl#/etc/resolv.conf
NetworkManager#/etc/resolv.conf
ConnMan#/etc/resolv.conf
To prevent programs from overwriting /etc/resolv.conf, it is also possible to write-protect it by setting the immutable file attribute:
chattr +i /etc/resolv.conf

安排:

sudo vim /etc/resolv.conf

image

如图,添加了两个DNS服务器,一个主DNS服务器,一个备用DNS服务器
分别是谷歌和Cloudflare的。
再使用chattr命令为/etc/resolv.conf施加禁制哈哈,防止被其他网络管理程序覆写。

sudo chattr +i /etc/resolv.conf

image

ok,再次尝试Ping github.com

ping github.com

image
怎么回事,还是不通。

难道是因为有DNS缓存?可是网友说archlinux没有DNS缓存呀?
image
真凶到底是谁?
让我们来排查一下,先运行nslookup指定dns服务器查询呢?
image
好吧,nslookup不存在。那就装上网络dns相关的工具:

问:很多和网络有关的命令都没有,ifconfig,route ,nslookup这些都没有,变量没设置错误,用root也找不到,这是什么原因呢?
答:以前net-tools属于base组,装base时自动就装上了,现在哪个组都不属于了,这些工具需要单独安装。其中ifconfig、route在net-tools包中,nslookup、dig在dnsutils包中,ftp、telnet等在inetutils包中,ip命令在iproute2包中。

sudo pacman -S dnsutils

指定dns服务器为8.8.8.8查询github.com

nslookup github.com 8.8.8.8

image

瞅着咋这么眼熟?这不就是之前国内DNS服务器返回的结果么。
Ping一下
image
好吧,不通。
山穷水尽了,难道谷歌的DNS服务器8.8.8.8也被污染了么?
再试试Cloudflare的DNS服务器1.1.1.1呢?

 nslookup github.com 1.1.1.1

image
惊喜的是,返回结果与谷歌DNS不同,ping一下试试

ping 192.30.255.113

image
可以Ping通,结合以上过程,猜测要么是谷歌DNS被污染了,或者这个ip确实是github的ip,但是恰好被墙了。
所以现在的解决办法就是把1.1.1.1放在8.8.8.8前面。
首先解除/etc/resolv.conf的禁制,然后编辑

sudo chattr -i /etc/resolv.conf

image

sudo vim /etc/resolv.conf

image
添加禁制,防止被其他程序修改:

sudo chattr +i /etc/resolv.conf

再次Ping github.com

ping github.com

image
ok,通了,但是!可以看到有33.3333%的丢包率,前面ping 223.5.5.5和www.bing.com时也有50%及33%左右的丢包,访问国内外都有丢包,说明是我们使用的网络质量不佳。毕竟随身wifi里插的是移动物联网卡,移动墙中墙果然名不虚传

可以ping通了,这下总能拉取代码到本地了吧:

ok,问题解决,虽然解决起来很简单,但是为了深入和严谨的了解与解决问题,我们还做了一点探究,也算功不唐捐啦~

但是!!!毕竟移动墙中墙对国外网站不友好,为了保险起见,防止再出现类似从github仓库拉取或提交代码卡或慢的问题,下一篇可能会写终端代理配置,墙是捉摸不透的,还是绕过它吧 @_@

posted @ 2022-12-17 20:50  Adosa(无嗔)  阅读(5841)  评论(2编辑  收藏  举报
levels of contents