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 thoughts 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.
Use Bootcamp to install Windows XP Pro and everything will be fine! 😉
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 wordpress.com, then it’s out with vim, change modes to insert, ESC, :wq
Simple eh? 😉
I have to search for that command every time I edit my hosts file. Just added this to my
alias reloadhosts="sudo niload -v -m hosts .
Okay, then WP ate that, but you get the idea. Paste the command into quotes.
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
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
999.999.999.999 foobar.org foobar.org
after that I tried the cache updating command
to check how the name is resolved:
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.
This is what i need but cant get it working correctly. I’m using OS X snow leopard. Does someone has an idea?
niloadwas only included up to Mac OS X 10.4, not in Leopard or Snow Leopard.
/etc/hostsshould be automatic in 10.5+, but you can always flush the cache with
Hope it helps!