<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>The Unlettered Gentleman &#187; sandboxes</title>
	<atom:link href="http://unlettered.org/tag/sandboxes/feed/" rel="self" type="application/rss+xml" />
	<link>http://unlettered.org</link>
	<description>Stuff and Nonsense</description>
	<lastBuildDate>Mon, 23 Apr 2012 03:52:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='unlettered.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/cdcfc6753f555024ea1be0d67dd22cf7?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>The Unlettered Gentleman &#187; sandboxes</title>
		<link>http://unlettered.org</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://unlettered.org/osd.xml" title="The Unlettered Gentleman" />
	<atom:link rel='hub' href='http://unlettered.org/?pushpress=hub'/>
		<item>
		<title>Fast switching your /etc/hosts file in Mac OS X</title>
		<link>http://unlettered.org/2008/03/06/fast-switching-your-etchosts-file-in-mac-os-x/</link>
		<comments>http://unlettered.org/2008/03/06/fast-switching-your-etchosts-file-in-mac-os-x/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 01:08:39 +0000</pubDate>
		<dc:creator>Sam Bauers</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[etcetera]]></category>
		<category><![CDATA[hackers]]></category>
		<category><![CDATA[hosts]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[sandboxes]]></category>

		<guid isPermaLink="false">http://unlettered.wordpress.com/?p=51</guid>
		<description><![CDATA[UPDATE: This method has been extended and superseded, find out about the &#8220;new improved&#8221; method here. Web developers often have reason to fake a host name&#8217;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 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=unlettered.org&#038;blog=2087930&#038;post=51&#038;subd=unlettered&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><img style="width:366px;height:277px;" src="http://unlettered.files.wordpress.com/2008/03/wordpress_sandbox.jpg" alt="My little sandbox" /></p>
<p><strong>UPDATE</strong>: This method has been extended and superseded, <a href="/2009/05/13/managing-multiple-etchosts-files-in-mac-os-x/">find out about the &#8220;new improved&#8221; method here</a>.</p>
<hr />
<p>Web developers often have reason to fake a host name&#8217;s associated IP address by editing their <code>/etc/hosts</code> file.</p>
<p>The peculiarities of the development environment we use here at <a href="http://wordpress.com/">WordPress.com</a> mean I find myself switching back and forth between two <code>/etc/hosts</code> configurations quite frequently. It started getting annoying to manually go into the <code>/etc/hosts</code> file and uncomment or re-comment entries all the time, so I came up with a quick and dirty solution to shortcut the process.</p>
<p>This solution involves creating two (or more) <code>/etc/hosts</code> files and writing a very short script for each which activates them.</p>
<p><span id="more-51"></span></p>
<h4>Duplicate existing <code>/etc/hosts</code> file</h4>
<p>First move your existing <code>/etc/hosts</code> file to a new location and symlink it back to the original location.</p>
<pre>$ sudo mv /etc/hosts /etc/hosts.normal
$ sudo ln -sf /etc/hosts.normal /etc/hosts</pre>
<p>This file should contain your &#8220;normal&#8221; set of entries. We will now copy it to create a file where we can have our custom entries.</p>
<pre>$ sudo cp /etc/hosts.normal /etc/hosts.custom</pre>
<p>Now we can edit the &#8220;custom&#8221; file to add our custom entries.</p>
<pre>$ sudo nano -w /etc/hosts.custom</pre>
<p>Add your entries just after the localhost entries.</p>
<pre>##
# 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</pre>
<p>Save your changes on exit (<code>ctrl-x</code>) and then we can set up the activation scripts.</p>
<h4>Set up activation scripts</h4>
<p>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&#8217;m not too keen on that.</p>
<p>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.</p>
<pre>$ echo $PATH</pre>
<p>The line it spits out is your path. Usually something like this:</p>
<pre>/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin</pre>
<p>I decided to put my scripts in <code>/usr/local/bin</code>, if the directory doesn&#8217;t already exist, you can create it.</p>
<pre>$ mkdir -p /usr/local/bin</pre>
<p>Now fire up a text editor and create the first script, this script will be the one that switches you to your &#8220;normal&#8221; <code>/etc/hosts</code> configuration.</p>
<pre>nano -w /usr/local/bin/hosts-normal</pre>
<p>In the file add the following:</p>
<pre>#!/bin/bash
sudo ln -sf /etc/hosts.normal /etc/hosts
dscacheutil -flushcache</pre>
<p>The <code>dscacheutil</code> 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 <code>dscacheutil</code> with the older command <code>lookupd</code> to achieve the same result.</p>
<p>Save the file and change it&#8217;s permissions.</p>
<pre>$ sudo chown root:wheel /usr/local/bin/hosts-normal
$ sudo chmod 750 /usr/local/bin/hosts-normal</pre>
<p>Now we can copy this script to create another for the custom configuration.</p>
<pre>$ sudo cp /usr/local/bin/hosts-normal /usr/local/bin/hosts-custom</pre>
<p>Edit the new script to point to the custom hosts file created earlier.</p>
<pre>$ sudo nano -w /usr/local/bin/hosts-custom</pre>
<p>Just change the target of the symlink command.</p>
<pre>#!/bin/bash
sudo ln -sf /etc/hosts.custom /etc/hosts
dscacheutil -flushcache</pre>
<p>Check the permissions on both files, they should look somthing like this:</p>
<pre>$ 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</pre>
<h4>Finally, using your scripts</h4>
<p>That was quite a lot of work, but here&#8217;s the pay-off. To switch to your &#8220;normal&#8221; hosts just call your <code>hosts-normal</code> script.</p>
<pre>$ hosts-normal
password:</pre>
<p>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 &#8220;custom&#8221; hosts configuration.</p>
<pre>$ hosts-custom
password:</pre>
<p>You can create additional sets of host configurations and scripts to match as you require them.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/unlettered.wordpress.com/51/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/unlettered.wordpress.com/51/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/unlettered.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/unlettered.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/unlettered.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/unlettered.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/unlettered.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/unlettered.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/unlettered.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/unlettered.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/unlettered.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/unlettered.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/unlettered.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/unlettered.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/unlettered.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/unlettered.wordpress.com/51/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=unlettered.org&#038;blog=2087930&#038;post=51&#038;subd=unlettered&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://unlettered.org/2008/03/06/fast-switching-your-etchosts-file-in-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/72d92b431bfc322bba32e24cf3183d24?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=X" medium="image">
			<media:title type="html">sambauers</media:title>
		</media:content>

		<media:content url="http://unlettered.files.wordpress.com/2008/03/wordpress_sandbox.jpg" medium="image">
			<media:title type="html">My little sandbox</media:title>
		</media:content>
	</item>
	</channel>
</rss>
