国内访问 Github 慢的解决方法

文章目录

国内用户访问 GitHub 慢怎么办

GitHub 这个网站相信大家都比较熟悉,但是在国内访问完全是看缘分,对 GitHub 说"爱"太难了:访问慢、图片加载不出来,甚至根本无法访问。

那么怎么办呢,当然最好的方式是你可以自行搭建代理服务器,这种可以突破各种 404 的问题,不过你得需要一点技术和成本。

如果你只是需要访问 Github 的话,可以试试以下介绍的这种通过修改 host 实现国内流畅访问的方法,修改 host 完全不需要投入成本,也无需安装任何程序。

本文介绍的解决方案来自 521xueweihan/Github520Licoy/fetch-github-hosts

通过修改本地 hosts 文件,试图解决:

  • GitHub 访问速度慢的问题
  • GitHub 项目中的图片显示不出的问题

这个方法确实不错,当然这种修改 host 的方法会失效的,需要即时更新。

Github 无法访问解决步骤

1、以 Windows 系统为例,打开 C:\Windows\System32\drivers\etc 路径下的 hosts 文件,在 hosts 文件内加上如下内容并保存

Github 可用的 host

140.82.112.26                 alive.github.com
140.82.112.6                  api.github.com
185.199.110.153               assets-cdn.github.com
185.199.108.133               avatars.githubusercontent.com
185.199.108.133               avatars0.githubusercontent.com
185.199.108.133               avatars1.githubusercontent.com
185.199.108.133               avatars2.githubusercontent.com
185.199.108.133               avatars3.githubusercontent.com
185.199.108.133               avatars4.githubusercontent.com
185.199.108.133               avatars5.githubusercontent.com
185.199.108.133               camo.githubusercontent.com
140.82.113.21                 central.github.com
185.199.108.133               cloud.githubusercontent.com
140.82.114.9                  codeload.github.com
140.82.112.22                 collector.github.com
185.199.108.133               desktop.githubusercontent.com
185.199.108.133               favicons.githubusercontent.com
140.82.113.4                  gist.github.com
52.216.153.252                github-cloud.s3.amazonaws.com
54.231.139.201                github-com.s3.amazonaws.com
52.217.228.97                 github-production-release-asset-2e65be.s3.amazonaws.com
54.231.168.233                github-production-repository-file-5c1aeb.s3.amazonaws.com
52.216.48.89                  github-production-user-asset-6210df.s3.amazonaws.com
192.0.66.2                    github.blog
140.82.114.4                  github.com
140.82.112.18                 github.community
185.199.109.154               github.githubassets.com
151.101.129.194               github.global.ssl.fastly.net
185.199.110.153               github.io
185.199.108.133               github.map.fastly.net
185.199.110.153               githubstatus.com
140.82.112.25                 live.github.com
185.199.108.133               media.githubusercontent.com
185.199.108.133               objects.githubusercontent.com
13.107.42.16                  pipelines.actions.githubusercontent.com
185.199.108.133               raw.githubusercontent.com
185.199.108.133               user-images.githubusercontent.com
13.107.238.51                 vscode.dev

hosts 文件在每个系统的位置不一,详情如下:

  • Windows 系统:C:\Windows\System32\drivers\etc\hosts
  • Linux 系统:/etc/hosts
  • Mac(苹果电脑)系统:/etc/hosts
  • Android(安卓)系统:/system/etc/hosts
  • iPhone(iOS)系统:/etc/hosts

把上面的 Github 可用的 host 复制到 hosts 文件的文本末尾:

  • Windows 使用记事本,如果无法编辑,鼠标右键打开文件属性将“只读”取消勾选并应用保存。
  • Linux、Mac 使用 Root 权限:sudo vi /etc/hosts。
  • iPhone、iPad 须越狱、Android 必须要 root。

2、激活生效

大部分情况下是直接生效,如未生效可尝试下面的办法,刷新 DNS:

  • Windows:按 Win+R 打开运行框,输入 cmd 打开命令窗口,输入 ipconfig/flushdns 命令会提示成功刷新 DNS 解析缓存的提示
  • Linux 命令:/etc/init.d/network restartsudo nscd restart,如报错则须安装:sudo apt install nscdsudo /etc/init.d/nscd restart
  • Mac 命令:sudo killall -HUP mDNSResponder

如果上述方法无效,请尝试重启一下机器。

然后就可以愉快的访问 Github 了!

自动更新 Github 可用 hosts

下面的地址无需访问 GitHub 即可获取到最新的 hosts 内容:

你可以采用设置定时任务通过 shell 从上面两个地址获取最新内容更新 hosts 文件,也可以借助一些图形化界面工具,推荐使用 SwitchHosts 工具管理 hosts。

在 SwitchHosts 中添加规则时,可以将类型指定为远程,将上面文件地址填入 URL 设置自动更新频率即可。

Unix/Linux 一键使用

sed -i "/# fetch-github-hosts begin/Q" /etc/hosts && curl https://hosts.gitcdn.top/hosts.txt >> /etc/hosts

提示:可以设置 crontab 定时任务定时获取更新即可,解放双手!


也可以看看


全国大流量卡免费领

19元月租ㆍ超值优惠ㆍ长期套餐ㆍ免费包邮ㆍ官方正品