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! šŸ™‚

By Donncha

Donncha Ɠ Caoimh is a software developer at Automattic and WordPress plugin developer. He posts photos at In Photos and can also be found on Twitter.

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

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.

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.


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? šŸ˜‰

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!

You can also just use
lookupd -flushcache

and it will reload the hosts file (and flush other caches, afaik).

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:

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)

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

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.

Hmmm. The problem went away when I deleted the DNS numbers in network prefs and substituted new ones I was given.

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


Leave a Reply