In computers, the hosts file is used to map IP addresses to domain names. This is an important part of any computer as it lets your manually add or delete domain mapping. Obviously, this file is usually used by the system administrators, power users or developers so that they can map particular domains to a particular IP address. If you are using Windows, editing hosts file is as easy as editing any text file. But on Mac, it's a little bit tricky.
In fact, if you try to open and edit the hosts file, you will see the text "Locked" on the title bar. Since the file is locked, you are blocked from making any changes.
So, if you ever want to edit hosts file on Mac, here is how to do it.
Edit Hosts File On Mac OS X
Though editing hosts file on Mac OS X is a bit tricky, it is still easy. To start off, search for the terminal in Launchpad or Spotlight and open it.
Once the terminal has been opened, copy the below command and execute it.
sudo nano /etc/hosts
As soon as you execute the command, the terminal will open the hosts file in the nano editor. Here you can edit the file to add or remove entries.
To add an entry, use your keyboard to move the text cursor and you can use the below format to add your own entries. For instance, I'm mapping example.com to the localhost IP address 127.0.0.1.
#Your custom comment 127.0.0.1 example.com
As you can see the below image, I've also added comments before the entry. To put it simply, anything after the
# symbol is treated as a comment, thus ignored.
Once you are done added or removing the entries from the hosts file, press the keyboard shortcut
Control + O to save the changes. The editor will ask for location and name, just leave the default as is and press the Enter button.
Once the changes are saved, you will receive a confirmation message telling you how many new lines are added.
To close the editor, press the keyboard shortcut
Control + X and you are good to go.
Generally, the changes are instantly reflected. But if your mac is taking longer than expected, then use the below command to flush the DNS cache.
sudo killall -HUP mDNSResponder
That's all there is to do and it is that simple to edit hosts file on Mac OS X.
Do comment below sharing your thoughts and experiences about using the above method to edit hosts file on Mac OS X.