Monday, October 20, 2008

/etc/hosts and Mac OS X 10.5 Leopard

Instead of using /etc/hosts on Mac OS Leopard, the proper way to add hosts is using the dscl utility. First, let's list the hosts already there:
$ dscl localhost -list /Local/Default/Hosts
$
Nothing. Let's add one. I want to be able to access localhost as me.blogger.com. Right now that doesn't work:
$ ping -q -c 1 me.blogger.com
ping: cannot resolve me.blogger.com: Unknown host
$
Create an entry for it and try again:
$ sudo dscl localhost -create /Local/Default/Hosts/me.blogger.com \
  IPAddress 127.0.0.1
$ ping -q -c 1 me.blogger.com
PING me.blogger.com (127.0.0.1): 56 data bytes

--- me.blogger.com ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.077/0.077/0.077/0.000 ms
You can list the hosts you have set up, where they point, and you can delete them:
$ dscl localhost -list /Local/Default/Hosts
me.blogger.com
$
$ dscl localhost -read /Local/Default/Hosts/me.blogger.com
AppleMetaNodeLocation: /Local/Default
IPAddress: 127.0.0.1
RecordName: me.blogger.com
RecordType: dsRecTypeStandard:Hosts
$
$ sudo dscl localhost -delete /Local/Default/Hosts/me.blogger.com
$ dscl localhost -list /Local/Default/Hosts
$

1 comment:

mikec said...

I can't ping 127.0.0.1 or localhost. Could it be a firewall issue ? Following is contents of my hosts file.

127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost