Reloading hosts files in OS X

Share on facebook
Share on twitter
Share on linkedin

Now that I’m using a Mac at home I’ve had to go through a bit of a mind switch when it comes to doing things I’ve been to doing for years.

Today’s mind bender came about because I needed to edit the hosts file to force Snow Leopard to find a URL on a different IP address to where it actually is. In Windows you just edit %SystemRoot%system32driversetchosts then run nbtstat -R to reload the hosts file but in OS X this obviously doesn’t work.

You still need to edit the hosts file, located at /etc/hosts, but obviously there’s no nbtstat command to run afterwards. Depending on where you look on Google you’ll find a number of answers, from running niload to lookupd but none of those worked for me. Thankfully I found the answer – here it is.

Flushing DNS Cache:

sudo dscacheutil -flushcache

Obviously you’ll need the sudo part unless you’re running your shell as root … let’s not get into that, though. 🙂

If you want to make an alias to this command so you don’t need to type the whole thing every time, do this:

Flushing DNS Cache:

alias flushcache=’sudo dscacheutil -flushcache’