While I was creating a staging site in a new account where its domain nameservers changes were not updated yet and but had to develop on a WordPress site. That required me to use the “hosts” file.
What is “hosts” File?
According to this documentation, “The hosts file is used to map hostnames (in other words domains) to IP addresses. With the hosts file, you can change the IP address that you resolve a given domain name to. This change only affects your own computer without affecting how the domain is resolved worldwide.”
Some properties of the hosts file include:
- doesn’t have an extension, its just called hosts.
- is a text file that maps hostnames to IP addresses.
- is located at
/etc/, which is a symbolic link to
- is checked before the active network connection’s DNS servers.
Importance of Hosts File
The “hosts” file is important & useful because if we “wish to see how a website will look like when hosted on a different server without making any DNS changes to your domain“.
Location of File on MacOS
The location of the “hosts” file varies depending on machine and its operating system. On MacOS ‘catalina’ for example it’s location is:
Mac OSx – /private/etc/hosts
An standard hosts file looks like below:
## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost fe80::1%lo0 localhost
Use Case Example
One of the prime use case of “host” file is “to resolve the domain name (eg. mysite.com) to the IP address (eg, 18.104.22.168).” How this can be resolved? To resolve this we need to edit the “host” file append the following line of code :
22.214.171.124 mysite.com www.mysite.com
The above code instructs the computer to resolve mysite.com to 126.96.36.199. “Once you do that you may need to clear your web browser’s cache, afterward, if you try to reach your domain http://mysite.com in a browser it should take you to the site hosted on the server with IP 188.8.131.52.”
How to Edit the hosts File
It appears that editing this file is little tricky because its locked file. I found the following PBX Cookbook step-wise-guide was useful in editing the “hosts” file MacOs ‘Catalina‘. The following steps taken directly from the guide :
Step 1: Open the Terminal App
Navigate the terminal app: Application > Utlities > Terminal
Step 2: Open the hosts File with nano Editor
Add the following code on the terminal window and add password if promted:
#! file edit with nano $ sudo nano /etc/hosts
Step 3: Make edit the “host” file
The hosts file contains some comments (lines starting with the # symbol), as well as some default hostname mappings (one per line), entered as IP address followed by one or more hostnames (e.g. 127.0.0.1 localhost).
Append your new mappings underneath the existing ones. Navigate using the arrow keys, leave space between fields, and make sure there is an empty blank line after the last entry.
## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost fe80::1%lo0 localhost 184.108.40.206 mysite.com www.mysite.com
Sep 4: Save the “host” file
- When done editing the hosts file, press Ctrl-o to save the file.
- Press enter on the filename prompt to leave it unchanged, and Ctrl-x to exit the editor.
The new mapping can be tested by entering the URL in a browser window.
Step 4: Clear the DNS Cache
If your changes don’t seem to have taken effect, use one of the following commands to flush the DNS cache.
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
For older OS X versions, refer to: https://support.apple.com/en-ca/HT202516
Now check the URL in your browser again, it should work!