通过修改host访问github.com

GitHub 并没有官方提供固定 IP 地址的文档,因为 GitHub 的 IP 地址和端口可能会随时更改。使用固定的 IP 地址来访问 GitHub 并不是一个推荐的方法。通常,建议使用 DNS 解析和代理服务来确保能够稳定地访问 GitHub。

然而,GitHub 提供了一些用于防火墙配置的 IP 地址范围文档,这些文档列出了 GitHub 服务使用的 IP 地址范围,可以用于网络配置:

GitHub 服务 IP 范围

GitHub 官方提供了一个 JSON 格式的 IP 地址范围文档,可以通过以下 URL 获取:

你可以通过浏览器访问该链接,或者使用 curl 命令获取该文档:

curl https://api.github.com/meta

示例输出:

{
  "verifiable_password_authentication": true,
  "github_services_sha": "hash",
  "hooks": [
    "192.30.252.0/22",
    "185.199.108.0/22"
  ],
  "git": [
    "192.30.252.0/22",
    "185.199.108.0/22"
  ],
  "pages": [
    "192.30.252.153",
    "185.199.108.153",
    "185.199.109.153",
    "185.199.110.153",
    "185.199.111.153"
  ],
  "importer": [
    "192.30.252.0/22",
    "185.199.108.0/22"
  ],
  "actions": [
    "185.199.108.0/22"
  ],
  "dependabot": [
    "185.199.108.0/22"
  ]
}

网络配置示例

以下是一个网络配置示例,展示如何将 GitHub 的 IP 地址范围添加到防火墙的白名单中:

配置防火墙

假设你使用的是 iptables,可以添加以下规则:

iptables -A OUTPUT -p tcp -d 192.30.252.0/22 --dport 443 -j ACCEPT
iptables -A OUTPUT -p tcp -d 185.199.108.0/22 --dport 443 -j ACCEPT

对于 firewalld

firewall-cmd --permanent --add-rich-rule='rule family="ipv4" destination address="192.30.252.0/22" port port="443" protocol="tcp" accept'
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" destination address="185.199.108.0/22" port port="443" protocol="tcp" accept'
firewall-cmd --reload

结论

虽然修改 hosts 文件和配置防火墙可以帮助临时解决网络访问问题,但为了长期稳定地访问 GitHub,建议使用代理服务器或 VPN,并确保你的 DNS 设置是正确和可靠的。

0 0 投票数
Article Rating
订阅评论
提醒
guest
1 评论
最旧
最新 最多投票
内联反馈
查看所有评论
匿名
匿名
2 月 前

可以通过修改/etc/hosts文件可解决该问题。
具体而言:
打开 https://www.ipaddress.com/ 输入访问不了的域名,获得对应的IP。
使用vim /etc/hosts命令打开不能访问的机器的hosts文件,添加如下内容:
199.232.68.133 raw.githubusercontent.com
199.232.68.133 user-images.githubusercontent.com
199.232.68.133 avatars2.githubusercontent.com
199.232.68.133 avatars1.githubusercontent.com
1.
2.
3.
4.
注:上面内容中199.232.68.133是raw.githubusercontent.com所在的服务器IP(通过 https://www.ipaddress.com/ 获知)。
保存该文件,再使用即可正常访问。

1
0
希望看到您的想法,请您发表评论x