Introduction:
When it comes to testing and developing websites, it is often necessary to view a website on a different server without modifying DNS settings. Luckily, both Mac and PC operating systems offer a convenient solution known as the hosts file. By making adjustments to this file, you can manually assign domain names to specific IP addresses, bypassing the need for DNS lookup. This article will guide you through the process of utilising the hosts file on Mac and PC systems, enabling smooth testing and development of websites.
Understanding the Hosts File:
The hosts file is a plain text file located on your computer that maps domain names to IP addresses. It acts as a local reference before contacting DNS servers when entering a domain name in your browser. By modifying the hosts file, you can redirect a domain to a different IP address, overriding the default DNS lookup behaviour.
Locating the Hosts File:
On Mac:
- Open the Terminal application.
- Access the hosts file by executing the following command:
sudo nano /etc/hosts. - Provide the necessary password when prompted.
- The hosts file will open in the Terminal text editor.
On PC (Windows):
- Open File Explorer and navigate to:
C:\Windows\System32\drivers\etc. - Look for a file named “hosts” (without a file extension).
- Right-click on the file and open it using a text editor such as Notepad or Notepad++.
Modifying the Hosts File:
- To add a new mapping, enter the desired IP address followed by the domain name you wish to map (e.g.,
127.0.0.1 example.com). - If you want to view the website on a different server, obtain the IP address associated with that server and insert it into the hosts file.
- Save the changes made to the hosts file (on Mac: Ctrl+O, Enter; on PC: Ctrl+S).
- Exit the text editor.
Clearing DNS Cache (Optional):
Occasionally, your computer may cache DNS entries, causing delays in recognising the changes made in the hosts file.
To clear the DNS cache:
- On Mac, launch Terminal and enter:
sudo killall -HUP mDNSResponder. - On PC, open Command Prompt and enter:
ipconfig /flushdns.
Testing and Verification:
- Open your web browser and enter the domain name you assigned in the hosts file.
- The website should now load from the server specified in the hosts file.
- To revert back to the original DNS settings, simply remove the added lines from the hosts file and save the changes.
Conclusion:
The hosts file serves as a powerful tool for web developers and testers, allowing them to view websites on different servers without altering DNS settings. By manually mapping domain names to IP addresses, testing and developing websites becomes a seamless process.
Whether you are using a Mac or PC, accessing and modifying the hosts file is straightforward. Remember to exercise caution when making changes to the hosts file and always keep a backup of the original file. Now you can easily test and develop your websites without worrying about DNS propagation delays or affecting the live site.








