Even though a website is functioning properly, it may not be accessible locally, or access speeds may be extremely slow, or the user may be redirected to an irrelevant page. A common cause of this phenomenon is DNS pollution. For users looking to quickly and directly resolve website access issues, modifying the Hosts file is an efficient and practical solution. Below, we'll explain in detail how to bypass DNS pollution by modifying the Hosts file, from the principles to the specific steps, including precautions, so even beginners can easily get started.
Why can modifying the Hosts file bypass DNS pollution?
To understand this, we first need to understand the domain name resolution process. Normally, when we enter a URL in a browser, the system queries the IP address corresponding to that domain name, typically relying on a DNS server. However, if the DNS result during this query is polluted, the user will receive an incorrect address.
The Hosts file acts as a "local dictionary." The operating system will first check the Hosts file. If the IP address corresponding to a domain name is already specified, it will use that IP address directly, bypassing external DNS queries. This means that by configuring the correct mappings in the Hosts file ahead of time, we can bypass external DNS resolution and avoid the impact of DNS contamination.
Preparation before modifying the Hosts file:
Before you begin, several prerequisites must be met: Finding the correct IP address is the most critical step. If the IP address you entered is incorrect, even if you modify the Hosts file, the website will still not open. Modifying the Hosts file requires administrator privileges. On Windows, you need to open an editor as an administrator, and on Linux or macOS, you need to use the sudo command. It is recommended to use the system's default Notepad (Windows) or nano/vim (Linux/macOS). Avoid using editors with formatting to prevent incorrect file saving.
The Hosts file path varies depending on your operating system. Once you know the path, navigate to the corresponding directory and prepare to edit it. Common locations are as follows:
Windows: C:\Windows\System32\drivers\etc\hosts
Linux: /etc/hosts
macOS: /etc/hosts
Steps to modify the Hosts file:
1. Windows system
(1) Run Notepad as an administrator.
Method: Enter "Notepad" in the search bar, right-click and select "Run as administrator".
(2) Open the Hosts file.
In Notepad, click "File" → "Open", enter the path C:\Windows\System32\drivers\etc\hosts, select "All Files" in the file type, and you will see the Hosts file.
(3) Add domain name and IP mapping.
Add similar content to the end of the file:
203.0.113.10 www.example.com
203.0.113.11 example.com
Note: There should be a space or tab between the IP and domain name, and only one set should be written on one line.
(4) Save and exit.
If a permission issue is prompted when saving, it means that the program is not running as an administrator and needs to be reopened.
(5) Flush the DNS cache.
In the command prompt (administrator mode), enter:
ipconfig /flushdns
Make sure the system uses the new Hosts rule immediately.
2. Linux System
(1) Open a terminal.
(2) Enter the following command:
sudo nano /etc/hosts
(3) Add the mapping at the end of the file:
203.0.113.10 www.example.com
203.0.113.11 example.com
(4) Press Ctrl+O to save, and Ctrl+X to exit.
(5) Flush the DNS cache.
The method varies depending on the distribution, for example:
sudo systemctl restart network-manager
or
sudo /etc/init.d/nscd restart
3. macOS System
(1) Open Terminal.
(2) Enter the following command:
sudo nano /etc/hosts
(3) Add domain name mapping:
203.0.113.10 www.example.com
(4) Save and exit.
(5) Clear the cache:
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
Notes on modifying the Hosts file:
Website server IPs are not fixed, especially for websites using CDN acceleration. If an IP suddenly becomes unavailable, you'll need to obtain a fresh IP. Many people share so-called "optimized Hosts" files online, but they may contain malicious entries that could redirect you to phishing websites. Ensure the IP source is reliable. Before modifying the Hosts file, it's recommended to make a backup copy of the Hosts file for quick recovery in case of problems.
The Hosts file takes precedence over DNS. A mistake in writing to it can cause certain websites to become permanently inaccessible. Therefore, exercise extreme caution when modifying the Hosts file. Modifying the Hosts file doesn't completely resolve DNS poisoning; it's only a temporary solution for specific websites. If you experience frequent DNS poisoning, it's best to use DoH.
Modifying the Hosts file to bypass DNS poisoning essentially leverages the system's priority mechanism to pre-fix the domain name and IP address mapping, preventing tampering with external resolution. Its advantages are simplicity, directness, and effectiveness, making it particularly suitable for resolving inaccessible issues for a single or small number of websites. Its disadvantages are that it requires manual IP updates and isn't suitable for large-scale poisoning or dynamic IP address scenarios. Therefore, modifying the Hosts file is more of a workaround than a long-term solution. If you only need to access the contaminated website occasionally, this method is very practical; if you encounter DNS issues on a long-term basis, it is best to combine public DNS, DoH/DoT, and other methods to solve the problem.
CN
EN