解决IPv6 only 访问Github失败的问题
在 IPv6 only 网络环境下,可通过追加 hosts 映射解决 GitHub 访问失败问题。
本文提供三种系统下的命令方式与手动方式,适用于 IPv6 only 环境访问 GitHub 失败的场景。
一、映射内容
以下内容为本文统一使用的 hosts 映射:
# https://danwin1210.de/github-ipv6-proxy.php
2a01:4f8:c010:d56::2 github.com
2a01:4f8:c010:d56::3 api.github.com
2a01:4f8:c010:d56::4 codeload.github.com
2a01:4f8:c010:d56::6 ghcr.io
2a01:4f8:c010:d56::7 pkg.github.com npm.pkg.github.com maven.pkg.github.com nuget.pkg.github.com rubygems.pkg.github.com
2a01:4f8:c010:d56::8 uploads.github.com
2606:50c0:8000::133 objects.githubusercontent.com www.objects.githubusercontent.com release-assets.githubusercontent.com gist.githubusercontent.com repository-images.githubusercontent.com camo.githubusercontent.com private-user-images.githubusercontent.com avatars0.githubusercontent.com avatars1.githubusercontent.com avatars2.githubusercontent.com avatars3.githubusercontent.com cloud.githubusercontent.com desktop.githubusercontent.com support.github.com
2606:50c0:8000::154 support-assets.githubassets.com github.githubassets.com opengraph.githubassets.com github-registry-files.githubusercontent.com github-cloud.githubusercontent.com二、命令方式(推荐)
Linux
sudo tee -a /etc/hosts >/dev/null <<'EOF'
# https://danwin1210.de/github-ipv6-proxy.php
2a01:4f8:c010:d56::2 github.com
2a01:4f8:c010:d56::3 api.github.com
2a01:4f8:c010:d56::4 codeload.github.com
2a01:4f8:c010:d56::6 ghcr.io
2a01:4f8:c010:d56::7 pkg.github.com npm.pkg.github.com maven.pkg.github.com nuget.pkg.github.com rubygems.pkg.github.com
2a01:4f8:c010:d56::8 uploads.github.com
2606:50c0:8000::133 objects.githubusercontent.com www.objects.githubusercontent.com release-assets.githubusercontent.com gist.githubusercontent.com repository-images.githubusercontent.com camo.githubusercontent.com private-user-images.githubusercontent.com avatars0.githubusercontent.com avatars1.githubusercontent.com avatars2.githubusercontent.com avatars3.githubusercontent.com cloud.githubusercontent.com desktop.githubusercontent.com support.github.com
2606:50c0:8000::154 support-assets.githubassets.com github.githubassets.com opengraph.githubassets.com github-registry-files.githubusercontent.com github-cloud.githubusercontent.com
EOFmacOS
sudo tee -a /etc/hosts >/dev/null <<'EOF'
# https://danwin1210.de/github-ipv6-proxy.php
2a01:4f8:c010:d56::2 github.com
2a01:4f8:c010:d56::3 api.github.com
2a01:4f8:c010:d56::4 codeload.github.com
2a01:4f8:c010:d56::6 ghcr.io
2a01:4f8:c010:d56::7 pkg.github.com npm.pkg.github.com maven.pkg.github.com nuget.pkg.github.com rubygems.pkg.github.com
2a01:4f8:c010:d56::8 uploads.github.com
2606:50c0:8000::133 objects.githubusercontent.com www.objects.githubusercontent.com release-assets.githubusercontent.com gist.githubusercontent.com repository-images.githubusercontent.com camo.githubusercontent.com private-user-images.githubusercontent.com avatars0.githubusercontent.com avatars1.githubusercontent.com avatars2.githubusercontent.com avatars3.githubusercontent.com cloud.githubusercontent.com desktop.githubusercontent.com support.github.com
2606:50c0:8000::154 support-assets.githubassets.com github.githubassets.com opengraph.githubassets.com github-registry-files.githubusercontent.com github-cloud.githubusercontent.com
EOF
# 刷新 DNS 缓存
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponderWindows(PowerShell 管理员)
$hostsPath = "$env:SystemRoot\System32\drivers\etc\hosts"
@"
# https://danwin1210.de/github-ipv6-proxy.php
2a01:4f8:c010:d56::2 github.com
2a01:4f8:c010:d56::3 api.github.com
2a01:4f8:c010:d56::4 codeload.github.com
2a01:4f8:c010:d56::6 ghcr.io
2a01:4f8:c010:d56::7 pkg.github.com npm.pkg.github.com maven.pkg.github.com nuget.pkg.github.com rubygems.pkg.github.com
2a01:4f8:c010:d56::8 uploads.github.com
2606:50c0:8000::133 objects.githubusercontent.com www.objects.githubusercontent.com release-assets.githubusercontent.com gist.githubusercontent.com repository-images.githubusercontent.com camo.githubusercontent.com private-user-images.githubusercontent.com avatars0.githubusercontent.com avatars1.githubusercontent.com avatars2.githubusercontent.com avatars3.githubusercontent.com cloud.githubusercontent.com desktop.githubusercontent.com support.github.com
2606:50c0:8000::154 support-assets.githubassets.com github.githubassets.com opengraph.githubassets.com github-registry-files.githubusercontent.com github-cloud.githubusercontent.com
"@ | Add-Content -Path $hostsPath
ipconfig /flushdns三、手动教程
Linux
- 获取 root 权限:
sudo -i。 - 编辑 hosts 文件:
nano /etc/hosts或vim /etc/hosts。 - 把“映射内容”整段追加到文件末尾并保存。
- 重新测试:
curl -I https://github.com。
macOS
- 打开终端,执行:
sudo nano /etc/hosts。 - 把“映射内容”整段追加到文件末尾并保存。
- 刷新 DNS:
sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder。 - 重新测试:
curl -I https://github.com。
Windows
- 以管理员身份打开记事本。
- 打开文件:
C:\Windows\System32\drivers\etc\hosts。 - 把“映射内容”整段追加到文件末尾并保存。
- 以管理员身份打开 CMD,执行:
ipconfig /flushdns。 - 重新测试:浏览器访问
https://github.com。
四、验证是否生效
Linux / macOS
ping -6 github.com -c 2
curl -I https://github.comWindows
ping -6 github.com
curl -I https://github.com五、回滚方法
如果后续网络环境恢复正常,可删除刚追加的 hosts 记录并刷新 DNS。
Linux / macOS
手动编辑 /etc/hosts,删除新增的 GitHub 映射后保存。
Windows
手动编辑 C:\Windows\System32\drivers\etc\hosts,删除新增的 GitHub 映射后保存,再执行:
ipconfig /flushdns