Solution for Unable to Load Images on GitHub

Yesterday, when I tried to add images on GitHub, I noticed that the images weren't loading properly. Upon investigation, I found out that GitHub's IP addresses were once again blocked by the Great Firewall (GFW). I resolved this issue by modifying my Hosts file. It's worth noting that this method can also be used to address problems with accessing GitHub when it's entirely blocked.

This post was translated from my Chinese blog post with the aid of ChatGpt.

Principle

When we access a website through its URL, the website’s domain name needs to be resolved to an IP address through DNS resolution. One of the reasons why GitHub may not be accessible is due to the Great Firewall’s (GFW) DNS poisoning, which can cause DNS servers to return incorrect IP addresses. As a result, the website cannot be connected to, and resources such as images fail to load. Another reason is direct IP blocking, where the GFW intercepts data packets with specific IP addresses, preventing data transmission.

In the early days of the internet when it was smaller in scale and DNS did not exist, domain names were mapped to IP addresses using the Hosts file. As the internet rapidly expanded, relying solely on the Hosts file became insufficient, and that’s when DNS servers came into play.

Although DNS took over much of what the Hosts file used to do, the Hosts file still maintains a higher priority. Before a computer requests DNS resolution, it first checks if there is a mapping between the domain name to be accessed and an IP address in the local Hosts file. If such a mapping exists, the computer doesn’t need to use DNS servers; it can directly access the IP address associated with the domain name. This makes it possible to add Hosts file mapping records to bypass GFW’s DNS poisoning.

Method

Here is a solution for Windows systems:

  • Right-click on the Windows icon in the Start menu.
  • Select Windows PowerShell (Admin).
  • Enter notepad.exe and press Enter to open Notepad.
    • The reason for doing this is that modifying the Hosts file requires administrator privileges, and using Notepad directly won’t allow you to save the changes.
  • In the Notepad menu, click on File, then Open.
  • Navigate to the Hosts file in the path C:\Windows\System32\drivers\etc and open it (for Ubuntu and MacOS, the file path is generally /etc/hosts; make a copy, replace it, and paste it back after making changes).
  • Add the following mappings at the bottom of the file:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    # Github
    192.30.253.112 github.com
    140.82.113.19 gist.github.com
    185.199.108.153 assets-cdn.github.com
    199.232.68.133 raw.githubusercontent.com
    199.232.68.133 gist.githubusercontent.com
    199.232.28.133 cloud.githubusercontent.com
    199.232.28.133 camo.githubusercontent.com
    199.232.68.133 avatars0.githubusercontent.com
    199.232.68.133 avatars1.githubusercontent.com
    199.232.68.133 avatars2.githubusercontent.com
    199.232.68.133 avatars3.githubusercontent.com
    199.232.28.133 avatars4.githubusercontent.com
    199.232.28.133 avatars5.githubusercontent.com
    199.232.68.133 avatars6.githubusercontent.com
    199.232.68.133 avatars7.githubusercontent.com
    199.232.28.133 avatars8.githubusercontent.com
  • Press Ctrl+S to save the file, then refresh the webpage to access it normally.
  • If you still can’t access the website, you need to clear the DNS cache and restart your browser:
    • Windows: ipconfig /flushdns
    • Ubuntu: sudo systemctl restart nscd
    • MacOS: sudo killall -HUP mDNSResponder

Updating Hosts

Because the GFW continually works to block access to these IP addresses, these IP addresses may be blocked again after some time. Therefore, you need to periodically update your Hosts file to access websites that have been blocked.

  • Open ipaddress.
  • Enter each relevant domain name from the Hosts file one by one.
  • Replace the old IP address with the new IP address returned by the website.
  • Save the changes.