Fast switching your /etc/hosts file in Mac OS X

My little sandbox

UPDATE: This method has been extended and superseded, find out about the “new improved” method here.


Web developers often have reason to fake a host name’s associated IP address by editing their /etc/hosts file.

The peculiarities of the development environment we use here at WordPress.com mean I find myself switching back and forth between two /etc/hosts configurations quite frequently. It started getting annoying to manually go into the /etc/hosts file and uncomment or re-comment entries all the time, so I came up with a quick and dirty solution to shortcut the process.

This solution involves creating two (or more) /etc/hosts files and writing a very short script for each which activates them.

Duplicate existing /etc/hosts file

First move your existing /etc/hosts file to a new location and symlink it back to the original location.

$ sudo mv /etc/hosts /etc/hosts.normal
$ sudo ln -sf /etc/hosts.normal /etc/hosts

This file should contain your “normal” set of entries. We will now copy it to create a file where we can have our custom entries.

$ sudo cp /etc/hosts.normal /etc/hosts.custom

Now we can edit the “custom” file to add our custom entries.

$ sudo nano -w /etc/hosts.custom

Add your entries just after the localhost entries.

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1		localhost
255.255.255.255		broadcasthost
::1			localhost
fe80::1%lo0		localhost

# Custom entries
10.0.1.4		host.example.com
192.168.4.2		anotherhost.example.com

Save your changes on exit (ctrl-x) and then we can set up the activation scripts.

Set up activation scripts

We will now setup an activation script for each file, this could be more cleverly done using a single script, but then we would need to do tricky things to get tab command completion to work, and I’m not too keen on that.

Create a new file in a location that is in your PATH. You can see the directories that are in your path by doing the following.

$ echo $PATH

The line it spits out is your path. Usually something like this:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

I decided to put my scripts in /usr/local/bin, if the directory doesn’t already exist, you can create it.

$ mkdir -p /usr/local/bin

Now fire up a text editor and create the first script, this script will be the one that switches you to your “normal” /etc/hosts configuration.

nano -w /usr/local/bin/hosts-normal

In the file add the following:

#!/bin/bash
sudo ln -sf /etc/hosts.normal /etc/hosts
dscacheutil -flushcache

The dscacheutil command will flush the existing local DNS cache and make the changes available right away. If you happen to be using OS X 10.4, then replace dscacheutil with the older command lookupd to achieve the same result.

Save the file and change it’s permissions.

$ sudo chown root:wheel /usr/local/bin/hosts-normal
$ sudo chmod 750 /usr/local/bin/hosts-normal

Now we can copy this script to create another for the custom configuration.

$ sudo cp /usr/local/bin/hosts-normal /usr/local/bin/hosts-custom

Edit the new script to point to the custom hosts file created earlier.

$ sudo nano -w /usr/local/bin/hosts-custom

Just change the target of the symlink command.

#!/bin/bash
sudo ln -sf /etc/hosts.custom /etc/hosts
dscacheutil -flushcache

Check the permissions on both files, they should look somthing like this:

$ ls -la /usr/local/bin
total 16
drwxr-xr-x   4 root  wheel  136 14 Feb 22:49 .
drwxr-xr-x  12 root  wheel  408 05 Mar 20:40 ..
-rwxr-x---   1 root  wheel   77 05 Mar 22:49 hosts-normal
-rwxr-x---   1 root  wheel   78 05 Mar 22:53 hosts-custom

Finally, using your scripts

That was quite a lot of work, but here’s the pay-off. To switch to your “normal” hosts just call your hosts-normal script.

$ hosts-normal
password:

You will be prompted for your admin password as the script makes the symlink using a sudo command. The process is similar to switch to your “custom” hosts configuration.

$ hosts-custom
password:

You can create additional sets of host configurations and scripts to match as you require them.

12 comments
  1. killeader said:

    Tnx…. great tutorial!!! 🙂

  2. Ed Phelps said:

    Great post. Thanks!!! This is extremely helpful.

  3. Awesome post, I’m a recent converter to Mac and Ive been looking for an easy way to do that for ages!!

    Thanks..

  4. Wonderful – I’m now using this technique constantly. Just wanted to thank you for making my life that much easier.

  5. Sergio said:

    Thanks a lot. It help me resolve a more than weird bug !

  6. Steve said:

    What is used a default on boot up?? Does it use what was used last or default to the normal one??

    • Because it makes a symbolic link it uses whatever you set it to last time on boot.

  7. Eduardo Sanchez said:

    Thank you. I’m using your method with AirPort Location. When I’m at work AirPort Location switches to one hosts file and when I open my laptop at home it switches to the other hosts file.

  8. Pedro said:

    Hi there and thanks for this great tutorial.

    I have another thing that I’m trying to get done without any luck. I’d like to be able to run 2 browsers in the same time (Firefox and Chrome for example), but loading their data from 2 different hosts files. This will enable me to check a site I’m working on vs the actual live site, without actually making the switch of the hosts files.

    Has anybody been able to do that on a mac? I heard it’s possible on Linux if you recompile some parts of the kernel, but I couldn’t find anything like that for a mac.

    Thanks again for this nice article

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: