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.
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.
Here is a solution for
- Right-click on the Windows icon in the Start menu.
- Select Windows PowerShell (Admin).
notepad.exeand 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:
Ctrl+Sto 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:
sudo systemctl restart nscd
sudo killall -HUP mDNSResponder
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.