How to Edit the Hosts File in OS X

Every Mac stores a hosts file in the hidden /etc folder, which is used by the operating system and your web browser to map particular hostnames to certain IP addresses. When you enter a website URL address into the Address Bar of Safari, for example, OS X will first check for a corresponding mapping in the hosts file, before moving on to the DNS server to find the required IP address.

For the vast majority of Mac OS X users, the hosts file is something they will never be required to touch. However, for more advanced users, the hosts file can have two particular uses.

Firstly, if you are involved in web programming, using the hosts file can allow you to redirect a domain to a local address on your Mac, where you can test out code “offline” before applying it to the actual website.

The second instance where you may want to edit the hosts file is because it is a highly effective means of blocking known suspicious sites that may attempt to deliver unwanted advertising to disrupt your surfing experience, or even download viruses or other malware to your computer.

Editing your hosts file incorrectly can have adverse effects on your Mac. You should only attempt to edit the hosts file if you have a clear purpose for doing so and are confident in your understanding of Terminal commands.

Editing the Hosts File in Mac OS X

1.) To begin editing the hosts file, you will need to use a Terminal window. Open one either by clicking on Applications, then Utilities and then Terminal, or by pressing Cmd-Spacebar to open Spotlight, typing Terminal and pressing Return. The Terminal window should look like this:

OS X Terminal Window.png

2.) We now need to open the hosts file for editing in the Terminal. We do this be entering the following command (or Copy and Paste from below) at the Terminal prompt:

sudo nano /private/etc/hosts

Press Return. Terminal will ask you to enter your Administrator Password:

Entering the Open Hosts File Code at the Terminal Prompt.png

Do so and again press Return.

3.) The hosts file will open in the Terminal window for editing:

The Mac OS X Hosts File.png

To navigate the hosts file, use your arrow keys to move up and down. The lines beginning with the hash (#) symbol are comments and should not be edited. There will also be some default mappings, such as the entry 127.0.0.1 for localhost.

4.) Use the ↓ Arrow Key to move the cursor to a blank space at the bottom of the hosts file, and enter your new mapping here. In the following example, we are adding an entry to prevent the Doubleclick Advertising server from sending files to our web browser:

Adding a Host Name to the Mac OS X Hosts File.png

5.) To save the hosts file after editing, press Ctrl-o. This will bring up a prompt for the file name to write to:

Prompt to Save after Editing the Mac OS X Hosts File.png

Press Return to save the hosts file. You can then exit editing the hosts file by pressing Ctrl-x. This will return you to the Terminal prompt:

The Mac OS X Terminal Window after Editing the Hosts File.png

6.) For the changes to your hosts file to take effect, you must first flush your DNS cache. To do so, enter the following command (or Copy and Paste from here) at the Terminal prompt:

dscacheutil -flushcache

Press Return. You can now exit the Terminal and your new host mappings will be in effect.

For more information on using the hosts file to prevent adware, spyware and other malicious software, we recommend visiting Blocking Unwanted Parasites with a Hosts File. The site tracks all reported problematic host names, and prepares a list of problem sites that is updated every few weeks.

The site is primarily Windows-focused, but the problem sites listed in their hosts .txt file can be used in the OS X hosts file by replacing each instance of “127.0.0.1” with “0.0.0.0”. Please note this does not include the “127.0.0.1 local host” entry mentioned above, which should never be altered.

For advanced users, editing the hosts file can be very useful in certain situations. However, we once again emphasize that you should not attempt to do so unless you are confident in using the Terminal and have a clear reason to edit the OS X hosts file.

More posts from the OS X Category

Overview: Reminders for OS X Mountain Lion

OS-X-Hidden-Files.png

How to Show Hidden Files in OS X

Internet Slow After Upgrading to Lion?

Open-Preview.png

Shrinking a PDF File

Comments [12]

  1. David Bunker says:

    This is the best writeup I've seen on editing the hosts file! Thank you!

  2. Jared says:

    Hey there,

    thanks SO much for this. This is the first tutorial I've seen about editing the host file on a mac that's crystal clear. I don't have a lot of experience using terminal to update documents, so it was nice to see it laid out step by step. Cheers!

  3. Milton says:

    When I have edited and command-o I can not press enter… what am I suppossed to open?

  4. Kyle says:

    WoW!!! thanks SO much !

  5. John says:

    The only website that explains this well! Thanks!

  6. Bronan says:

    Thanks for the tutorial. Very useful.

  7. Sher says:

    It doesn’t get any easier than pictures and cut ‘n paste. Awesome! Thank you!

  8. Jeff says:

    Thanks man!! Saved me a lot of frustration… thanks again!

  9. Emily says:

    Thank you so much! These were fantastic instructions!!! :) Sincerely, thank you!

  10. jaden says:

    the absolute best host editing explanation! thank you!!

  11. Lumpy Gravy says:

    If you want to avoid the hassle of a text-based editor, just copy and paste this into Terminal:

    sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts

Leave a Reply