Ubuntu Server 22.04无法连接GitHub 求助排查网络故障
无法访问GitHub的排查与解决建议
问题背景
新安装的Ubuntu 22.04.1 LTS服务器,可正常访问Google等主机,但完全无法访问GitHub,ping、curl、git clone均失败,无代理和异常防火墙配置。
Ping测试
# 测试Google ping -c3 google.com PING google.com(ord37s32-in-x0e.1e100.net (2607:f8b0:4009:808::200e)) 56 data bytes 64 bytes from ord31s21-in-x0e.1e100.net (2607:f8b0:4009:808::200e): icmp_seq=1 ttl=115 time=16.6 ms 64 bytes from ord37s32-in-x0e.1e100.net (2607:f8b0:4009:808::200e): icmp_seq=2 ttl=115 time=15.2 ms 64 bytes from ord37s32-in-x0e.1e100.net (2607:f8b0:4009:808::200e): icmp_seq=3 ttl=115 time=10.9 ms --- google.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2004ms rtt min/avg/max/mdev = 10.914/14.210/16.553/2.398 ms # 测试GitHub - 失败,提示‘Destination Host Unreachable’ ping -c3 github.com PING github.com (140.82.113.3) 56(84) bytes of data. From ubuntu (192.168.0.38) icmp_seq=1 Destination Host Unreachable From ubuntu (192.168.0.38) icmp_seq=2 Destination Host Unreachable From ubuntu (192.168.0.38) icmp_seq=3 Destination Host Unreachable --- github.com ping statistics --- 3 packets transmitted, 0 received, +3 errors, 100% packet loss, time 2026ms pipe 3 # 测试GitHub(强制IPv4) ping -c3 github.com PING github.com (140.82.112.4) 56(84) bytes of data. From ubuntu (192.168.0.38) icmp_seq=1 Destination Host Unreachable From ubuntu (192.168.0.38) icmp_seq=2 Destination Host Unreachable From ubuntu (192.168.0.38) icmp_seq=3 Destination Host Unreachable --- github.com ping statistics --- 3 packets transmitted, 0 received, +3 errors, 100% packet loss, time 2020ms pipe 2
curl测试
# 测试Google curl -s --head --request GET google.com > /dev/null; echo $? 0 # 测试GitHub curl -s --head --request GET github.com > /dev/null; echo $? 7
git clone测试
# 失败,提示‘no route to host’ git clone https://github.com/pi-hole/pi-hole.git Cloning into 'pi-hole'... fatal: unable to access 'https://github.com/pi-hole/pi-hole.git/': Failed to connect to github.com port 443 after 18534 ms: No route to host
nslookup测试
# 测试Google nslookup google.com Server: 127.0.0.53 Address: 127.0.0.53#53 Non-authoritative answer: Name: google.com Address: 142.250.191.206 Name: google.com Address: 2607:f8b0:4009:803::200e # 测试GitHub nslookup github.com Server: 127.0.0.53 Address: 127.0.0.53#53 Non-authoritative answer: Name: github.com Address: 140.82.112.4
路由表查询(ip r s)
ip r s default via 192.168.0.1 dev enp0s31f6 proto dhcp src 192.168.0.38 metric 100 1.1.1.3 via 192.168.0.1 dev enp0s31f6 proto dhcp src 192.168.0.38 metric 100 192.168.0.0/24 dev enp0s31f6 proto kernel scope link src 192.168.0.38 metric 100 192.168.0.1 dev enp0s31f6 proto dhcp scope link src 192.168.0.38 metric 100 192.168.0.38 dev enp0s31f6 proto dhcp scope host src 192.168.0.38 metric 100
网络接口信息(ifconfig)
ifconfig enp0s31f6: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.0.38 netmask 255.255.255.0 broadcast 192.168.0.255 inet6 2601:246:4d7f:8361:d681:d7ff:fe8d:637b prefixlen 64 scopeid 0x0<global> inet6 fe80::d681:d7ff:fe8d:637b prefixlen 64 scopeid 0x20<link> ether d4:81:d7:8d:63:7b txqueuelen 1000 (Ethernet) RX packets 231023 bytes 191591697 (191.5 MB) RX errors 0 dropped 8 overruns 0 frame 0 TX packets 174168 bytes 16427225 (16.4 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 device interrupt 16 memory 0xef200000-ef220000 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 10709 bytes 1034079 (1.0 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 10709 bytes 1034079 (1.0 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
路由追踪(traceroute)
# 测试Google traceroute to google.com (172.217.1.110), 30 hops max, 60 byte packets 1 ubuntu (192.168.0.38) 3060.551 ms !H 3060.478 ms !H 3060.429 ms !H # 测试GitHub traceroute to github.com (140.82.114.4), 30 hops max, 60 byte packets 1 ubuntu (192.168.0.38) 3049.846 ms !H 3049.774 ms !H 3049.725 ms !H
解决建议
手动添加GitHub IP段路由
DNS解析正常但路由无法到达,可手动添加GitHub所属IP段的路由:sudo ip route add 140.82.112.0/20 via 192.168.0.1 dev enp0s31f6测试有效后,可将路由写入
/etc/netplan/配置文件实现永久生效。检查路由器拦截规则
服务器本身无异常防火墙,但路由器可能存在针对GitHub IP段的黑名单、端口限制(如443端口)。登录路由器后台排查规则,或尝试重启路由器。切换公共DNS服务器
替换当前默认DNS为公共DNS(如8.8.8.8或1.1.1.1):sudo echo "nameserver 8.8.8.8" > /etc/resolv.conf注意:Ubuntu 22.04中
/etc/resolv.conf多为软链接,永久修改需配置systemd-resolved或netplan。临时禁用IPv6
部分网络环境下IPv6会导致访问异常,临时禁用测试:sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1 sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1有效则写入
/etc/sysctl.conf永久生效。排查本地iptables规则
确认是否存在隐性拦截规则:sudo iptables -L -n若有针对GitHub IP的拦截,用
iptables -D命令删除对应规则。
内容的提问来源于stack exchange,提问作者user9074332
相关产品推荐
相关产品推荐

