What is a Hosts File ?
The hosts file is a “plain-text” file which enables an Operating System to map hostnames to IP addresses.
So, when we type a Website URL in the address bar of the browser such as Firefox or Internet Explorer, the operating system first checks locally if there is any such entry in the hosts file relevant to that URL. If there is one such entry, the OS gets the corresponding IP Address of that URL.
If no such value is present, the system uses the Global Domain Name System(DNS) servers to resolve that Address to a real IP Address.
Use of Hosts File ?
In addition to resolve host names, the hosts file may is also useful in the following two cases:
- Redirection:
A very practical application of Hosts File is in “redirecting” a host name to a different IP Address. This is always needed to test new web applications or websites which are under the development stage.
- Blocking Host Names:
The other important usage of Hosts file is to block certain malicious domain names or websties. This is achieved by adding new entries to “Hosts” file. These bad hosts mighth contain spyware & other programs which may cause harmful to our computer.
Editing the hosts file on MAC Operating System X
We can edit the Hosts file on a Macintosh Operating System with the help of a application called as Terminal. If you have used this applications in the past, your job is half done !!
Let us have a look at the steps one by one:
1) Launching the Terminal application
You can open the Terminal app in MAC either by entering text “Terminal” in the Spotlight Window
or via launching the application from Applicaions > Utilities > Terminal
2) Opening the Hosts File
Now you need to open the Hosts File via Terminal application(shown in the screenshot below).
For this we require two things – Location of Hosts file & the command to access the same. We will use the following command to achieve the same:
$ sudo nano /private/etc/hosts
You will be required to enter your password when you are prompted for that.
3) Editing the hosts file
A common hosts file could includes the following:
- Comments (Beginning with the # symbol)
- Hostname mappings
You need to append your new mappings beneath the existing mappings of Host File.
4) Saving the hosts file
Once you have completed changing the hosts file using Terminal application, you need to push Ctrl-o to save the file & Ctrl-x to exit out of the file
5) Flushing the Cache using “dscacheutil”
After you are done with the above given steps, you need to clear your DNS Cache. We have a command in MAC OS to achieve this.
$ dscacheutil -flushcache
6) Exiting the Terminal Application
Exit the Terminal Application once you are done.
Now you should be able to test this new mapping. Enter the Domain Name in the Address bar and see it being resolved it to the new IP Address that you specified in the Hosts File.




[…] article is about the Terminal command nano which is sometimes required in editingĀ text files like changing a Hosts File in Macintosh Operating […]
Many thanks for the tip. I needed to view a website hosted on a server which currently has no domain name associated with it. Your post was very informative.
Cheers
Thanks