What is a hosts file and how can I use it to test my website?
Most common operating systems include a hosts file, which is used to translate a human friendly host name into an IP address used by the web browser to connect to the hosting server.
This process forms part of your operating systems DNS lookup in order to establish which server to connect to for your requested host name specified in your URL, i.e. /, will connect to the server hosting xneelo.co.za.
Changing the contents of this file allows you to specify which IP address your DNS lookup should resolve the host name to thereby allowing you to test your website on a new server without updating the domains DNS records. This will however only affect the computer that the hosts file is changed on.
Note: This change should only be made temporarily for testing purposes. The added entry must always be deleted from the hosts file after testing has been concluded.
To change the hosts file on a Windows 7, XP and Vista operating system, follow the steps below:
- Click Start
- In the search field, search for “Notepad“
- Right-click on the result for “Notepad”
- Select “Run as Administrator“
- Navigate the hosts file, which is usually in the location: C:WindowsSystem32driversetchosts
- Append your entry to the bottom of the file as shown below in the following format: e.g. MyDomain.co.za MyIPaddress
MyDomain.co.za 197.221.22.12
- Save the file by opening the “File menu” and select “Save”
Note: Changes to this file will require administrative privileges.
To change the hosts file on most common Linux distributions, follow the steps below:
- Open a terminal session
- Open the hosts file usually located in the path, /etc/hosts in a text editor of your choice, i.e sudo nano /etc/hosts
- Append the required line on a new line at the bottom of the file as shown below in the following format: MyDomain.co.za MyIPaddress
MyDomain.co.za 197.221.22.12
- Save the file and exit the editor
Note: Changes to this file will require root permissions
To change the hosts file on most Mac versions, follow the steps below:
- Open a terminal session
- Open the hosts file usually located in the path, /private/etc/hosts in a text editor of your choice, i.e sudo nano /private/etc/hosts
- Append the required line on a new line at the bottom of the file as shown below in the following format: MyDomain.co.za MyIPaddress
MyDomain.co.za 197.221.22.12
- Save the file and exit the editor
Note: Changes to this file will require root permissions
After the changes made to your hosts file have been saved, you may need to clear your operating system’s cache.
The website can now be tested as you normally would by browsing to the domain’s URL. The website displayed will be the one located on the server with the IP address you specified in your hosts file.
Note: Always ensure that you delete the lines you have added to the hosts file.