How to Edit the Hosts File in Mac OS X with Terminal

Aug 7, 2012 - 36 Comments

Need to edit or modify the hosts file on a Mac? You’ll find hosts in Mac OS X is stored at /private/etc/hosts but it can also be accessed at the more traditional location of /etc/hosts. That said, if you’re looking to edit hosts, you’ll want to target the file located in /private/etc/ though. We’ll walk through how to manually edit the hosts file in OS X Lion, OS X Mountain Lion, and OS X Mavericks, this will be done with the command line using the simple text editor called nano. Don’t let the command line or Terminal sound intimidating though because it’s not, we’ll make the entire process super easy.

Edit the Hosts file in Mac OS X using Terminal

Let’s get started making some edits to /etc/hosts!

  1. Launch Terminal, found in /Applications/Utilities/ or launched through Spotlight
  2. Type the following command at the prompt:
  3. sudo nano /private/etc/hosts

  4. Enter the administrator password when requested, you will not see it typed on screen as usual with the command line
  5. Once the hosts file is loaded within nano, use the arrow keys to navigate to the bottom of the hosts file to make your modifications
  6. When finished, hit Control+O followed by ENTER/RETURN to save changes to /private/etc/hosts, then hit Control+X to exit out of nano
  7. Quit out of Terminal when finished

You can verify your hosts modifications immediately with ping, Safari, or any other network app. Changes take effect immediately though some adjustments may need to be accompanied by a DNS flush which can be done with the following command in OS X 10.9:

dscacheutil -flushcache;sudo killall -HUP mDNSResponder

When flushing DNS cache with that command you will need to enter the admin password.

If you’d like to see how this entire process is accomplished before doing it yourself, watch the video below to see a demonstration of the hosts file being modified on OS X to block the website ‘yahoo.com’ from loading:

Note: the procedure is the same with older versions of Mac OS X, though the path to hosts could be /etc/hosts if the version of OS X is dated significantly.

Tips to Consider When Editing Hosts Files

The following tips go beyond OS X and apply to any hosts file, be it on a Mac, Windows, or Linux.

  • The preceding IP address is where the following domain will resolve to
  • Always add new hosts to their own unique line
  • The # symbol functions as a comment, it can be used to add comments to hosts entries or to comment out hosts modifications
  • You can block websites through hosts by adding them to the file and sending them nowhere, preventing access
  • You can redirect websites locally using the same logic, perfect for setting up test domains
  • With some modifications, it can be necessary to flush DNS cache with dscacheutil before the changes take effect
  • For juggling multiple hosts files consider using a manager app like GasMask
  • If the hosts file claims to be locked, it’s because you did not prefix the edit with the “sudo” command
  • Consider making a backup of hosts if you plan on making significant modifications, or it’s your first time editing the file (process described below)

Making a backup of the hosts file can be a good idea if you plan on making significant changes or just want to play around with modifications and see what happens, a simple way to do that would be to use this command, which would store a backup in your home ~/Documents/ folder:

sudo cp /private/etc/hosts ~/Documents/hosts-backup

Then, if you wanted to restore the modified hosts to the backup of the original file, you just have to swap the paths like so and rename the file again:

sudo cp ~/Documents/hosts-backup /private/etc/hosts

That’s it, though again you may need to flush the DNS for changes to take effect.

Finally, it’s worth mentioning that if you’d prefer to avoid the Terminal and the command line completely, you could try the easier method of using a preference pane to modify the contents of hosts that way through System Preferences instead. Generally speaking though, we recommend just using the tools that are built directly into the Mac.

Let us know in the comments if you have any questions or tips.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS X, Tips & Tricks

36 Comments

» Comments RSS Feed

  1. @fukr says:

    sudo vi /private/etc/hosts

    • Ben says:

      I can’t remember how to do anything in vi and I’m probably not alone, I think unless you’re a frequent user vi/vim are too easy to forget.

      But, to make it super easy, you can do this:

      sudo open /private/etc/hosts

      That takes it into Text Edit

      • @fukr says:

        Agreed that vi/vim are probably not something for the feint hearted. As mention in another comment, using Text Edit is not a good option for unix files. Best to stick to nano instead :D

      • Wiggums says:

        Once you learn vi, you will be glad you did.
        You can do things like search and replace based on regular expressions, sort lines, convert case, indent etc.

  2. Travis says:

    I have tried this in the past using but using large hosts files like from http://winhelp2002.mvps.org/hosts.htm caused my processor to run hot constantly when the internet is connected. Is it still like that?

  3. Napoleon Wils0n says:

    You should never ever use Text Edit to edit unix based files.

    If you want a free editor use TextWrangler, if you download TextWrangler from the Mac App Store you need to install the command line tools separately.

    http://www.barebones.com/support/textwrangler/

    There are 2 ways to edit system files with TextWrangler.

    Open the file from the Finder with TextWrangler and then enter you admin password, or use the sudo command with the TextWrangler command line tool to open the file.

  4. Aberto I says:

    You can use Gas Mask too
    http://code.google.com/p/gmask/

  5. Petr says:

    What is a difference between /etc/hosts and /private/etc/hosts?

    • ebut says:

      /private/etc/hosts is the source, /etc/hosts is hard linked to it. If you change one, both change. The proper one to edit is /private/ though.

      • woodgie says:

        It’s even simpler than that. There is only one host’s file and that’s in /private/etc/, /etc is a symlink (not iirc, a hard link) to /private/etc/. A symlink is, to all intents and purposes, the same as a Finder alias.

        Therefore if you go to /etc/hosts (via the symlink) you’ll end up at the exact same file as if you got there via /private/etc/hosts (the ‘real’ path). It’s the same as if you make a Finder alias on your Desktop of your Documents folder and double click on it. That’ll take you to the same place in your file system as going directly to the Documents folder.

        ~W

  6. Wiggums says:

    What I need is a list of known attack sites to add to my hosts file. I’ve seen a ton of them in my logs but I’ve never bothered to collect their domains.

  7. Napoleon Wils0n says:

    Heres my set up

    hosts file blocking

    http://someonewhocares.org/hosts/

    You can also use the hosts file to rickroll people, just add the youtube iframe code to the index.html in /Library/WebServer/Documents

    then add an entry to the /etc/hosts file

    127.0.0.1 somedomain.com

    I used the rickroll hosts trick when setting up a laptop for my mate who didnt want his kids accessing things they shouldnt, by adding a list of unsuitable domains to the hosts file.

    So whenever they try to look at naughty sites they get rickrolled.

    Then install icefloor the gui frontend for the pf firewall in mountain lion

    icefloor is made by Hany El Imam who also made the excellent noobproof and waterroof for previous versions of osx

    http://www.hanynet.com/icefloor/

    Then set up the emerging threats list with the pf firewall

    http://ikawnoclast.com/2012/04/using-the-lion-pf-firewall-with-the-emerging-threats-list.html

    I have been talking to Hany the developer of icefloor and he should be adding the emerging threats list to the next version.

  8. delix says:

    Something is causing my hosts file to be reset on each reboot mt. lion on macbook pro 7,1

    this is not happening on imac running same software…

    anyone experiencing this?

  9. Phillip says:

    Is there anyway to use the hosts file to block facebook in Mountain Lion? I’ve added the entry to the hosts file, and trying to ping http://www.facebook.com shows that it won’t connect, however if I type http://www.facebook.com into Safari, it will still manage to go to facebook’s website. Does this have something to do with the facebook integration in Mountain Lion?

  10. Bill says:

    HUGE MISTAKE in these instructions.

    The changes will not save with:

    When finished, hit Control+O to save changes to /private/etc/hosts, then hit Control+X to exit out of nano

    DO THIS:

    When finished, hit Control+O, THEN CLICK ENTER to save changes to /private/etc/hosts, then hit Control+X to exit out of nano

    • Eric says:

      Thanks! I was wary of doing anything wrong and messing things up beyond my own (very) limited abilities, and I would have spent a while googling more and trying to figure it out.

  11. Eddie says:

    Hi I got a question, I just got my MacBook and I’m running mountain lion. I’ve tried editing my host file but when I open the window, there is nothing in there. I’m seeing everyone with default settings that you’re not supposed to mess with but I don’t have them in there. I have nothing! Is that normal?

  12. Charly says:

    Funciono perfecto, Gracias!!

  13. Chris says:

    I messed around with terminal to unblock facebook in Vietnam and now I can’t re-access it in the States. What do I need to do? Can someone help?

  14. van says:

    why whenever i save my or hit command+o it looks for the hosts file…

  15. van says:

    can someone help me. whenever i save it, it looks for the host file…

  16. mrfrish says:

    the commands have to be :

    1. sudo nano /private/etc/hosts
    1.1. put your changes
    1.2. CTRL+O (press ENTER in order to save)
    1.3. CTRL+X (to exit nano)

    2. sudo killall -HUP mDNSResponder

    3. dscacheutil -flushcache

    enjoy

  17. pam says:

    My mac shows “access denied” even though I am the administrator. It suggests that I find the file and unblock it but I dont know how…

  18. a b says:

    Mountain lion pretty much ignores /etc/hosts
    it uses directory services now, and you need to learn to use dscl in terminal in order to tame it.

  19. mrpink says:

    thank you so much!!!:)

  20. M says:

    Very Helpful! thanks!

  21. Harakiri says:

    Is it possible to block websites with just the ip address if the blocked site instead of the name also?

    And can I block communication of a program from accessing the internet this way?

    If not please tell me how.

    • ds says:

      Yes, placing an IP in the hosts file with another domain associated with it will prevent that IP from being accessed, or use the loopback address (127.0.0.1)

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates