这篇文章整理的是访问 GitHub 时遇到连接不稳定、解析异常或加载缓慢时的排查思路。早期很多人会通过修改 hosts 文件临时指定 GitHub 的 IP,但这种方式现在并不推荐长期使用。
为什么不建议长期写死 hosts #
GitHub 使用了全球化的基础设施,域名解析结果会随着地区、运营商、CDN 和服务调整而变化。某个时间点可用的 IP,过一段时间可能就失效,甚至可能解析到不适合当前网络的节点。
如果长期在 hosts 文件中写死旧 IP,可能会导致访问更慢、证书异常、部分接口不可用,或者某些子域名加载失败。
先做基础排查 #
遇到 GitHub 访问问题时,可以先检查 DNS:
nslookup github.com或者:
dig github.com再测试网络连通性:
ping github.com如果 ping 不通,不一定代表网站不可访问,因为部分服务可能限制 ICMP。更直接的方法是使用 curl:
curl -I https://github.com如果 HTTPS 能返回响应头,说明网站本身可以访问。
可以尝试的处理方式 #
第一,刷新 DNS 缓存。Windows 可以执行:
ipconfig /flushdnsLinux 上根据发行版和 DNS 服务不同,可以重启 systemd-resolved 或网络服务。
第二,检查系统代理、浏览器代理、公司网络策略和安全软件。有些访问异常并不是 DNS 问题,而是代理配置或防火墙导致。
第三,使用稳定的 DNS 服务。可以根据所在网络选择运营商 DNS、路由器 DNS,或者其他可靠 DNS。不要频繁混用多个来源不明的 DNS。
第四,如果确实临时修改 hosts,修改前要备份,修改后要记录时间,并在问题解决后及时恢复。
hosts 文件位置 #
Windows hosts 文件位置通常是:
C:\Windows\System32\drivers\etc\hostsLinux 和 macOS 通常是:
/etc/hosts修改 hosts 需要管理员权限。修改后如果没有生效,可以刷新 DNS 缓存,或者重启浏览器。
总结 #
GitHub 访问异常时,不建议直接长期复制网上的 hosts IP。更好的做法是先判断问题出在 DNS、网络、代理还是本地缓存。hosts 只能作为临时排查手段,不能作为稳定方案。
但是由于某些原因实在无法访问,可以尝试以下方案:
编辑 hosts 文件,加入如下内容:
52.74.223.119 github.com
192.30.253.119 gist.github.com
54.169.195.247 api.github.com
185.199.111.153 assets-cdn.github.com
151.101.76.133 raw.githubusercontent.com
151.101.76.133 gist.githubusercontent.com
151.101.76.133 cloud.githubusercontent.com
151.101.76.133 camo.githubusercontent.com
151.101.76.133 avatars0.githubusercontent.com
151.101.76.133 avatars1.githubusercontent.com
151.101.76.133 avatars2.githubusercontent.com
151.101.76.133 avatars3.githubusercontent.com
151.101.76.133 avatars4.githubusercontent.com
151.101.76.133 avatars5.githubusercontent.com
151.101.76.133 avatars6.githubusercontent.com
151.101.76.133 avatars7.githubusercontent.com
151.101.76.133 avatars8.githubusercontent.com