How to edit the hosts file in Mac OS X 10.6 – Snow Leopard

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+s (cmd+s) to save the file.

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.

Newer 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 – Lion

10 comments

  1. Comment by shayne
    on August 17, 2011 at 12:51 pm

    you can also just edit /etc/hosts as it’s just a symlink.

    also you don’t require doing a dns flush.

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

    [...] 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 – [...]

  3. Comment by Marc
    on October 9, 2012 at 6:23 am

    What do I do if it won’t let me type in my password? Thanks

  4. Comment by Rajesh Patel
    on October 9, 2012 at 6:29 am

    What do you mean it won’t let you?

  5. Comment by Marc
    on October 27, 2012 at 12:49 pm

    Hi Rajesh,

    After entering the sudo nano command in step 2, it asks me for the password, but I can not type anything. I have no idea why.

  6. Comment by Steve
    on January 9, 2013 at 1:33 pm

    Hi Marc, the password isn’t echoed, meaning that when you type it, nothing appears on screen. This is a feature, not a bug – it prevents anyone from seeing the password, or even just the length of your password over your shoulder.

    Just type it and press return. It will work, even though you can’t see it.

  7. Comment by michelle
    on January 25, 2013 at 9:04 pm

    when i enter sudo nano/private/etc/hosts i get “command not found”

    help?

  8. Comment by Rajesh Patel
    on January 26, 2013 at 12:01 am

    Did you put a *space* between nano and /private…

  9. Comment by Maini
    on May 25, 2013 at 1:39 am

    Thanks for the tip… Worked…

  10. Comment by Tania
    on January 15, 2014 at 7:37 am

    Hello

    this post was really helpful but I still have a question. It doesn’t seem to let me add the new info underneath the default ones. I can only seem to type before the ##

    I try to stay far far away from Terminal because I don’t know enough about what I’m doing so I don’t know why it’s not letting me type at the end or what to do about it. Thanks for any help.