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
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.
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:
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”
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
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 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.