The hosts file is a special file within an operating system that stores IP addresses and domain name information. Before the system resolves a domain name through DNS, it first accesses the hosts file. Therefore, by modifying the hosts file, we can change the IP direction of a domain name. Utilizing this feature of the hosts file can assist us in testing whether a website is functioning properly before its domain name officially takes effect.
The syntax of the hosts file is straightforward, with each record consisting of an IP address and a domain name, separated by a space. The hosts file typically includes some default records, such as localhost, which are generally not recommended to be modified.
1.2.3.4 example.com
1.2.3.4 www.example.com
Windows Operating System
In Windows operating systems, the path to the hosts file is: C:\Windows\System32\Drivers\etc\hosts.
To modify the hosts file, simply open it with Notepad and make changes directly. In some systems, the hosts file is read-only, so it's necessary to uncheck the read-only status before making modifications.
Linux Operating System
In Linux operating systems, the command line to modify the hosts file is:
vi /etc/hosts
Local Testing
We can use the ping command to check if the hosts file has been successfully modified.
ping example.com
If the IP address that appears matches the one in the hosts file, it indicates that the hosts file has been successfully modified.
23-02-2024 02:02:07
22-02-2024 03:19:32
22-02-2024 03:16:03
22-02-2024 03:14:03
22-02-2024 03:11:58