Changing Mac OS X Hosts File

This is more for me so I don't forget it, but other folks might find it useful. If you want to add or delete an entry from the Mac OS X /etc/hosts file, its a bit more involved than on Windows. Here's the steps you need to do.

To add an entry:
  • Edit /etc/hosts
  • sudo niload -v -m hosts . < /etc/hosts
  • If on Firefox, go to Tools > Clear Private data and clear things.
  • lookupd -flushcache
To remove the entry:
  • Edit /etc/hosts
  • sudo niload -v -d hosts . < /etc/hosts
  • If on Firefox, go to Tools > Clear Private data and clear things.
  • lookupd -flushcache
Notice the -d for deletion.

Comments

Ole Begemann said…
Is this really necessary? I find new additions to /etc/hosts are usually available right after editing the file.
burtonator said…
There's a way to avoid this.... you just have to set a system property to tell it to go to the hosts file first.

But I forget the property :)

I always google for it when I do an OS X re-install which is like once every year and half.

For example:

sh-3.2# echo "127.0.0.1 fake.server.com" >> /etc/hosts
sh-3.2# ping fake.server.com
PING fake.server.com (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.058 ms

worked fine on my machine.

Anyway... maybe that helps save you a step.

It bothers me that OS X isn't really Unix.... it's more like an insane unix than anything else.

I'm thinking about going back to Linux myself :)
ttrenka said…
This seems like its a bit of overkill...all I do is:

sudo vi /etc/hosts
[enter password]
[make my changes]
:wq

I do this a lot, especially since I do almost all my dev using virtual machines with assigned IP addresses, and to test on the host OS (OS X) I need to ensure that the IP address matches the host name I'm looking for.

I've never had to stop any services to do this.

Maybe that will help?
Brad Neuberg said…
@Tom: Hi Tom, I started by doing that but Firefox doesn't always reliably pick up the addition or removal of an address. _Sometimes_ it does but not always. The extra steps ensure that Firefox picks up the changes. Otherwise its looking into an IP cache that might be stale.
Mark said…
You can also get FF to clear its IP cache by hitting File > Work Offline twice (once to go offline, once to go back online).
SkinnyBoy said…
What version of OS X are you guys using... i tried to use niload and it said command not found (i'm on 10.5.3)
Joachim said…
niload is a thing of the past.. (10.4.x, OS X Tiger)
In Leopard /etc/hosts work just like on any other unix so there is nothing else that needs to be done on newer os X machines.
Santa said…
What gives! it doesent work at all for me.


gcostanzo@euclid:~$ uname -a
Darwin euclid.local 10.4.0 Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386 i386
gcostanzo@euclid:~$ cat /etc/host
tv 192.168.1.115
joshua 192.168.1.113
gcostanzo@euclid:~$ sudo dscacheutil -flushcache
gcostanzo@euclid:~$ ping tv
PING tv (192.168.1.100): 56 data bytes