OS X 10.5.7 - /etc/hosts, no! dscl, yes!

May 26 2009
Posted by Anj

Recently I upgraded my older gray body MBP 15″ to the new fancy unibody MBP 15″. Fun and excitement ensued and after some account name mishaps with Migration Assistant, I got it up and running. Like a champ, this laptop is.

But I noticed that it wouldn’t resolve any of my VPN IP addresses, which I had added in the /etc/hosts file, the usual place to add specific IP addresses I want to resolve. After harnessing the awesome power of the “internet search”, I found that Leopard uses a system utility to manage configuration files. Specifically, dscl. Sadly, Apple’s usless help page for dscl is the first result. But this post may be somewhat helpful, if not totally outdated.

Check Current Hosts

Run dscl as root:

sudo dscl

You can explore it just as you would a filesystem i.e. ls,cd, etc.

Go to the hosts directory:

cd /Local/Default/Hosts

Now, see whats already there:

ls

If you haven’t done this before, you shouldn’t see any entries.

Add a Host

Lets add the host 192.168.112.2o7.net to point to localhost, just because they are evil.

Add the host:

create 192.168.112.2o7.net IPAddress 127.0.0.1

I found that I couldn’t actually add this as a single command:

sudo dscl -create /Local/Default/Hosts/192.168.112.2o7.net IPAddress 127.0.0.1

Seems like it should be possible, but I was only able to do it in multiple steps.

Delete a host

Deleting is almost as easy as adding a host; simply issue the delete command:

delete 192.168.112.2o7.net

NB: You may need to flush the cache of stale entries before your changes take effect:

dscacheutil -flushcache

I didn’t need to, but I didn’t have any entries in there.

Be sure to check the man page for a complete listing of other commands offered by dscl.

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “OS X 10.5.7 - /etc/hosts, no! dscl, yes!”

  1. jona says:

    Thanks for the tip … this is how you would type these dscl commands as a single command from the shell:

    sudo dscl . create Hosts/192.168.112.2o7.net IPAddress 127.0.0.1
    sudo dscl . delete Hosts/192.168.112.2o7.net

    OR

    sudo dscl /Local/Default create Hosts/192.168.112.2o7.net IPAddress 127.0.0.1
    sudo dscl /Local/Default delete Hosts/192.168.112.2o7.net

Leave a Reply