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 Comments

Mark (5 comments.) on October 12, 2006 at 12:35 pm.

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.

Reply

Mel (9 comments.) on October 12, 2006 at 5:14 pm.

Use Bootcamp to install Windows XP Pro and everything will be fine! ;)

Reply

adam (22 comments.) on October 12, 2006 at 6:47 pm.

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.

Oh.

Reply

Donncha (1707 comments.) on October 12, 2006 at 10:52 pm.

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 wordpress.com, then it’s out with vim, change modes to insert, ESC, :wq
Simple eh? ;)

Reply

Mark Jaquith (12 comments.) on October 13, 2006 at 5:11 am.

I have to search for that command every time I edit my hosts file. Just added this to my ~/.bash_profile :

alias reloadhosts="sudo niload -v -m hosts .

Reply

Mark Jaquith (12 comments.) on October 13, 2006 at 5:15 am.

Okay, then WP ate that, but you get the idea. Paste the command into quotes.

Reply

Erik Innocent on November 10, 2006 at 7:49 pm.

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!

Reply

codeFiend on March 1, 2007 at 9:14 pm.

You can also just use
lookupd -flushcache

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

Reply

Anonymous on July 19, 2007 at 1:49 am.

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:

Reply

Henrik on November 27, 2007 at 12:37 pm.

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)

Reply

tigernam on May 29, 2008 at 5:22 am.

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

999.999.999.999 foobar.org foobar.org

after that I tried the cache updating command
lookupd -flushcache

to check how the name is resolved:
lookupd -d
hostWithName foobar.org

Reply

Kath on January 13, 2009 at 7:35 pm.

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.

Reply

Kath on January 18, 2009 at 7:24 pm.

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

Reply

Anonymous on August 24, 2009 at 5:48 pm.

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

cheers

Reply

Fotograf, Mirza Oezoglu (1 comments.) on March 24, 2010 at 5:43 pm.

This is what i need but cant get it working correctly. I’m using OS X snow leopard. Does someone has an idea?

cheers
Mo

Reply

Florent Delannoy (1 comments.) on December 29, 2010 at 10:24 pm.

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!

Reply

Leave Your Comment

Your email will not be published or shared. 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>


Holy Shmoly! is Stephen Fry proof thanks to caching by WP Super Cache