Hosts aliases in Leopard

Back in Tiger days, I used to love having aliases pointing to localhost or other servers saving me some typing and allowing me to use Apache vhosts locally. To achieve this without restarting the whole system, I used NetInfo to add a new Machine entry, this works great, no restart needed only NetInfo reload.

With Leopard, NetInfo is gone (for the best) and it has been replaced by a folder hierarchy full of flat .plist files. No more NetInfo Manager, but a new dscl command and a bunch of related one : dscacheutil (lookupd replacement), dseditgroup, dsmemberutil, etc.

For today, we’ll use dscl and dscacheutil, let’s create a new Host entry to map myvhost.loc to 127.0.0.1 (I like adding .loc to distinguish my added hosts, but you’re free to use anything else) :

sudo dscl localhost -create /Local/Default/Hosts/myvhost.loc IPAddress 127.0.0.1

Then to be sure it’s looked up correctly by software (in fact it’s only needed if you tried to access the myvhost.loc before adding it) :

sudo dscacheutil -flushcache

A little handy feature of the new dscacheutil when we’re at it :

sudo dscacheutil -cachedump -entries Host

This will list all entries cached by Leopard, it can comes in handy when trying to solve a name resolution problem.

And finally if you want to get ride of your entry later on :

sudo dscl localhost -delete /Local/Default/Hosts/myvhost.loc

[Update 2008/01/07]
Benjamin Stiglitz wrote a comment to say /etc/hosts is now automatically watched for changes on Leopard, so there’s no more need to play with dscl to add simple host. Thanks again Benjamin.

Sat, 05 Jan 2008 18:36 Posted in ,

  1. By Ned Baldessin 05/01/2008 at 20h24


    Why not simply use the /etc/hosts file ? Works fine for me, both under Tiger and Leopard.

  2. By Jonathan Tron 05/01/2008 at 23h37


    Ned : As I said in the post, because /etc/hosts solution requires a system restart before changes are effective.

  3. By Benjamin Stiglitz 07/01/2008 at 18h39


    Actually, the /etc/hosts file is automatically watched for changes on Leopard. You won’t need a system restart, but do be aware that some apps may cache nameserver lookups.

  4. By Jonathan Tron 07/01/2008 at 19h04


    Benjamin : Thanks for the info, I just tried again and it worked. Don’t know why it wasn’t the case the last time. Anyway it’s a really nice change.

  5. By James 11/01/2008 at 00h05


    Awesome thanks for the tip. /etc/hosts did not work for me on leopard I assumed it would (unix background).

    This worked though.

  6. By Jonathan Tron 16/01/2008 at 22h17


    James : after more tests it seems changes are picked up only if you add/remove a/some new line(s). Plus don’t forget to clear the dns cache if you already tried accessing the host you’re setting up, plus don’t forget your browser cache if you try through one.

  7. By sneeuwitje #at- sneeuwitje -dot# org 19/01/2008 at 02h42


    Thanks a bunch!
    I created a little script that uses the above, and might inspire others to expand or automate (eg: put in a directory→run (tuned script that works with ‘./’ >> no args)→dir==accessible):


    #!/bin/bash

    1. bash shell script to add/remove Directory Service to localhost
    2. scripted to the occasion of OSX Leopard (10.5) missing netinfo.app
    3. rendering the creation of netinfo ‘machines’ impossible
    4. typical use:
    5. sudo sh /users/myUserName/sites/localhost-dscl.sh add /Users/myUserName/Sites/projects
    6. It will typically enable Apache WebServer to enable a VirtualHost on the directory
    7. thanks Jonathan: http://jonathan.tron.name/articles/2008/01/05/hosts-aliases-in-leopard

    if [[ $1 && $1 == “add” ]]
    then
    if [[ $2 && -d $2 ]]
    then
    sudo dscl localhost -create $2 IPAddress 127.0.0.1
    sudo dscacheutil -flushcache
    sudo dscacheutil -cachedump -entries Host
    echo “Directory $2 is now accessible as localhost on IP 127.0.0.1”
    else
    echo “you have to specify a valid directory to add as 2nd argument”
    fi
    exit
    fi

    if [[ $1 && $1 == “remove” ]]
    then
    if [[ $2 && -d $2 ]]
    then
    sudo dscl localhost -delete $2;
    echo “Directory access as localhost on IP 127.0.0.1 has been removed from $2”
    else
    echo “you have to specify a valid directory to remove as 2nd argument”
    fi
    exit
    fi

    echo “##”
    echo “## requires 2 arguments:”
    echo “## \$1 a valid action (add/remove) as the 1st argument, and”
    echo “## \$2 a valid directory as the 2nd argument”
    echo “## e.g.:”
    echo “sudo sh /users/myUserName/sites/localhost-dscl.sh add /Users/myUserName/Sites/projects”
    echo “##”

  8. By Jonathan Tron 19/01/2008 at 14h57


    sneeuwitje : nice script thanks to share ! I too have some shortcuts for cache clearing and other tasks as simple shell aliases. And a bunch of ruby scripts for tasks such as vhosts creation, trac installation, etc.

  9. By Georg Heinrich 13/02/2008 at 20h28


    Thanks for the tip.
    Unfortunately, nothing seems to work for me, neither /etc/hosts nor your tip. Maybe because my machine is bound to an OpenLDAP server? Any clues?

  10. By William 09/03/2008 at 03h35


    Thank you so much for figuring this out. I was so sad that 127.0.0.1 and localhost weren’t working with PHP enabled!

  11. By UncleFoobar@moo.gov 17/06/2008 at 00h28


    you can always get hosts to load by going to sys prefs, networking and have a config that is a dupe set up; switching between will load goodness under Tiger after changes to /etc/hosts

  12. By Pablo Ramirez 29/08/2008 at 02h14


    This happen to me:

    PRF:Desktop PRF$ cat /etc/hosts | grep tracker
    tracker 192.168.1.182

    but…

    PRF:Desktop PRF$ ping tracker
    ping: cannot resolve tracker: Unknown host
    PRF:Desktop PRF$

    how something such trivial in *NIX systems doesn’t work in leopard, please me help on this.

  13. By Jonathan Tron 29/08/2008 at 12h40


    Pablo : it looks like you inversed hostname / ip in your hosts file.

    The correct syntax is :
    ip hostname

    So :
    192.168.1.182 tracker
    in your case

  14. By Christoph Bünte 06/09/2008 at 14h33


    Thx a million for the howto. /etc/hosts used to work for me all the time. Last week i tried to add another alias for localhost, and it didn’t seem to work. Even restarting the computer wouldn’t help. I tried to your way and it worked for exactly one request in safari. The response to the second request would say, that it couln’t find the hostname. Why the f*** did they mess with the good old /etc/hosts? It was so convinient!

    Has anyboday else the same experience? Any help is highly appreciated

  15. By gs 15/05/2009 at 01h18


    Interesting. Doing a Host inquiry or using the Lookup in Network Utility by-passes the hosts file.

    Either above method gives me the “legit” info, whereas using Safari (and presumably other apps) will resolve using the hosts file. (eg, 127.0.0.1 for ad severs)

  16. By Laurei 28/06/2009 at 21h01


    Thanks for this tip, makes adding hosts so much easier, I’ve found my self coming here every time I need to add hosts because I forget exactly how to :)

  17. By tyler Havener 01/07/2009 at 23h56


    Does anyone know of tricks or ability for this to support wildcards (like DNS does)? /etc/hosts files didn’t support wildcards. I would assume that this new method is not any different than hosts files in that regard.

  18. By Isaac Seymour 06/08/2009 at 16h27


    Thank god for this article. I’ve tried twice before to set up virtual hosts on my Mac, to no avail. I’ll never bother with localhost again – setting up a virtual domain is so much better! THANK YOU!

  19. By Dave 16/09/2009 at 07h00


    Slight problem with all this is that you are never supposed to alias anything to 127.0.0.1 as this is known to break many things. 127.0.0.1 should only ever resolve to “localhost” or “localhost.localdomain”.

    You should be adding ipaliases using ipconfig or the /etc/IPAliases file and then using those for your local Apache vhosts.

    1. Add the following line to /etc/hostconfig:

    IPALIASES=-YES-

    2. In /etc create a file IPAliases.conf with contents of each line following the pattern :

    /
    eg:
    lo0:172.16.10.1/32
    lo0:172.16.10.2/32
    lo0:127.0.1.2/32

    3. Create the directory /System/Library/StartupItems/IPAliases and in it create the script IPAliases with the following contents:

    #! /bin/sh

    . /etc/rc.common

    CONF=/etc/IPAliases.conf

    if [ ! “${IPALIASES:=-NO-}” = “YES” ] ; then
    exit 0
    fi

    if [ ! -f “$CONF” ] ; then
    ConsoleMessage “File $CONF does not exist. No IP aliases to define.”
    exit 0
    fi

    cat $CONF | grep -vE ‘^[[:space:]](#.)?$’ | while read ALIAS ; do
    INTERFACE=$(echo $ALIAS | cut -f1 -d:)
    IPADDR=$(echo $ALIAS | cut -f2 -d:)
    NETMASK=$(echo $ALIAS | cut -f3 -d:)
    NETMASK=${NETMASK:+netmask $NETMASK}
    ifconfig $INTERFACE alias $IPADDR $NETMASK
    RET=$?
    if [ “$RET” -ne 0 ] ; then
    MSG=“$RET (error)”
    else
    MSG=“0 (OK)”
    fi
    ConsoleMessage “IPAliases: ifconfig $INTERFACE alias $IPADDR $NETMASK / returned $MSG
    done

    Now create StartupParameters.plist in the same directory with contents:

    {
    Description = “IP aliasing startup”;
    Provides = (“IPAliases”);
    Requires = (“Network”);
    OrderPreference = “None”;
    Messages =
    {
    start = “Starting IP aliasing”;
    stop = “Stopping IP aliasing”;
    };
    }

    There are obviously other ways this could be achieved but using this technique will at least be consistent with Apple’s own config in OSX Server.

  20. By Dave 16/09/2009 at 07h02


    Actually to be more correct you should use /Library/StartupItems/IPAliases rather than /System/Library/StartupItems/IPAliases as this is not an Apple shipped config.

  21. By Dave 16/09/2009 at 07h03


    Note that this is an area which is being evolved away in Mac OS X so this advice may not be valid beyond Snow Leopard.

  22. By Dave 16/09/2009 at 07h29


    Actually it would seem the most appropriate place to put this stuff these days would be in /var/db/dslocal/nodes/Default/computers using dscl.

  23. By Jamie van Dyke 24/11/2009 at 15h57


    Leopard comes with Ruby (as does Snow Leopard). Ruby has a gem that handles this really easily:

    Add the ghost gem:

    sudo gem install ghost

    Add a host that maps “blah.local” to 127.0.0.1:

    ghost add blah.local

    List what you have so far:

    ghost list

    Delete:

    ghost delete blah.local

    Just thought I’d add this.

Comment Hosts aliases in Leopard


RSS