Mac OS X – how to update /etc/hosts

Way back in the good old days of Linux and Windows it was much easier to update my hosts file. There it was, /etc/hosts or C:\windows\hosts, edit, save and the change becomes active.

MacOS X is a little more complicated. Once you update /etc/hosts you’ll have to update the Netinfo Database. That’s why I’m blogging this so I’ll remember it. This page documents the steps required but the important command is this one:

sudo niload -v -m hosts . < /etc/hosts

There is a GUI but it’s a little clunky and duplicating an existing entry isn’t the most elegant method of adding a new one. Especially when a warning dialog pops up!

Thanks Barry for suggesting a similar fix while I was in SF and getting used to my new laptop! 🙂



20 Replies to “Mac OS X – how to update /etc/hosts”

  1. NetInfo was one of those great ideas back in the early 90’s, especially when NIS was the other option, but they’ve buried it so deep into the system it’s taking them years to dig it out.

    It’s influence diminishes with every release but still. Yuck.

  2. If only. The hosts file on Windows is in C:\WINDOWS\system32\drivers\etc these days. Pain the hole to get to unless you’re efficient enough to set up a shortcut.


  3. ah so, Linux remains the easiest to update. Slight irony in that methinks!

    Luckily I don’t have to update the hosts file too often except when I’m debugging a blog on, then it’s out with vim, change modes to insert, ESC, :wq
    Simple eh? 😉

  4. Holy crap! I’ve been bashing my head against the wall trying to get this to work for months! Thanks for the post. My co-worker was starting to think I was an idiot. The worst part is, he has a Macbook, too, and didn’t have to type the magic command to get /etc/hosts to reload. Fah!

  5. Yep, the -flushcache thing is exactly it. No need to fiddle with the netinfo commands.

    Also, this page at Apple is very helpful, although it should mention the flushcache command for completeness:

  6. Mac OS 10.4 and 10.5, (and I presume 10.3) you can update /private/etc/hosts exactly like in Linux’s /etc/hosts

    in 10.5 /etc is just a symlink to /private/etc, so editing the /etc/hosts in 10.5 will work perfectly. (remember to do a “sudo su” before editing with vim og nano og wathever)

  7. Tried on Tiger 10.4.11

    sudo niload hosts . < hosts.txt
    tried this, also had to have my hosts.txt in this format

    X.X.X.X hostname alias

    For some strange reason if you don’t put the alias there (even if the alias is the same as the hostname), it doesn’t seem to get read properly. Therefore my hosts file is full of these entries


    after that I tried the cache updating command
    lookupd -flushcache

    to check how the name is resolved:
    lookupd -d

  8. I’ve been directed here by someone trying to help me with … suddenly one Mac on a shared network, shared wired internet connection, can’t get to a couple of websites but gets redirected to a domain name parking site. The other machines have no problem with these sites, the one laptop can get to other sites. It’s running 10.4.11 and the same thing happens in Safari and Firefox.

    Make sense to anyone? Thanks.

    1. you can edit the file and use the command “dscacheutil -flushcache” to have your changes take immediate effect, too.


  9. niload was only included up to Mac OS X 10.4, not in Leopard or Snow Leopard.

    Reloading of /etc/hosts should be automatic in 10.5+, but you can always flush the cache with dscacheutil -flushcache.

    Hope it helps!

Leave a Reply