<?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>Technical Diary &#187; vpn</title>
	<atom:link href="http://andriigrytsenko.net/tag/vpn/feed/" rel="self" type="application/rss+xml" />
	<link>http://andriigrytsenko.net</link>
	<description>With Andrii Grytsenko</description>
	<lastBuildDate>Tue, 17 Aug 2010 08:25:33 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>OpenVPN connection between two points</title>
		<link>http://andriigrytsenko.net/2009/07/openvpn-connection-between-two-hosts/</link>
		<comments>http://andriigrytsenko.net/2009/07/openvpn-connection-between-two-hosts/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 13:57:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[openvpn]]></category>
		<category><![CDATA[vpn]]></category>

		<guid isPermaLink="false">http://andriigrytsenko.net/?p=341</guid>
		<description><![CDATA[We have two host which have to be connected by VPN connection. For our connect I am going to use OpenVPN software.

You can download it here. If you are going to look at OpenVPN more deeply than this post I would like to advice you get information from this source.
There is two machines:

 host node1 [...]]]></description>
			<content:encoded><![CDATA[<p>We have two host which have to be connected by VPN connection. For our connect I am going to use OpenVPN software.</p>
<p><span id="more-341"></span><br />
You can download it <a href="http://openvpn.net/download.html">here</a>. If you are going to look at OpenVPN more deeply than this post I would like to advice you get information from this <a href="http://openvpn.net/index.php/open-source/documentation.html">source</a>.</p>
<p>There is two machines:</p>
<ul>
<li> host node1 server side with debian on board (10.0.2.2)</li>
<li> host node2 client side with centos  (without visible for node1  IP address )</li>
</ul>
<p>First, install and setting up server side:</p>
<pre>node1# apt-get install openvpn
node1# cd /usr/share/doc/openvpn/examples/easy-rsa/2.0
node1# . ./vars</pre>
<p>On next step you will be prompted to ask on simple question before your certificates be generated.</p>
<pre>node1# ./build-ca</pre>
<p>Next generate certificate for server :</p>
<pre>node1# ./build-key-server node1</pre>
<p>I left password request line empty.</p>
<p>Next generate certificate for client:</p>
<pre>node1# ./build-key node2</pre>
<p>The  same actions as for previous step exclude &#8220;Common name&#8221;. Its should be the same to you hostname.</p>
<p>Now generate Diffie Hellman parameters</p>
<pre>node1# ./build-dh</pre>
<p>Here is table about where should be stored certificates and keys files. Which was got by me from official documentation.</p>
<table border="1" cellspacing="0" cellpadding="8">
<tbody>
<tr>
<td><strong>Filename</strong></td>
<td><strong>Needed By</strong></td>
<td><strong>Purpose</strong></td>
<td><strong>Secret</strong></td>
</tr>
<tr>
<td>ca.crt</td>
<td>server + all clients</td>
<td>Root CA certificate</td>
<td>NO</td>
</tr>
<tr>
<td>ca.key</td>
<td>key signing machine only</td>
<td>Root CA key</td>
<td>YES</td>
</tr>
<tr>
<td>dh{n}.pem</td>
<td>server only</td>
<td>Diffie Hellman parameters</td>
<td>NO</td>
</tr>
<tr>
<td>server.crt</td>
<td>server only</td>
<td>Server Certificate</td>
<td>NO</td>
</tr>
<tr>
<td>server.key</td>
<td>server only</td>
<td>Server Key</td>
<td>YES</td>
</tr>
<tr>
<td>client1.crt</td>
<td>client1 only</td>
<td>Client1 Certificate</td>
<td>NO</td>
</tr>
<tr>
<td>client1.key</td>
<td>client1 only</td>
<td>Client1 Key</td>
<td>YES</td>
</tr>
<tr>
<td>client2.crt</td>
<td>client2 only</td>
<td>Client2 Certificate</td>
<td>NO</td>
</tr>
<tr>
<td>client2.key</td>
<td>client2 only</td>
<td>Client2 Key</td>
<td>YES</td>
</tr>
<tr>
<td>client3.crt</td>
<td>client3 only</td>
<td>Client3 Certificate</td>
<td>NO</td>
</tr>
<tr>
<td>client3.key</td>
<td>client3 only</td>
<td>Client3 Key</td>
<td>YES</td>
</tr>
</tbody>
</table>
<p>!!! According to this make sure that all you files located at properly place.</p>
<p>Copy sample config file into the /etc/openvpn:</p>
<pre>node1:# cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/
node1:# cd /etc/openvpn/
node1:# gunzip server.conf.gz</pre>
<p>Also keys and certificates should be copied too:</p>
<pre>node1:# cp /usr/share/doc/openvpn/examples/easy-rsa/2.0/keys/ca.* .
node1:# cp /usr/share/doc/openvpn/examples/easy-rsa/2.0/keys/dh1024.pem .
node1:# cp /usr/share/doc/openvpn/examples/easy-rsa/2.0/keys/node1* .</pre>
<p>Comment out or change value for next lines:</p>
<pre>local 10.0.2.2
cert node1.crt
key node1.key
user nobody
group nogroup
log /var/log/openvpn.log # in this case openvpn doesn't use syslog daemon for logging</pre>
<p>Now it&#8217;s time to start our daemon.</p>
<pre>node1:# /etc/init.d/openvpn start
Starting virtual private network daemon: server.</pre>
<p>If no errors occurred. Check whether interface is up:</p>
<pre>node1:# /sbin/ifconfig tun0
tun0      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
inet addr:10.8.0.1  P-t-P:10.8.0.2  Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)</pre>
<p>Also it&#8217;s a good idea to check /var/log/openvpn.</p>
<p>Don&#8217;t forget to put daemon start into the boot:</p>
<pre>node1:# update-rc.d openvpn defaults</pre>
<p>Looks like good for server side. Go to the client.</p>
<p>Before you start to install openvpn make sure that next software have been already installed at your box.</p>
<ol>
<li><em>openssl</em></li>
<li><em>lzo</em></li>
<li><em> </em><em>pam</em></li>
</ol>
<p>In my CentOS repository I could not find lzo package :( . Therefore I had to compile it from sources:</p>
<pre>[root@node2]#  wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.03.tar.gz
[root@node2]# tar -xzvf lzo-2.03.tar.gz
[root@node2]# ./configure --enable-shared
[root@node2]# make
[root@node2]# make install</pre>
<p>Now add new path to library&#8217;s paths:</p>
<pre>[root@node2]# echo "/usr/local/lib" &gt; /etc/ld.so.conf.d/lzo.conf
[root@node2]# ldconfig</pre>
<p>Get latest openvpn version from site :</p>
<pre>[root@node2]# wget http://dag.wieers.com/rpm/packages/openvpn/openvpn-2.0.9-1.el5.rf.i386.rpm
[root@node2]# rpm -Uhv --nodeps openvpn-2.0.9-1.el5.rf.i386.rpm</pre>
<p>After this copy config file and edit it:</p>
<pre>[root@node2]# cp /usr/share/doc/openvpn-2.0.9/sample-config-files/client.conf /etc/openvpn/</pre>
<p>copy clients cert and key from server:</p>
<pre>[root@node2]cd /etc/openvpn/
[root@node2]# scp root@node1:/usr/share/doc/openvpn/examples/easy-rsa/2.0/keys/node2* .</pre>
<p>Next lines should be edited:</p>
<pre>remote 10.0.2.2 1194
user nobody
group nobody
cert node2.crt
key node2.key</pre>
<p>Now system is ready to start:</p>
<pre>[root@node2]# /etc/init.d/openvpn start
Starting openvpn:                                          [  OK  ]</pre>
<p>To check is everithing works fine. Identify ip address of virtual interface:</p>
<pre>[root@node2]# ifconfig tun0
tun0      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
inet addr:10.8.0.6  P-t-P:10.8.0.5  Mask:255.255.255.255
&lt; skipped &gt;</pre>
<p>As you can see it&#8217;s 10.8.0.6. Now try to ping it from server side :</p>
<pre>node1:# ping 10.8.0.6
PING 10.8.0.6 (10.8.0.6) 56(84) bytes of data.
64 bytes from 10.8.0.6: icmp_seq=1 ttl=64 time=0.692 ms</pre>
<p>If you all checks done successful than put script into the autoload:</p>
<pre>[root@node2]# chkconfig --level 35 openvpn on</pre>
<p>To double check it reboot both systems and check that connection is established.</p>
<h3><strong>UPDATE:</strong></h3>
<p>Sometimes you face with the problem when your server located at client side(yes, it&#8217;s really happens time to time) . Therefore you should have some mechanism to keep your connection alive all time. I use next script to do this(please pay attention on red parts &#8211; they must be changed to your conditions):</p>
<pre>#!/bin/bash

function start_vpn {
        cd <span style="color: #ff0000;">/path/where/client.conf/is/located</span>
        sudo /usr/sbin/openvpn --config client.conf &amp;&gt;/dev/null &amp;
        echo "vpn was started at `date +%H:%M:%S' '%d/%m/%y`" &gt;&gt; /var/log/vpn.log
}

server_ext_ip=<span style="color: #ff0000;">/you server external ip/</span>
server_int_ip=<span style="color: #ff0000;">/you server internal ip/</span>
avaib=$(nc -w 5 -z $server_ext_ip 80 &amp;&gt;/dev/null; echo $?)

if [ $avaib -eq 0 ] &amp;&amp; [ ! ping -c 5 $server_int_ip &amp;&gt;/dev/null ]; then
    if ! ps uax | grep -v grep| grep openvpn &amp;&gt;/dev/null; then
        start_vpn
    else
        pid=$(ps uax | grep -v grep| grep openvpn| awk '{print $2}')
        sudo kill $pid
        start_vpn
    fi
fi</pre>
<p>Please make sure that this line is commented out in your /etc/sudoers:</p>
<pre>#Defaults    requiretty</pre>
<p>It&#8217;s makes your sudo works without tty(mean from crontab).</p>
<p>And add new entry to your crontab to run check every minutes.</p>
<pre>crontab -l
*/1 * * * *     /path/to/open.sh</pre>
]]></content:encoded>
			<wfw:commentRss>http://andriigrytsenko.net/2009/07/openvpn-connection-between-two-hosts/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
