你还在为访问不了github头疼吗?一招解决,骗你是狗

570 阅读2分钟

Github520 解决github访问老大难

如果对你有用,点个赞吧

你是否还在为访问不到github仓库抓耳挠腮,这篇文章绝对帮你搞定,重点是:

不限网络环境!!!

不限网络环境!!!

不限网络环境!!!

除非是你局域网,就当我没说

一、手动方式

1.1 后去host

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

  • 文件:https://raw.hellogithub.com/hosts
  • JSON:https://raw.hellogithub.com/hosts.json

1.2 复制下面的内容

# GitHub520 Host Start
140.82.113.25                 alive.github.com
140.82.113.6                  api.github.com
185.199.108.153               assets-cdn.github.com
185.199.108.133               avatars.githubusercontent.com
185.199.108.133               avatars0.githubusercontent.com
185.199.110.133               avatars1.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.112.21                 central.github.com
185.199.108.133               cloud.githubusercontent.com
140.82.112.9                  codeload.github.com
140.82.114.22                 collector.github.com
185.199.108.133               desktop.githubusercontent.com
185.199.108.133               favicons.githubusercontent.com
140.82.114.4                  gist.github.com
3.5.30.52                     github-cloud.s3.amazonaws.com
3.5.30.49                     github-com.s3.amazonaws.com
54.231.230.33                 github-production-release-asset-2e65be.s3.amazonaws.com
52.217.231.1                  github-production-repository-file-5c1aeb.s3.amazonaws.com
52.217.136.217                github-production-user-asset-6210df.s3.amazonaws.com
192.0.66.2                    github.blog
140.82.112.3                  github.com
140.82.112.17                 github.community
185.199.111.154               github.githubassets.com
151.101.193.194               github.global.ssl.fastly.net
185.199.108.153               github.io
185.199.108.133               github.map.fastly.net
185.199.108.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.110.133               raw.githubusercontent.com
185.199.108.133               user-images.githubusercontent.com
13.107.246.51                 vscode.dev
140.82.112.22                 education.github.com
127.255.0.128                 private-user-images.githubusercontent.com

# Update time: 2024-12-29T10:30:30+08:00
# Update url: https://raw.hellogithub.com/hosts
# Star me: https://github.com/521xueweihan/GitHub520
# GitHub520 Host End

该内容会自动定时更新

1.3 hosts 文件

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

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

修改方法,把第一步的内容复制到文本末尾:

  1. Windows 使用记事本。
  2. Linux、Mac 使用 Root 权限:sudo vi /etc/hosts
  3. iPhone、iPad 须越狱、Android 必须要 root。

1.4 激活生效

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

  1. Windows:在 CMD 窗口输入:ipconfig /flushdns
  2. Linux 命令:sudo nscd restart,如报错则须安装:sudo apt install nscd 或 sudo /etc/init.d/nscd restart
  3. Mac 命令:sudo killall -HUP mDNSResponder

Tips:  上述方法无效可以尝试重启机器。

二、自动方式(推荐)

Tip:推荐 SwitchHosts 工具管理 hosts

以 SwitchHosts 为例,看一下怎么使用的,配置参考下面:

  • Hosts 类型: Remote
  • Hosts 标题: 随意
  • URL: https://raw.hellogithub.com/hosts
  • 自动刷新: 最好选 1 小时

如图:

image.png

这样每次 hosts 有更新都能及时进行更新,免去手动更新。

三、一行命令

Windows

使用命令需要安装git bash 复制以下命令保存到本地命名为fetch_github_hosts

_hosts=$(mktemp /tmp/hostsXXX)
hosts=/c/Windows/System32/drivers/etc/hosts
remote=https://raw.hellogithub.com/hosts
reg='/# GitHub520 Host Start/,/# Github520 Host End/d'

sed "$reg" $hosts > "$_hosts"
curl "$remote" >> "$_hosts"
cat "$_hosts" > "$hosts"

rm "$_hosts"

CMD中执行以下命令,执行前需要替换git-bash.exefetch_github_hosts为你本地的路径,注意前者为windows路径格式后者为shell路径格式

"C:\Program Files\Git\git-bash.exe" -c "/c/Users/XXX/fetch_github_hosts"

可以将上述命令添加到windows的task schedular(任务计划程序)中以定时执行

GNU(Ubuntu/CentOS/Fedora)

sudo sh -c 'sed -i "/# GitHub520 Host Start/Q" /etc/hosts && curl https://raw.hellogithub.com/hosts >> /etc/hosts'

BSD/macOS

sudo sed -i "" "/# GitHub520 Host Start/,/# Github520 Host End/d" /etc/hosts && curl https://raw.hellogithub.com/hosts | sudo tee -a /etc/hosts

将上面的命令添加到 cron,可定时执行。使用前确保 GitHub520 内容在该文件最后部分。