Skip to content


Adding an entry to /etc/hosts on OS X

Web Developers, Administrators of Mac Networks, and anyone who likes to noodle with OS X will probably eventually need to add hosts file entries. I use them when I’m testing a moved and/or changed version of a website at new hosting before I move the DNS.

I used to add hosts file entries manually then wonder why they didn’t seem to work. I tried rebooting, logging out, etc with varying success, until I learned this trick. On the Mac you must flush the DNS cache for the changes to take effect. Mac OS X used to have a friendly utility called NetInfo Manager, but that seems to have been removed in Leopard, so we’re using terminal commands.

Open a terminal sesion and type:
sudo nano /etc/hosts

Hit the down arrow until you get to the end of the file, and add your hosts file entry, for example:
192.168.2.33 myintranetserver.mydomain.com
… I put in an extra line break at the end. I don’t think it’s really necessary but it’s a habit from having to use Windows, which would ignore the last line in a hosts file.

then flush the cache:
sudo dscacheutil -flushcache

…the name should now resolve! Test it:
ping myintranetserver.mydomain.com

Other Info

One thing that Windows, for all its annoyances, did get right is that changes to the hosts file (in ‘c:\Windows\system32\drivers\etc’) are reflected immediately. I’d like to see Apple do something about this in OS X, perhaps just flushing the cache automatically when /etc/hosts changes.

Another handy use for /etc/hosts is to set up mapping of virtual server addresses. For example, I use MAMP on my development Mac, and create new virtual hosts entries in Apache for each site I’m working on. Then for each I just add a new entry in the hosts file, e.g.:
127.0.0.1 devclientsite7.leftcolumn.net
… then I just use devclientsite7.leftcolumn.net:8080 in Safari or Firefox to access the development site. It means my development sites don’t have to be in subdirectories, instead I just make sure the sites use a config file to figure out what their address is, and I make sure that config file is updated when the site is moved to a live system.

On OS X Tiger (10.4) and probably earlier, there was some strange behaviour with hosts file entries. You could add an entry and it would take effect, but if you had no network available, the name would not resolve and could not be used, even if it resolved to 127.0.0.1! So you have the bizarre situation of putting in addresses for virtual hosts as described in the previous paragraph, all of which resolve to 127.0.0.1, but still not being able to use them for testing and development when not connected to the Internet. If anyone can enlighten me as to how this was anything other than a bug in OS X I’d be interested…

You can also use hosts file entries to block ads (by resolving common ad server addresses to e.g.: 127.0.0.1). You can download huge lists of ad servers for this purpose. This is a good idea when you’re stuck using Internet Explorer on Windows, especially on dial-up.

Share:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google
  • Furl
  • Reddit
  • StumbleUpon
  • Technorati
  • description
  • Ma.gnolia
  • Slashdot
  • Spurl
  • Tumblr
  • TwitThis

Posted in mac osx. Tagged with , , , .


6 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. rajender said

    Add the following lines to the /etc/sysctl.conf file:

    plz… tell me above process with the help of “VI” editor…

    Thanks in advance

  2. Joe said

    sure, open it in vi, then go to last line:
    G
    go to insert mode:
    i
    paste in the text, then back to command mode: hit ‘escape.’

    save and quit :
    :wq

  3. joe said

    Thanks!

Continuing the Discussion

  1. /etc/hosts file on Mac OSX linked to this post on November 29, 2011

    [...] See here for more about using /etc/hosts for Virtual Servers: Adding an entry to /etc/hosts on OS X [...]

  2. Adding an entry to /etc/hosts on OS X linked to this post on June 21, 2012

    [...] http://www.leftcolumn.net/2009/07/04/adding-an-entry-to-etchosts-on-os-x/ Filed Under: Uncategorized Tagged With: resource [...]

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.