How to Edit the Hosts File on Mac

In many cases, we need to know whats going on with internet on our Mac. Well, we can take a look at DNS (Domain Name System) and associated cache file. There is another way to get this task done, using Hosts file. Hosts file is always hidden on Mac's /etc folder. It carries hostnames with certain IP address. Our OS and browsers come and check this file for desired hostname with IP address. In particularly, once we write URL on browser's address bar, OS gets in this file for corresponding mapping before going to the DNS server to look for IP address. By the way, hosts file can be used to override the default DNS information.

     Most of the Mac users don't need to worry or touch the Hosts file. Its always ready to get done what it meant to do. However, we need to touch this file in certain cases. We know, it can redirect a domain. Suppose, we are testing a developing server and we don't want others to access it using DNS. We can use Hosts file to make a domain redirect to local address on our machine, we will be able to test our code offline before it becomes online for users.

     Another reason that makes Hosts file handy is to block unwanted and suspicious sites, spywares, ad networks or even download viruses. We need to zero out ( their IP addresses. They wouldn't be able to get into our network.

     To get to the Hosts file, we need to open the Nano Text Editor using Mac Terminal. Terminal is under Application and side the Utilities folder. Once opened, enter the command line mentioned below and hit return/enter.
     sudo nano /etc/hosts
You will see this window below, enter your Administrative Password and hit return.

We are on nano Text Editor which will look like the figure below and ready to edit the hosts file.

     Use the Arrow Keys to navigate the commands from the bottom of the screen looks like the figure below. Use these commands as what your requirements are.

     To map particular IP addresses on local network to a domain, type IP and hit Tab to enter domain. To prevent Mac to go to certain sites, use IP "127.0.01". This IP address is the default to the local machine in Hosts file. It maps back to the Mac even though router assigns different IP for the Mac.

     Once you are done with your task, use CONTROL + O to save the Hosts file and CONTROL + X to exit to the Terminal prompt. On command line type command line mentioned below and hit return. It will flush Mac's DNS cache, so that it doesn't get confused by your changes. Always remenber or make a note somewhare that you have changed Hosts file. At some point, you may need to revert your changes to keep your machine working properly.
     sudo killall -HUP mDNSResponder
     For advanced users, editing Hosts file is useful in many cases. However, we need to keep in mind that we should not attempt to edit Hosts file unless we are comfortable to work with necessary tools and have clear reasons to do so.

No comments:

Post a Comment

Grab The Button...

Grab the above button and add to your blog or website to get easy access to Virtual Species. Simply click the textbox, copy the code and paste where you want to see the button. That's it!