Wed, 23 Feb 2011

Configure openvpn on a Debian server

These notes cover the installation of openvpn on a Debian server and client.
Once setup, all internet traffic, including browser traffic, from the client
will travel via the VPN through the server. The server config write-up is
first, followed by the client write-up further down the page.

This presumes you are not ethernet bridging.

Begin by installing openvpn on both your server and your client.

# aptitude install openvpn

Switch to your server.

[server config]

First you must create the keys needed by both server and client.

# mkdir /etc/openvpn/easy-rsa
# cp -ai /usr/share/doc/openvpn/examples/easy-rsa/2.0/ /etc/openvpn/easy-rsa
# cd /etc/openvpn/easy-rsa/

# vi vars

In the vars file, edit the KEY_* entries at the bottom of the file, such as KEY_COUNTRY,
KEY_ORG, KEY_EMAIL, etc. Next, source the vars file and then clean the directory.

# . ./vars
# ./clean-all

Next build the certificates. For the 'Common Name' field, you can use anthying
to your liking. I used 'OpenVPN-CA-rustybear'. For the Certificate Authority
(build-ca), use 'server'. For the client keys (build-key), use 'client1' or
'client2' or whatever you like, I used 'client_kevin'.

# ./build-ca
# ./build-key-server server
# ./build-key client_kevin
# ./build-key client2

Generate the Diffie Hellman parameters for the server.

# .build-dh

When this is done, you will have a number of files in the keys/ subdirectory.
Copy the keys listed below to the server's /etc/openvpn directory.

# cd /etc/openvpn
# cp easy-rsa/keys/ca.crt .
# cp easy-rsa/keys/server.key .
# cp easy-rsa/keys/server.crt .
# cp easy-rsa/keys/dh1024.pem .

And copy the keys needed for the client either directly to the client via scp
or to a USB disk. The files needed by the client are ca.crt, client_kevin.crt,
and client_kevin.key (or whatever you named the files when you generated them
with the build-key script).

On the client machine, copy the client keys to the /etc/openvpn directory.

Next, still on the server, create the openvpn server config file. Start with
the example in the docs.

# cd /etc/openvpn
# cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf .

Gunzip it if necessary then edit it. Here's a simple but workable example:

[server.conf]
port 1194
proto udp
dev tun
ca /etc/openvpn/ca.crt
cert /etc/openvpn/server.crt
key /etc/openvpn/server.key
dh /etc/openvpn/dh1024.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 202.107.105.13"
push "dhcp-option DNS 202.108.107.21"
keepalive 10 120
comp-lzo
user nobody
group nogroup
persist-key
persist-tun
status openvpn-status.log
verb 3

Note the entries for 'push dhcp-option DNS'. These will be DNS servers that
are accessible from your server. They will be pushed out to the client.

Now start the openvpn server with either of the following commands.

# /etc/init.d/openvpn start
or
# openvpn /etc/openvpn/server.conf

You will need to enable IP forwarding.

# echo 1 > /proc/sys/net/ipv4/ip_forward

You can make this a permanent change by uncommenting the line:

net.ipv4.ip_forward = 1

in the file /etc/sysctl.conf.

You'll also have to allow NAT forwarding through your firewall. This will most
likely be accomplished with something like the following rule in iptables:

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

This assumes you have set up your openvpn server with the IP 10.8.0.0 in the
server.conf file as described above.

Next, the client must be set up.

=====

[client config]

In the server config above, you created keys for the client, which you should
have already copied to /etc/openvpn. This includes the ca.crt file.

Next you need a client.conf file, a sample of which is found in the docs.

# cd /etc/openvpn
# cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf .
# vi client.conf

[client.conf]
client
dev tun
proto udp
remote 66.32.272.181 1194
resolv-retry infinite
nobind
user nobody
group nogroup
persist-key
persist-tun
mute-replay-warnings
ca /etc/openvpn/ca.crt
cert /etc/openvpn/client_kevin.crt
key /etc/openvpn/client_kevin.key
ns-cert-type server
comp-lzo
verb 3
up /etc/openvpn/update-resolv-conf
down /etc/openvpn/update-resolv-conf

Some obvious things: You'll want to use your server's IP for the remote entry.
List your client keys and the server CA. Uncomment the user and group entries.

Not so obvious are the last two lines.  These are the key to getting DNS to
work correctly on the client. You can check the README.Debian in the docs, but
basically you need to install the deb package resolvconf. Make sure you read
the README for resolvconf, as it can potentially conflict with other DNS
writing programs on your client.

The last two lines call the script update-resolv-conf, which should be in your
/etc/openvpn directory. The script will use resolvconf, and the DNS settings
of the openvpn server, to rewrite your client resolv.conf file.

To start openvpn on the client, issue the command:

# openvpn --script-security 2 --config /etc/openvpn/client.conf &

You'll need the --script-security setting to get the update-resolv-conf script
to execute.

Check your installation by pinging 10.8.0.1 from the client. You should
successfully be pinging the server. Check it further by opening a browser and
going to http://www.whatismyip.com. It should return the IP of the server, not
the client.  Note also that if you run the command ifconfig, you'll see a new
entry for tun0.


Posted at: 01:20 | category: /configure | Comments ()