How to edit the hosts file in Mac OS X 10.8 – Mountain Lion

The hosts file is a text file that maps hostnames to IP addresses. Upon typing a url address in the browser, the system first checks if there is a relevant entry in the hosts file and if exists gets the corresponding IP address. If no entries exists it resolves the IP via the active connection’s DNS servers.

The hosts file can be edited to block certain hostsnames, 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 start typing Terminal in the Spotlight search, or goto Applications > Utilities > Terminal.

Step 2 – Open the host file

Open the hosts file by typing the following in the Terminal window:

$ sudo nano /private/etc/hosts

Type your user password when prompted.

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 – local host). Simply append your new mappings underneath the default ones.

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

You can use a simple Terminal command to flush the DNS cache, and have your host file changes take immediate effect. Using the open Terminal window, then the following command:

$ dscacheutil -flushcache

Your new mappings should now take effect.

Previous Mac OS X operating systems:
How to edit the hosts file in Mac OS X 10.6 – Snow Leopard
How to edit the hosts file in Mac OS X 10.7 – Lion

34 comments

  1. Pingback by How to edit the hosts file in Mac OS X 10.7 – Lion | Grapii
    on August 2, 2012 at 2:52 pm

    [...] Mac OS X operating systems: How to edit the hosts file in Mac OS X 10.8 – Mountain Lion How to edit the hosts file in Mac OS X 10.6 – Snow [...]

  2. Pingback by How to edit the hosts file in Mac OS X 10.6 – Snow Leopard | Grapii
    on August 2, 2012 at 2:54 pm

    [...] Mac OS X operating systems: How to edit the hosts file in Mac OS X 10.8 – Mountain Lion How to edit the hosts file in Mac OS X 10.7 – [...]

  3. Comment by Sam
    on August 15, 2012 at 4:07 pm

    thanks! BTW I had “$” symbol already in terminals default string, thus there is no necessarily to type “$” for whole sudo string

  4. Comment by Steve
    on September 8, 2012 at 1:29 am

    Worked perfectly. Thanks!

  5. Comment by Jan
    on September 20, 2012 at 8:46 am

    Thank! Minor note, you should use ‘sudo -e’ rather than ‘sudo nano’.

    From man sudo:

    -e The -e (edit) option indicates that, instead of running a command, the user wishes to
    edit one or more files. In lieu of a command, the string “sudoedit” is used when
    consulting the sudoers file. If the user is authorized by sudoers the following steps
    are taken:

    1. Temporary copies are made of the files to be edited with the owner set to the
    invoking user.

    2. The editor specified by the SUDO_EDITOR, VISUAL or EDITOR environment variables is
    run to edit the temporary files. If none of SUDO_EDITOR, VISUAL or EDITOR are set,
    the first program listed in the editor sudoers variable is used.

    3. If they have been modified, the temporary files are copied back to their original
    location and the temporary versions are removed.

    If the specified file does not exist, it will be created. Note that unlike most commands
    run by sudo, the editor is run with the invoking user’s environment unmodified. If, for
    some reason, sudo is unable to update a file with its edited version, the user will
    receive a warning and the edited copy will remain in a temporary file.

  6. Comment by @jcambrohio
    on September 21, 2012 at 4:31 pm

    Thanks Rajesh, this saved me from a big headache today and got me to use terminal for something new.

  7. Comment by Mike Venables
    on October 11, 2012 at 4:31 am

    I much prefer GasMask, a free app that allows you to switch back and forth at the click of a mouse.

  8. Comment by aank budi santoso
    on November 20, 2012 at 5:01 pm

    What exactly are you trying which doesn’t work? Cause it seems to be working just fine here.

  9. Comment by tony
    on November 28, 2012 at 4:34 am

    Hi,

    How do you undo this? I edited the hosts file and the website is blocked, but when I go back to terminal to remove the ip address and website, it won’t save.

    What I mean is, “control + o” then “control + x” won’t work anymore.

    I have also flushed the dns cache too.

    Unfortunately, I can’t undo the hosts file anymore.

    Also, what key is the “M” in “M-A append” in the hosts file?

    Thanks

  10. Comment by Rajesh Patel
    on November 28, 2012 at 5:58 am

    Not sure what you’re doing or not doing exactly, but I just edited the hosts file (essentially undoing) and works fine. Can you define your steps more clearly?

  11. Comment by BMC
    on January 2, 2013 at 11:19 am

    I am having trouble – have edited hosts under 10.7 on my old mac and worked fine… having trouble on new macbook pro with 10.8

    When I PING mydomain.com and http://www.mydomain.com the correct IP address (the one I am redirecting to) is returned. However from ALL browsers (Safari, firefox and chrome) I still get the old IP – I have flushed the cache and reset safari etc but can’t seem to resolve this issue.

    Tried GASMASK also – still no go

    Ideas?

  12. Comment by BMC
    on January 2, 2013 at 11:28 am

    *** sorry correction – on my new mac under 10.8 PINGing “www.urbanhealth.com.au” doesn’t redirect (still goes to the old IP) but the url without the “www” goes to the intended/new IP…

    No matter what I type in the browser – all go to old IP – grrrr!!!

    I have listed both variants when editing the hosts file.

  13. Comment by Rohan
    on January 29, 2013 at 8:42 am

    Thanks a lot for this post. You should post this on Citirx site for those who are using Citrix receiver on Mac.
    They need to edit the hosts file in order for citrx to work on mac.

    It saved my life my not making me install Windows :)

  14. Comment by @ObjT
    on February 18, 2013 at 3:29 pm

    Thanks! Exactly what I needed.

  15. Comment by Simon
    on March 12, 2013 at 9:29 pm

    Hi, I’m having issues trying to write to the host file, when I get into it step 2 it’s blank… I try to write some codes & I get an the following error: error writing /private/ etc /hosts no such file or directory, I’m using Mountain Lion. Can you help??? Thanks

  16. Pingback by links for 03/13/2013 | Alan Vonlanthen's blog
    on March 13, 2013 at 12:35 am

    [...] How to edit the hosts file in Mac OS X 10.8 – Mountain Lion | Grapii [...]

  17. Comment by kist
    on March 27, 2013 at 3:10 am

    hey thanks for this. been very helpful! :D

  18. Comment by Helene
    on March 27, 2013 at 10:39 pm

    Wow, thank you so much! After 36 hours of hell, I was able (with your help) to figure out how to edit and save the host properly. I love this!!

    Thanks again,

    ~Helene

  19. Comment by Øyvind
    on March 31, 2013 at 10:02 am

    Hi!

    Thanks a lot for this post, but i am having an issue here. I am seemingly not allowed to edit my hosts file, and my MacBook keeps telling me that I “Don´t own the file”. I have the only user, except the guest user, on this Mac, and of course all administrative rights, so i see no reason for not being able to edit it. Is there a root user in OS X as there is in Windows? And if there is, how do i get access to it?

    - Øyvind

  20. Comment by Nick
    on April 5, 2013 at 12:14 pm

    Hey there I’m receiving the same error code as Simon: error writing /private/ etc /hosts no such file or directory though I did not see any response to this posted. =( Help would be much appreciated.
    Thank you =)

  21. Comment by Oscar
    on April 20, 2013 at 12:08 am

    Thanks Grapii. I finally got my host file edited (Mac OSX 10.8.3) Your step by step instructions where determinants to get it done. Trustable and updated informations

  22. Comment by Witty
    on April 29, 2013 at 11:01 am

    It helped me to do some important stuffs . . thank you

  23. Comment by Ian
    on April 30, 2013 at 8:46 am

    Concise and clear. Followed the instructions and enabled me to access by name a machine whose IP address had changed.
    Many thanks, very useful. Have recommended.

  24. Comment by roger
    on May 8, 2013 at 12:50 pm

    great, thanks!

  25. Comment by asheenlevrai
    on May 23, 2013 at 3:07 pm

    I tried blocking http://www.youtube.com by modifying the hosts files according to these guidlines on my mac (10.8.3).

    It didn’t work. I can still access youtube. I tride flushing the cache and even rebooting the mac… same result… youtube is still accessible…
    thoughts?
    -a-

  26. Comment by asheenlevrai
    on May 23, 2013 at 3:14 pm

    Hi,

    apparently this is an IPv6 thing… 127.0.0.1 is for IPv4
    For IPv6, it should be replaced by
    fe80::1%lo0
    or by
    ::1
    in the hosts file, apparently. I’ll try this…
    -a-

  27. Pingback by How can I add 127.0.0.1 localhost to my etc/hosts file/ - Mac-Forums Discussions for Apple Products & Services
    on June 19, 2013 at 7:39 pm

    [...] [...]

  28. Pingback by Server Move 7/14 - INGO Downtime - Page 2
    on July 14, 2013 at 9:21 pm

    [...] File – YouTube Mac: How to edit the hosts file in Mac OS X ? Leopard | Decoding the Web Mac: How to edit the hosts file in Mac OS X 10.8 ? Mountain Lion | Grapii You'll need to add the following to the host file: 198.178.120.197 ingunowners.com The server [...]

  29. Pingback by Reported: Issues with the new forum software - Page 31
    on July 15, 2013 at 11:15 pm

    [...] File – YouTube Mac: How to edit the hosts file in Mac OS X ? Leopard | Decoding the Web Mac: How to edit the hosts file in Mac OS X 10.8 ? Mountain Lion | Grapii You'll need to add the following to the host file: 198.178.120.197 ingunowners.com The server [...]

  30. Pingback by A Father’s Guide to Filtering Out the Big Bad Internet | North Rockies Design
    on July 24, 2013 at 9:21 pm

    [...] have instructions for how to set it up on Windows Vista, Windows 7, and Windows 8. For those of us Mac users, go here (http://blog.grapii.com/2012/08/how-to-edit-the-hosts-file-in-mac-os-x-10-8-mountain-lion/). If [...]

  31. Comment by Alanna
    on August 6, 2013 at 11:45 pm

    Hello, I know it’s possible and very likely that I’m doing something incorrectly even though I followed your directions but when I type the code that you gave into the terminal it responds saying “Permission denied”. I don’t know what I can do to remedy this. I am the only user on the computer and have full administrative rights.

  32. Pingback by In The Beginning… | My No Two The Same Blog
    on September 2, 2013 at 2:48 pm

    [...] source of this information found at http://blog.grapii.com/2012/08/how-to-edit-the-hosts-file-in-mac-os-x-10-8-mountain-lion/ [...]

  33. Comment by congtue
    on October 31, 2013 at 10:17 am

    Hi, i’m using Mac OS 10.8.5.
    I have followed your instructions. Then i click Finder->Go->Goto Folder-> /etc, it shows some files: hosts, hosts.save, hosts.save.1, hosts.save.2…
    No if i open Terminal anh type:

    $ sudo nano /private/etc/hosts

    This file has unsaved content that i edited.

    Please help me!
    Thanks

  34. Comment by Debby
    on January 1, 2014 at 8:00 pm

    Hi, I have no problem getting the terminal window up but when asked for a password it will not allow me to type the password in.
    HELP!?