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

35 comments

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

  2. 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.

  3. 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

  4. 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?

  5. 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?

  6. *** 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.

  7. 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 :)

  8. 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

  9. 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

  10. 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

  11. 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 =)

  12. 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

  13. 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.

  14. 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-

  15. 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-

  16. 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.

  17. 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

  18. 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!?

  19. Hi,

    I have followed the instructions clearly but every time I finish and clear the cache I still have access to the website I am trying to block. Any suggestions?

    Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>