<?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/"
	>

<channel>
	<title>RPS &#187; ssh</title>
	<atom:link href="http://www.delic.com/blog/tag/ssh/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.delic.com/blog</link>
	<description>The view from here</description>
	<lastBuildDate>Tue, 22 Feb 2011 12:01:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>VPN for OSX client via SSH and the tun device driver</title>
		<link>http://www.delic.com/blog/2009/05/12/vpn-for-osx-client-via-ssh-and-the-tun-device-driver/</link>
		<comments>http://www.delic.com/blog/2009/05/12/vpn-for-osx-client-via-ssh-and-the-tun-device-driver/#comments</comments>
		<pubDate>Tue, 12 May 2009 22:18:05 +0000</pubDate>
		<dc:creator>rshah21</dc:creator>
				<category><![CDATA[Researching]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[os x]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[tun]]></category>
		<category><![CDATA[vpn]]></category>

		<guid isPermaLink="false">http://www.delic.com/blog/?p=82</guid>
		<description><![CDATA[For a long time, I have used SSH port forwarding to access a lot of my home files and services, but I&#8217;ve found myself hitting the limits of usefulness. So I decided to try and create a VPN. This isn&#8217;t a true VPN &#8211; it&#8217;s NAT over SSH with tun. Good enough for me. Much [...]]]></description>
			<content:encoded><![CDATA[<p>For a long time, I have used SSH port forwarding to access a lot of my home files and services, but I&#8217;ve found myself hitting the limits of usefulness.   So I decided to try and create a VPN.  This isn&#8217;t a true VPN &#8211; it&#8217;s NAT over SSH with tun.  Good enough for me.<br />
<span id="more-82"></span></p>
<p class="alert">Much of the heavy lifting for this post was done by daleroberts at his <a href="http://wiki.daleroberts.info/doku.php?id=technical:unix:mac_os_x:vpn_with_ssh">wiki site</a>.  My only real contribution here is around the last steps of getting the routing working.</p>
<p>So, here&#8217;s what you&#8217;ll need:</p>
<ol>
<li>Two macs &#8211; one is the remote &#8220;client&#8221; and one is the home &#8220;server&#8221;.  Both should be running OS X 10.4.10+ <em>client</em>.  If you have OS X Server, I&#8217;d suggest looking around for OpenVPN. </li>
<li>Both machines connected to the network.  You&#8217;ll probably want to use a service like DynDNS so you can access the server machine via a hostname, especially if your ISP uses dynamic IP addresses </li>
<li> Your should know how to SSH to your home server</li>
<li>Your should know how to enable and use the root account on both your remote machine and home server</li>
<li>You should know how to handle yourself on the command line via Terminal. </li>
</ol>
<h3>Conventions used</h3>
<ul>
<li>The remote &#8220;client&#8221; (aka. the client) is the machine you are VPN-ing from.  It is off the target network (aka. the home network).  This is the machine you&#8217;re sitting at saying &#8220;I wish I had access to this file on my home network&#8221;</li>
<li>The home &#8220;server&#8221; (aka. the server) is the machine you are VPN-ing to.  It serves as the gateway to all the other machines on your home network. </li>
<li>For the purposes of this example, the &#8220;home network&#8221; has an IP of ranges 192.168.0.1 to 192.168.0.255 &#8211; which will be notated as 192.168.0.0/24</li>
<li>I have used en0 as the interface on both the client and server machines &#8211; this is the Ethernet inteface.  If either your client or server are connected via another mechanism (e.g. Airport), then you will need to use the appropriate interface at the appropriate time.   This is only applicable to the client and server &#8211; if machines on your home network are connected wirelessly, etc. it should not be an issue.
</ul>
<h3>Getting the Tun drivers setup</h3>
<p> Even though OS X has a tun manpage, it appears to not actually have tun installed.  The easiest way to get the tun/tap drivers is to download <a href="http://tunnelblick.net">tunnelblick</a>.  You will need it both on the client and server machines.  Once you have it, mount the disk image, and copy the application somewhere (for illustrative purposes, I put it on my Desktop).</p>
<p>At the command line, execute the following (assuming you are in your home dir):</p>
<p class="code"> sudo cp -rp Desktop/Tunnelblick.app/Contents/Resources/*kext /System/Library/Extensions/</p>
<p class="code">cd /System/Library/Extensions </p>
<p class="code">sudo chown -R root:wheel tun.kext </p>
<p class="code">sudo chmod -R go-w tun.kext </p>
<p class="code">sudo chown -R root:wheel tap.kext </p>
<p class="code">sudo chmod -R go-w tap.kext </p>
<p class="code">sudo kextload /System/Library/Extensions/tun.kext</p>
<p>Repeat the above on the server</p>
<h3>The first SSH connection</h3>
<p>Edit /private/etc/sshd_config on the server as root to permit tunnelling.  Make sure the following lines are uncommented (in a default OS X install, they should be there, but will be commented out with a &#8216;#&#8217; sign &#8211; you just need to delete the &#8216;#&#8217;)</p>
<p class="information">
PermitRootLogin yes<br />
PermitTunnel yes
</p>
<p>Then, from the client, execute:</p>
<p class="code">
sudo ssh -w 0:0 root@home while true \; do echo . \&#038;\&#038; sleep 60 \; done
</p>
<p>Replace &#8220;home&#8221; with the internet available hostname of the server.  The output should be a &#8220;period&#8221;, with a new &#8220;period&#8221; every 60 seconds.<br />
If this worked correctly, you should be able to open a new command prompt on the client and execute the following:</p>
<p class="code">ifconfig tun0</p>
<p>which should return</p>
<p class="information">
tun0: flags=8851<UP,POINTOPOINT,RUNNING,SIMPLEX,MULTICAST> mtu 1500<br />
        open (pid ABCDE)
</p>
<p>where ABCDE isn&#8217;t really relevant.  If you execute the same &#8216;ifconfig tun0&#8242; on the server, you should see something similar.</p>
<h3>Setting up the VPN</h3>
<p>Execute the following on the client:</p>
<p class="code">sudo ifconfig tun0 172.16.0.1 172.16.0.2</p>
<p class="code">ssh root@home ifconfig tun0 172.16.0.2 172.16.0.1 \&#038;\&#038; sysctl -w net.inet.ip.forwarding=1</p>
<p>Again, replacing &#8220;home&#8221; with  the internet available hostname of the server.<br />
To test this, add the route on your client</p>
<p class="code">sudo route add -net 172.16.0 -interface tun0</p>
<p>You should now be able to access your server from your client via the IP address 172.16.0.2.  Congrats, you now have a basic VPN</p>
<h3> NAT </h3>
<p>So, what if you have more machines on your home network than just your server?  You&#8217;ll need to set up NAT and some routing rules<br />
First, a route to push traffic to 172.16.0.* via the tun0 interface:</p>
<p class="code">sudo route add -net 172.16.0 -interface tun0</p>
<p>Second, a route to push traffic headed to the home network via the 172.16.0.2 gateway</p>
<p class="code">sudo route add 192.168.0 172.16.0.2</p>
<p>Finally, you need to set up NAT on the server:</p>
<p class="code">sudo /usr/sbin/natd -interface en0 -l -s -m</p>
<p class="code">sudo ipfw add 00002 allow ip from any to any via tun0</p>
<p class="code">sudo ipfw add 00003 divert 8668 ip from any to 192.168.0/24 via en0</p>
<p class="code">sudo ipfw add 00004 allow ip from any to 172.16.0.1</p>
<p>You should now be able to access any of the machines on the home network by IP address!</p>
<h3>Getting it running the next time</h3>
<p>I have setup some aliases and scripts so I don&#8217;t have to remember the exact commands to execute.  This is probably not the most efficient way to do this, but it&#8217;s better than nothing.</p>
<ol>
<li>(Optional) If NAT is not running on the server, you will need to get it setup.  I have dumped the server commands from above into a shell script and just ssh into the box, and execute the script</li>
<li>Execute the first ssh tunnel on the client: &#8220;sudo ssh -w 0:0 root@home while true \; do echo . \&#038;\&#038; sleep 60 \; done&#8221;  Leave it running</li>
<li>Execute the second ssh tunnel on the client: &#8220;sudo ifconfig tun0 172.16.0.1 172.16.0.2; sudo ssh root@home ifconfig tun0 172.16.0.2 172.16.0.1 \&#038;\&#038; sysctl -w net.inet.ip.forwarding=1&#8243;  If successful, this will return the prompt to you</li>
<li>Setup the routes on the client: &#8220;sudo route add -net 172.16.0 -interface tun0; sudo route add 192.168.0 172.16.0.2&#8243;  The VPN is running</li>
</ol>
<ol> When you&#8217;re done</p>
<li>Remove the routes on the client: &#8220;sudo route delete 172.16.0; sudo route delete 192.168.0&#8243;</li>
<li>Kill the running ssh client with a Control-C</li>
<p>That&#8217;s it.  I&#8217;m sure some people will comment on how this could be made better, but it&#8217;s meant to be quick and dirty but work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.delic.com/blog/2009/05/12/vpn-for-osx-client-via-ssh-and-the-tun-device-driver/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.866 seconds -->

