<?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; howto</title>
	<atom:link href="http://unlettered.org/tag/howto/feed/" rel="self" type="application/rss+xml" />
	<link>http://unlettered.org</link>
	<description>Stuff and Nonsense</description>
	<lastBuildDate>Mon, 06 Feb 2012 05:26:54 +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; howto</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>Managing multiple /etc/hosts files in Mac OS X</title>
		<link>http://unlettered.org/2009/05/13/managing-multiple-etchosts-files-in-mac-os-x/</link>
		<comments>http://unlettered.org/2009/05/13/managing-multiple-etchosts-files-in-mac-os-x/#comments</comments>
		<pubDate>Wed, 13 May 2009 06:44:52 +0000</pubDate>
		<dc:creator>Sam Bauers</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[etcetera]]></category>
		<category><![CDATA[hosts]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[Mac OS X]]></category>

		<guid isPermaLink="false">http://unlettered.org/?p=169</guid>
		<description><![CDATA[A while ago I wrote up a method for switching between /etc/hosts files. That post is pretty popular and clearly been useful to a few people. Since then I&#8217;ve actually extended this method into a tool that can manage multiple /etc/hosts files. Using this new method you can have a folder that you can add [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=unlettered.org&amp;blog=2087930&amp;post=169&amp;subd=unlettered&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A while ago I wrote up <a href="/2008/03/06/fast-switching-your-etchosts-file-in-mac-os-x/">a method for switching between <code>/etc/hosts files</code></a>. That post is pretty popular and clearly been useful to a few people.</p>
<p>Since then I&#8217;ve actually extended this method into a tool that can manage multiple <code>/etc/hosts</code> files. Using this new method you can have a folder that you can add hosts configurations to, and those files become available for switching into <code>/etc/hosts</code> straight away.</p>
<p>There&#8217;s no reason why an adaptation of this method wouldn&#8217;t work in Linux as well.</p>
<p>Here&#8217;s how you do it:</p>
<h3>1. Create the host configuration directory</h3>
<p>We&#8217;ll store all the <code>/etc/hosts</code> type files in <code>/usr/local/share/hs</code>.</p>
<p>You&#8217;ll need to do all of this as a root user. So switch to root using <code>sudo -s</code> first (you&#8217;ll need to be an administrator to do this).</p>
<pre><code>
$ mkdir -p /usr/local/share/hs
$ chmod 755 /usr/local/share/hs
</code></pre>
<p>Now to start with, let&#8217;s make a copy of the existing <code>/etc/hosts</code> file in this directory.</p>
<pre><code>
$ cp /etc/hosts /usr/local/share/hs/hosts.normal
$ chmod 744 /usr/local/share/hs/hosts.normal
</code></pre>
<p>Then make the real <code>/etc/hosts</code> a symbolic link to that new file.</p>
<pre><code>
$ ln -sf /usr/local/share/hs/hosts.normal /etc/hosts
</code></pre>
<p>Any additional host configurations in this folder need to be named using a similar <code>hosts.*</code> pattern.</p>
<h3>2. Create the host switching script</h3>
<p>Take the following bash script and save it to a file located at <code>/usr/local/bin/hs</code></p>
<p><pre class="brush: bash;">
#!/bin/bash

if [ $1 ]; then
	HFILE=hosts.$1
else
	HFILE=hosts.normal
fi

# Change this if you put your host files elsewhere
HPATH=&quot;/usr/local/share/hs/&quot;

echo &quot;Attempting to switch host file to $HPATH$HFILE&quot;

if [ ! -e $HPATH$HFILE ]; then
	echo &quot;--&gt; File does not exist!&quot;
	echo &quot;--&gt; Exiting with status 1&quot;
	exit 1
fi

if [ -d $HPATH$HFILE ]; then
	echo &quot;--&gt; File is a directory!&quot;
	echo &quot;--&gt; Exiting with status 2&quot;
	exit 2
fi

if [ -h $HPATH$HFILE ]; then
	echo &quot;--&gt; File is a symbolic link!&quot;
	echo &quot;--&gt; Exiting with status 3&quot;
	exit 3
fi

if [ ! -h /etc/hosts ]; then
	echo &quot;--&gt; Current hosts file is a real file!&quot;
	echo &quot;--&gt; Exiting with status 4&quot;
	exit 4
fi

# Link it up
sudo ln -sf $HPATH$HFILE /etc/hosts

# Flush the system-wide DNS cache
if [ -x /usr/bin/dscacheutil ]; then
	# Mac OS X 10.5
	/usr/bin/dscacheutil -flushcache
elif [ -x /usr/bin/lookupd ]; then
	# Mac OS X Less than 10.5
	/usr/bin/lookupd -flushcache
elif [ -x /usr/sbin/lookupd ]; then
	# Mac OS X Less than 10.5 (alternative)
	/usr/sbin/lookupd -flushcache
fi

echo &quot;--&gt; Done&quot;
</pre></p>
<p>The script does some basic validity checking on the file and also makes sure that <code>/etc/hosts</code> is just a symbolic link and not a real file.</p>
<p>You will need to adjust the permissions on this script so that only admins can run it.</p>
<pre><code>
$ chmod 750 /usr/local/bin/hs
</code></pre>
<p>You also need to make sure that <code>/usr/local/bin</code> is in your PATH so that the <code>hs</code> command can be found. We can test that.</p>
<pre><code>
$ echo $PATH
</code></pre>
<p>&#8230; will output something like &#8230;</p>
<pre><code>
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
</code></pre>
<p>If it&#8217;s not there then you need to add it in your users <code>~/.profile</code> file. Add the following line to that file, you can create the file if it isn&#8217;t there.</p>
<pre><code>
export PATH=/usr/local/bin:$PATH
</code></pre>
<p>Now reload your profile and the <code>hs</code> command should be available.</p>
<pre><code>
$ . ~/.profile
</code></pre>
<p>You could also just close and open your terminal window to reload your profile.</p>
<h3>3. Trying it out</h3>
<p>The <code>hs</code> command takes one argument. The argument is used to specify the hosts configuration file to be loaded from <code>/usr/local/share/hs</code>. If no argument is given, then the assumed file is <code>hosts.normal</code>.</p>
<p>So for our first test we should add another hosts configuration.</p>
<pre><code>
$ cp /usr/local/share/hs/hosts.normal /usr/local/share/hs/hosts.test
</code></pre>
<p>Edit the new hosts configuration file to your liking.</p>
<p>Now we can try the switching command to switch to that configuration. You will need to supply your password if you are not already running as root.</p>
<pre><code>
$ hs test
Attempting to switch host file to /usr/local/share/hs/hosts.test
Password:
--&gt; Done
</code></pre>
<p>The symbolic link at <code>/etc/hosts</code> should now be pointing to <code>/usr/local/share/hs/hosts.test</code>.</p>
<pre><code>
$ ls -l /etc/hosts
/etc/hosts -&gt; /usr/local/share/hs/hosts.test
</code></pre>
<p>Now try switching back to your normal configuration.</p>
<pre><code>
$ hs
Attempting to switch host file to /usr/local/share/hs/hosts.normal
Password:
--&gt; Done
</code></pre>
<p>Any more configurations that you add to <code>/usr/local/share/hs</code> are available this way.</p>
<p>Neat, huh?</p>
<h3>4. Fancy schmancy tab completion</h3>
<p>So that&#8217;s all great until you have 10 different hosts config files and you can&#8217;t remember their names.</p>
<p>The simple solution is to register additional commands so that command line tab completion can be used. The following additional script registers all the available hosts configs as separate commands. So instead of typing <code>hs test</code> we can type <code>hstest</code>. More importantly we can type <code>hs</code> then hit the &#8220;tab&#8221; key twice and get a full list of all available commands (one per configuration file).</p>
<p>Hard to explain, easier to just do. Add the following lines of code to the end of your <code>~/.profile</code> file.</p>
<p><pre class="brush: bash;">
# Setup hs* commands
HSPATH=/usr/local/share/hs
if [ -d $HSPATH ]; then
	HSFILES=(`ls $HSPATH`)
	HSCOUNT=${#HSFILES[*]}
	for (( HSI=0 ; $HSI &lt; $HSCOUNT ; HSI=$HSI+1 )); do
		HSFILE=${HSFILES[HSI]}
		HSFILE=${HSFILE:6}
		alias hs$HSFILE='hs '$HSFILE
	done
fi
unset HSPATH HSFILES HSCOUNT HSI HSFILE
</pre></p>
<p>This script loops through all the files in <code>/usr/local/share/hs</code> and adds a command for each. Once that is added, reload your profile to make the script work.</p>
<pre><code>
$ . ~/.profile
</code></pre>
<p>Now you should be able to type <code>hs</code> then hit the &#8220;tab&#8221; key twice and get a list of the available commands.</p>
<pre><code>
$ hs TAB TAB
hs            hsnormal      hstest
</code></pre>
<p>If you add any new configuration files to <code>/usr/local/share/hs</code> you need to reload your profile for it to turn up as a new command (or just relaunch your terminal window).</p>
<p>Enjoy!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/unlettered.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/unlettered.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/unlettered.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/unlettered.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/unlettered.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/unlettered.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/unlettered.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/unlettered.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/unlettered.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/unlettered.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/unlettered.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/unlettered.wordpress.com/169/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/unlettered.wordpress.com/169/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/unlettered.wordpress.com/169/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=unlettered.org&amp;blog=2087930&amp;post=169&amp;subd=unlettered&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://unlettered.org/2009/05/13/managing-multiple-etchosts-files-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>
	</item>
		<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&amp;blog=2087930&amp;post=51&amp;subd=unlettered&amp;ref=&amp;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&amp;blog=2087930&amp;post=51&amp;subd=unlettered&amp;ref=&amp;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>
