GitHub 加速访问三大方案全解析
国内访问 GitHub 慢的三种解法:SwitchHosts 自动维护 hosts、GitHub Hosts 自动更新服务、手动改 hosts 文件,含测速验证与注意事项。
前言:为什么需要加速GitHub?
GitHub作为全球最大的代码托管平台,国内访问常遇到:
- 仓库克隆速度低于50KB/s
- 页面加载超时/403错误
- Releases文件下载失败
📌 据2024开发者网络报告,中国大陆访问GitHub的平均延迟高达380ms
方案一:SwitchHosts智能加速
1. 工具安装
**官方下载地址**:
- [macOS x64](https://github.com/oldj/SwitchHosts/releases/download/v4.1.2/SwitchHosts!_macOS_x64.dmg)
- [Windows x64](https://github.com/oldj/SwitchHosts/releases/download/v4.1.2/SwitchHosts!_windows_x64_4.1.2.6086.exe)
- [Linux AppImage](https://github.com/oldj/SwitchHosts/releases/download/v4.1.2/SwitchHosts!_linux_x86_64_4.1.2.AppImage)
2. 配置规则(全平台通用)
# GitHub加速专用规则
140.82.113.4 github.com
185.199.108.154 github.global.ssl.fastly.net
199.232.69.194 githubusercontent.com
3. 自动更新设置
sequenceDiagram
participant 用户
participant SwitchHosts
participant GitHub
participant 系统hosts
用户->>SwitchHosts: 新建远程规则
SwitchHosts->>GitHub: 每小时请求最新IP
GitHub-->>SwitchHosts: 返回CDN节点数据
SwitchHosts->>系统hosts: 自动更新配置
4. 开源地址
https://github.com/oldj/SwitchHosts/
方案二:GitHub Hosts自动服务
🚀 特点 使用 Cloudflare Workers 部署,无需服务器 使用 Cloudflare DNS (1.1.1.1) 和 Google DNS 进行域名解析 使用 Cloudflare KV 存储数据,确保高可用性 提供 REST API 接口 每 60 分钟自动更新一次 DNS 记录
开源地址:https://github-hosts.tinsfox.com
配置步骤
# macOS 一键脚本
sudo curl -fsSL https://github.com/TinsFox/github-hosts/releases/download/v0.0.1/github-hosts.darwin-arm64 -o github-hosts && sudo chmod +x ./github-hosts && ./github-hosts
liunux/windows未经过测试
# Linux 一键脚本
sudo curl -fsSL https://github.com/TinsFox/github-hosts/releases/download/v0.0.1/github-hosts.linux-amd64 -o github-hosts && sudo chmod +x ./github-hosts && ./github-hosts
# Windows PowerShell (管理员权限)
irm https://github.com/TinsFox/github-hosts/releases/download/v0.0.1/github-hosts.windows-amd64.exe | iex
方案三:手动修改Hosts文件
| 系统平台 | 文件路径 |
|---|---|
| Windows | C:\Windows\System32\drivers\etc\hosts |
| Linux | /etc/hosts |
| macOS | /private/etc/hosts |
推荐IP列表
| 域名 | 推荐IP | TTL |
|---|---|---|
| github.com | 140.82.121.3 | 3600 |
| assets-cdn.github.com | 185.199.111.133 | 1800 |
| raw.githubusercontent.com | 199.232.68.133 | 3600 |
效果验证
# 终端速度测试(全平台通用)
curl -o /dev/null https://github.com/ubuntu/ubuntu/archive/refs/heads/main.zip
# 预期下载速度
> 平均速度:2.5MB/s - 8MB/s
注意事项
- ⚠️ IP地址可能因运营商变化,建议配合自动更新方案
- 🔄 修改hosts后需执行 ipconfig /flushdns (Windows) 或 sudo killall -HUP mDNSResponder (MacOS)
- ⚠️ 权限问题:
# Windows必须使用管理员权限编辑文件
Start-Process notepad -ArgumentList "C:\Windows\System32\drivers\etc\hosts" -Verb RunAs
- 🔄 DNS缓存刷新:
# macOS
sudo dscacheutil -flushcache
# Linux
sudo systemctl restart nscd
📆 IP有效期:推荐配合自动更新方案使用
评论