How to Temporarily Test Website Accessibility Using the Hosts File?

19-01-2024 05:13:39

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.