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 Right now that doesn't work:
$ ping -q -c 1
ping: cannot resolve Unknown host
Create an entry for it and try again:
$ sudo dscl localhost -create /Local/Default/Hosts/ \
$ ping -q -c 1
PING ( 56 data bytes

--- 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
$ dscl localhost -read /Local/Default/Hosts/
AppleMetaNodeLocation: /Local/Default
RecordType: dsRecTypeStandard:Hosts
$ sudo dscl localhost -delete /Local/Default/Hosts/
$ dscl localhost -list /Local/Default/Hosts

1 comment:

mikec said...

I can't ping or localhost. Could it be a firewall issue ? Following is contents of my hosts file. localhost broadcasthost
::1 localhost
fe80::1%lo0 localhost