Knowledge Essentials - 3Essentials Hosting

How To: Edit HOSTS file on Mac OS X

Article ID: 1313

 Back to Search

Information

The hosts file is a text file that maps hostnames to IP addresses.
Upon typing a url address on the browser, the system is checking if there is a relevant entry on the hosts file and gets the corresponding IP address, else it resolves the IP via the active connection’s DNS servers.

The hosts file can be edited to block certain hostnames (like ad-serving/malicious hosts), or used for web development purposes, i.e. to redirect domains to local addresses.

Step 1 – Open the Terminal.app

Either by start typing Terminal on the Spotlight, or by going into Applications -> Utilities -> Terminal.

Step 2 – Open the hosts file

Open the hosts by typing on the Terminal that you have just opened:

sudo nano /private/etc/hosts

Step 3 – Edit the hosts file

The hosts file contains some comments (lines starting with the # symbol), as well as some default hostname mappings (e.g. 127.0.0.1 – localhost).
Simply append your new mappings underneath the default ones. Or edit one of the default values if you know what you are doing!
You can navigate the file using the arrow keys.

Example HOSTS File entry:

Syntax: HOST IP DomainName DomainName DomainName ....

Each subdomain like mail.Mydomain.com, something.mydomain.com has to be entered seperately to the HOSTS file.

72.249.5.10 mydomain.com www.mydomain.com

The above entry will force your PC's DNS for MyDomain.com to 72.249.5.10 instead of the live site.

Once you don't need the entry, you can either comment it out or delete it.  Commented out entry will look like this:

#72.249.5.10  mydomain.com www.mydomain.com

# sign comments out an entry in the HOSTS file.

Step 4 – Save the hosts file

When done editing the hosts file, press control-o to save the file.
Press enter on the filename prompt, and control-x to exit the editor.

Step 5 – Flush the DNS cache

On Leopard you can issue a simple Terminal command to flush the DNS cache, and have your host file changes to take immediate effect:

dscacheutil -flushcache

All Steps Complete: You can now test your new mapping on the browser!  You may need to restart your browser in case your browser uses DNS pre-fetching.

 

 
Downloads Associated With This Article
No downloads are currently associated with this article.