Local Host Entries on OS X Snow Leopard

Today I wanted to setup local host entries on my MacBook. Searching around I found several articles about editing the /private/etc/hosts file to add local domains entries pointing back to my computer. I don’t know if it is something that changed with Leopard or Snow Leopard but it just wasn’t working for me. Even restarting the computer was a no go.

Using the dscl Utility

With the wonder of Google searching and the generous comments of others I found out about the dscl utility.

Listing Host Entries

To see what is listed for your hosts at any time just run this command:

dscl localhost -readall /Local/Default/Hosts

Adding a New Entry

In this example I am going to have the snow.ftd host point to my 127.0.0.1 IP address:

sudo dscl localhost -create /Local/Default/Hosts/snow.ftd IPAddress 127.0.0.1

You should now be able to ping snow.ftd and have it resolve to your local computer.

Removing an Existing Entry

If you decide you do not want an entry anymore just run this command to remove it:

sudo dscl localhost -delete /Local/Default/Hosts/snow.ftd

Reading the BSD Flat File

It was also noted that you can read the BSD flat file using this command:

dscl localhost -readall /BSD/local/Hosts

Comments

2 Comments so far. Leave a comment below.
  1. Excellent thanks so much for this. Just moved to mac from PC and had given up on setting up virtual hosts with Railo.

  2. I don’t know of a way to import them using dscl… but you could probably write a bash script to do it if you had a ton of entries to pull over.

Trackbacks

One Trackback

Add Your Comments

Required
Required
Tips

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <ol> <ul> <li> <strong>

Your email is never published nor shared.

Ready?