FIELD NOTE / 2026
DNS 污染不是墙:一次 GitHub 连不上的排查
新机器上第一件事是配环境,结果第一步就卡住了:git clone 超时。
奇怪的是浏览器能开一部分网站。于是开始排查。
现象
curl -o /dev/null -m 12 -w "%{http_code} connect=%{time_connect}\n" https://github.com
# curl: (28) Operation timed out after 12002 milliseconds
# code=000 connect=0.104160s
注意 connect=0.104s —— TCP 三次握手在 100 毫秒内就“完成”了。这不是丢包,是有东西接了连接但什么都不回。
DNS 是解析出来的:
nslookup github.com
# Name: github.com
# Address: 20.205.243.166
关键一步:换 IP 试
20.205.243.166 是亚太区的 GitHub 地址,但在国内不少网络下不可达。手动指定另一个:
curl --resolve github.com:443:140.82.121.4 \
-o /dev/null -m 12 -w "code=%{http_code} connect=%{time_connect}s\n" \
https://github.com
# code=200 connect=0.203059s
通了。
再看 api.github.com,140.82.121.5 到 140.82.112.5 全是 200:
for ip in 140.82.121.5 140.82.121.6 140.82.112.5; do
curl --resolve api.github.com:443:$ip https://api.github.com
done
所以不是防火墙封 IP,是 DNS 返回了错误的 IP。域名本身没被拦,解析结果被换成了不可达的地址。
三种绕过
1. hosts 文件
最直接。Windows 在 C:\Windows\System32\drivers\etc\hosts,Linux/macOS 在 /etc/hosts。
140.82.121.4 github.com
140.82.121.5 api.github.com
140.82.121.9 codeload.github.com
185.199.108.133 raw.githubusercontent.com
Windows 下写这个文件需要管理员权限,普通终端会 Permission denied。用管理员开的记事本或者 PowerShell 改。
改完刷新缓存:
ipconfig /flushdns
2. curl –resolve
临时用,不改系统配置。--resolve 域名:端口:IP 让 curl 绕过 DNS:
curl --resolve github.com:443:140.82.121.4 https://github.com
缺点是每个命令都得带,且只对 curl 生效。
3. 镜像代理
拉大文件(release、tarball)时最省事:
curl -L https://ghproxy.net/https://github.com/user/repo/archive/refs/heads/main.tar.gz
这类服务存活时间不定,别写进脚本里长期依赖。
一个坑
git clone 不吃 http.curloptResolve 这个配置项。试过:
git -c http.curloptResolve=github.com:443:140.82.121.4 clone <url>
# fatal: Failed to connect to github.com:443
git 底层的 libcurl 版本和参数传递方式不一样,这条不通。要 git 能用,必须改 hosts。
如果只是拿代码不打算 push:
curl -L --resolve codeload.github.com:443:140.82.121.9 \
-o repo.tar.gz \
https://codeload.github.com/owner/repo/tar.gz/refs/heads/main
tar -xzf repo.tar.gz
小结
| 判断依据 | 含义 |
|---|---|
connect 极快 + code=000 |
有设备接连接但不应答,典型劫持 |
| DNS 有结果但连不上 | 大概率 DNS 污染 |
| 换 IP 就通 | 确认是 DNS 问题 |
| 换 IP 还不通 | 才是真正的 IP 封锁 |
排查时先分清这两种,能省很多时间 —— 我第一次遇到的时候,以为整个网络都断了,折腾了半天代理。
