Configure openvpn on a Debian server and client

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 to 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 configuration

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/2.0
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 anything 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/2.0/keys/ca.crt .
cp easy-rsa/2.0/keys/server.key .
cp easy-rsa/2.0/keys/server.crt .
cp easy-rsa/2.0/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).

Switching to the client machine for just a moment, copy the client keys to the /etc/openvpn directory.

Next, back 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:

   1 # [server.conf]
   2 port 1194
   3 proto udp
   4 dev tun
   5 ca /etc/openvpn/ca.crt
   6 cert /etc/openvpn/server.crt
   7 key /etc/openvpn/server.key
   8 dh /etc/openvpn/dh1024.pem
   9 server 10.8.0.0 255.255.255.0
  10 ifconfig-pool-persist ipp.txt
  11 push "redirect-gateway def1 bypass-dhcp"
  12 push "dhcp-option DNS 202.107.105.13"
  13 push "dhcp-option DNS 202.108.107.21"
  14 keepalive 10 120
  15 comp-lzo
  16 user nobody
  17 group nogroup
  18 persist-key
  19 persist-tun
  20 status openvpn-status.log
  21 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 configuration

In the server config above, you created keys for the client, which you should have already copied from the server to the client's directory at /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

   1 # [client.conf]
   2 client
   3 dev tun
   4 proto udp
   5 remote 66.32.272.181 1194
   6 resolv-retry infinite
   7 nobind
   8 user nobody
   9 group nogroup
  10 persist-key
  11 persist-tun
  12 mute-replay-warnings
  13 ca /etc/openvpn/ca.crt
  14 cert /etc/openvpn/client_kevin.crt
  15 key /etc/openvpn/client_kevin.key
  16 ns-cert-type server
  17 comp-lzo
  18 verb 3
  19 up /etc/openvpn/update-resolv-conf
  20 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 should check the README.Debian in the openvpn 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. You can place this setting in the client.conf file if you like.

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.

On both the server and the client, you can control whether your vpn is automatically started on machine startup by editing the AUTOSTART lines in the file /etc/default/openvpn.

-- KevinCoyner 2011-02-23 08:07:04