How To Configure OpenVPN

From Sfvlug

Setting up and running OpenVPN is pretty easy. You just have to understand a couple principles and set up the certificates. So let's start with the basics.

Contents

TUN And TAP Networking

Running OpenVPN requires you to either set up a TUN or TAP network interface. These are both fairly similar but there is a critical difference and I'll try to explain it as best as I can.

You can think of both of these as network interfaces that are attached to a program in userspace. With them, the program is able to interact with the rest of the network as if it were its own node on the network. In the case of OpenVPN, this is very useful because it allows OpenVPN to send encrypted packets over your real network interface, but it encrypts and decrypts packets entering or leaving the tun or tap interface. So OpenVPN gets to manipulate the cryptography around the packets on its tun or tap interface and use the machine's real interface for sending the packets just like any other client or server program.

TUN
A tun interface operates at layer 3. This means it can encapsulate packets at the IP address layer. Use a tun interface if you want to route packets between VPN endpoints. That means both sides of the VPN will use different IP address spaces and use the VPN to route between the two networks. This tutorial will be using the tun interface. It is also worth mentioning that OpenBSD only supports tun interfaces.
TAP
A tap interface operates at layer 2. This means it can encapsulate entire ethernet frames. Use a tap interface when you want to bridge packets between VPN endpoints. That means packets on either side of the VPN will be from the same address space. Hopefully that will mean you can avoid NAT, if both sides of the VPN have all unique addresses.

X.509 Certificates

We have all heard of SSL certificates. They are the implementation of the X.509 protocol. Let's look at what we need.

When seting up OpenVPN, I create my own Certificate Authority (CA). This is just a self-signed certificate with a very long expiration period. In general, generating any X.509 results in three files. First, there is the private key. This is a secret file which contains some important random numbers. Second, from this file I generate what is called a Certificate Signing Request (sometimes a CSR or req file). The CSR is an unsigned public key, it contains no secrets. However, it contains the Subject which will identify the user of the certificate. Third, I use the CA to sign the CSR. This results in a Certificate, which is a signed public key. Certificates also contain no secrets. They are just the CSR which contains the Subject and a cryptographic signature generated using the CA's key to make a special checksum. The idea is, instead of sharing your public key with everyone you want to communicate, every participant only needs a copy of the CA's public key or Certificate. Each participant can validate the signature of any certificate any other participant tries to share.

Key
Private key
CSR
Unsigned public key
Cert
Signed public key

Installing OpenVPN

I am going to assume that you will be using the precompiled package of OpenVPN which every distro provides. I see no good reason to compile it from source.

We are going to configure OpenVPN with one server and multiple road-warrior style clients. This just means that the server will always have the same IP address, or at least DNS name, and will always be on, and that the clients will come and go as they please and won't have the same IP addresses or DNS names.

The server is going to provide routing to its entire network. Let's assume the server is the gateway to a LAN, and that LAN's IP space is 192.168.1.0/24, and the router which runs OpenVPN has the address 192.168.1.1. Since we are using TUN networking, we need to use an interim network to route over. Let's use 172.16.1.0/24 for this space. OpenVPN is actually going to break this space down into multiple slash-30 networks, each containing four addresses -- two usable, one network address and one broadcast address -- and it will use up the first of these so in reality we will have room for only 63 clients in this scenario. The first client to connect gets a network that looks like this:

172.16.1.4 network
172.16.1.5 server
172.16.1.6 client
172.16.1.7 broadcast

The clients won't necessarily provide routing to their networks so this means they are participating in a one-to-many VPN. Therefore we don't care what the network space our clients are in is, so long as it is possible to cleanly route to the server's network. That means no IP conflicts in the two networks. It is possible to create a many-to-many VPN with OpenVPN, but we just won't be doing that in this example.

Configure The Server

Every installation of OpenVPN I have installed (on Linux) has always used /etc/openvpn to store the configuration. On Windows or MacOS it is wherever you installed it, and on FreeBSD and OpenBSD it's /usr/local/etc/openvpn. We'll start with the server configuration file. For the server I left all the comments in the file. There are example configuration files in /usr/share/doc. Create the following as /etc/openvpn/server.conf:

# Which local IP address should OpenVPN
# listen on? (optional)
;local a.b.c.d

It is probably not necessary to specify a particular address to listen on. Only use this if you definitely don't want OpenVPN to listen on every address.

# Which TCP/UDP port should OpenVPN listen on?
# If you want to run multiple OpenVPN instances
# on the same machine, use a different port
# number for each one.  You will need to
# open up this port on your firewall.
port 1194

Port 1194 is the registered port for OpenVPN. In actual practice, I have never noticed any significant attempts to crack or otherwise abuse the default port out on the wild Internet. There are probably two reasons for this. First, since UDP is a connectionless protocol, it's impossible to tell if the port is open or filtered using normal port-scanning like nmap. And second, since authentication is via X.509 certificates which are typically thousands of bits, the likelihood of successfully guessing the correct signature to gain access is astronomically near impossible.

# TCP or UDP server?
;proto tcp
proto udp

Use UDP for your VPN. Tunneling TCP over TCP can be less reliable. As previously stated, it's impossible to differentiate an open and a filtered UDP port. And the connection-oriented nature of TCP doesn't actually buy you anything in this situation.

# "dev tun" will create a routed IP tunnel,
# "dev tap" will create an ethernet tunnel.
# Use "dev tap0" if you are ethernet bridging
# and have precreated a tap0 virtual interface
# and bridged it with your ethernet interface.
# If you want to control access policies
# over the VPN, you must create firewall
# rules for the the TUN/TAP interface.
# On non-Windows systems, you can give
# an explicit unit number, such as tun0.
# On Windows, use "dev-node" for this.
# On most systems, the VPN will not function
# unless you partially or fully disable
# the firewall for the TUN/TAP interface.
;dev tap
dev tun

As stated previously, we're using TUN networking for this example. The comment mentions firewalls, and we'll cover iptables a little later.

# Windows needs the TAP-Win32 adapter name
# from the Network Connections panel if you
# have more than one.  On XP SP2 or higher,
# you may need to selectively disable the
# Windows firewall for the TAP adapter.
# Non-Windows systems usually don't need this.
;dev-node MyTap

The preceding section should be irrelevant unless you're using Windows. Running Windows as a server is just stupid; using it as a gateway just makes no sense at all.

# SSL/TLS root certificate (ca), certificate
# (cert), and private key (key).  Each client
# and the server must have their own cert and
# key file.  The server and all clients will
# use the same ca file.
#
# See the "easy-rsa" directory for a series
# of scripts for generating RSA certificates
# and private keys.  Remember to use
# a unique Common Name for the server
# and each of the client certificates.
#
# Any X509 key management system can be used.
# OpenVPN can also use a PKCS #12 formatted key file
# (see "pkcs12" directive in man page).
ca ca.crt
cert server.crt
key server.key  # This file should be kept secret

All files are going to be relative to the configuration file unless otherwise qualified. Generating each of these will be covered below.

# Diffie hellman parameters.
# Generate your own with:
#   openssl dhparam -out dh2048.pem 2048
dh dh2048.pem

The server should include a Diffie-Hellman parameter. Use the command shown above to generate it. This file improves security and makes the tunnel harder to decrypt. Generating a Diffie-Hellman parameter is very time consuming; it can take several minutes to generate a two-thousand bit parameter. Be patient, it is not worth generating a weak parameter file just to save a little time.

# Network topology
# Should be subnet (addressing via IP)
# unless Windows clients v2.0.9 and lower have to
# be supported (then net30, i.e. a /30 per client)
# Defaults to net30 (not recommended)
;topology subnet

As stated, this parameter shouldn't be necessary unless you have to support outdated Windows software. OpenVPN is open source, just download a more recent copy why don'tcha?

# Configure server mode and supply a VPN subnet
# for OpenVPN to draw client addresses from.
# The server will take 10.8.0.1 for itself,
# the rest will be made available to clients.
# Each client will be able to reach the server
# on 10.8.0.1. Comment this line out if you are
# ethernet bridging. See the man page for more info.
;;server 10.8.0.0 255.255.255.0
server 172.16.1.0 255.255.255.0

This is where we configure the pool of addresses which will be used by the client-to-server connections as illustrated above.

# Maintain a record of client <-> virtual IP address
# associations in this file.  If OpenVPN goes down or
# is restarted, reconnecting clients can be assigned
# the same virtual IP address from the pool that was
# previously assigned.
ifconfig-pool-persist ipp.txt

If you include the above parameter, then each client will get the same IP address from the server pool above each time it reconnects. You can take advantage of this to implement client-specific firewall rules.

# Configure server mode for ethernet bridging.
# You must first use your OS's bridging capability
# to bridge the TAP interface with the ethernet
# NIC interface.  Then you must manually set the
# IP/netmask on the bridge interface, here we
# assume 10.8.0.4/255.255.255.0.  Finally we
# must set aside an IP range in this subnet
# (start=10.8.0.50 end=10.8.0.100) to allocate
# to connecting clients.  Leave this line commented
# out unless you are ethernet bridging.
;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100

You would only need to use this parameter if you are using a TAP interface and bridging your networks.

# Configure server mode for ethernet bridging
# using a DHCP-proxy, where clients talk
# to the OpenVPN server-side DHCP server
# to receive their IP address allocation
# and DNS server addresses.  You must first use
# your OS's bridging capability to bridge the TAP
# interface with the ethernet NIC interface.
# Note: this mode only works on clients (such as
# Windows), where the client-side TAP adapter is
# bound to a DHCP client.
;server-bridge

Again, this parameter is TAP networking and bridge specific.

# Push routes to the client to allow it
# to reach other private subnets behind
# the server.  Remember that these
# private subnets will also need
# to know to route the OpenVPN client
# address pool (10.8.0.0/255.255.255.0)
# back to the OpenVPN server.
;push "route 192.168.10.0 255.255.255.0"
;push "route 192.168.20.0 255.255.255.0"

If your OpenVPN server is a gateway to multiple subnets, you can push additional routes to the clients.

# To assign specific IP addresses to specific
# clients or if a connecting client has a private
# subnet behind it that should also have VPN access,
# use the subdirectory "ccd" for client-specific
# configuration files (see man page for more info).

# EXAMPLE: Suppose the client
# having the certificate common name "Thelonious"
# also has a small subnet behind his connecting
# machine, such as 192.168.40.128/255.255.255.248.
# First, uncomment out these lines:
;client-config-dir ccd
;route 192.168.40.128 255.255.255.248
# Then create a file ccd/Thelonious with this line:
#   iroute 192.168.40.128 255.255.255.248
# This will allow Thelonious' private subnet to
# access the VPN.  This example will only work
# if you are routing, not bridging, i.e. you are
# using "dev tun" and "server" directives.

# EXAMPLE: Suppose you want to give
# Thelonious a fixed VPN IP address of 10.9.0.1.
# First uncomment out these lines:
;client-config-dir ccd
;route 10.9.0.0 255.255.255.252
# Then add this line to ccd/Thelonious:
#   ifconfig-push 10.9.0.1 10.9.0.2

All of the above examples are to give a particular host specific configurations. In a simple scenario such as ours, it is not necessary.

# Suppose that you want to enable different
# firewall access policies for different groups
# of clients.  There are two methods:
# (1) Run multiple OpenVPN daemons, one for each
#     group, and firewall the TUN/TAP interface
#     for each group/daemon appropriately.
# (2) (Advanced) Create a script to dynamically
#     modify the firewall in response to access
#     from different clients.  See man
#     page for more info on learn-address script.
;learn-address ./script

As the comments state, if you want to modify your firewall each time a particular client connects, in some particular way, then you could use a script to do it. Personally, I think you should have your firewall rules pre-set and use the above ipp.txt file to assign the client the expected address. If you have more clients than your IP pool allows to assign each of them a separate address, then use this script to add their address to an IPSet, which will be matched by rules you already have established. Modifying your firewall rules dynamically by a script can be a recipe for disaster and it's very difficult to thoroughly test all the possible outcomes with a large number of modifications.

# If enabled, this directive will configure
# all clients to redirect their default
# network gateway through the VPN, causing
# all IP traffic such as web browsing and
# and DNS lookups to go through the VPN
# (The OpenVPN server machine may need to NAT
# or bridge the TUN/TAP interface to the internet
# in order for this to work properly).
;push "redirect-gateway def1 bypass-dhcp"

Some people want to configure OpenVPN as a proxy for their web browsing. This means all their web browsing traffic will pass out of their local network encrypted and be kept out of the prying eyes of whoever is watching at their gateway (either their ISP, employer, or government). If you want to configure this, follow the above example.

# Certain Windows-specific network settings
# can be pushed to clients, such as DNS
# or WINS server addresses.  CAVEAT:
# http://openvpn.net/faq.html#dhcpcaveats
# The addresses below refer to the public
# DNS servers provided by opendns.com.
;push "dhcp-option DNS 208.67.222.222"
;push "dhcp-option DNS 208.67.220.220"

You can push any options normally available to any DHCP server.

# Uncomment this directive to allow different
# clients to be able to "see" each other.
# By default, clients will only see the server.
# To force clients to only see the server, you
# will also need to appropriately firewall the
# server's TUN/TAP interface.
;client-to-client

Most often, you don't need your clients to talk to each other over your tunnel. Usually they are only laptops or possibly desktop computers and are not providing any services of their own to other machines. So you won't forward traffic from one client to another. But if you do want to do this, then you need to turn on this parameter.

# Uncomment this directive if multiple clients
# might connect with the same certificate/key
# files or common names.  This is recommended
# only for testing purposes.  For production use,
# each client should have its own certificate/key
# pair.
#
# IF YOU HAVE NOT GENERATED INDIVIDUAL
# CERTIFICATE/KEY PAIRS FOR EACH CLIENT,
# EACH HAVING ITS OWN UNIQUE "COMMON NAME",
# UNCOMMENT THIS LINE OUT.
;duplicate-cn

Most of the time you should make sure each client has a unique common name. Quite often, the common name of a certificate will just be the name of the person using it. If that person happens to use multiple computers, they might use the same name in multiple certificates (or worse, use the same certificate across multiple machines -- discourage this). If this happens, then you must turn on this parameter. Also, you will probably not be able to use ipp.txt above.

# The keepalive directive causes ping-like
# messages to be sent back and forth over
# the link so that each side knows when
# the other side has gone down.
# Ping every 10 seconds, assume that remote
# peer is down if no ping received during
# a 120 second time period.
keepalive 10 120

The comments here are pretty self-explanatory. I think the defaults are reasonable.

# For extra security beyond that provided
# by SSL/TLS, create an "HMAC firewall"
# to help block DoS attacks and UDP port flooding.
#
# Generate with:
#   openvpn --genkey --secret ta.key
#
# The server and each client must have
# a copy of this key.
# The second parameter should be '0'
# on the server and '1' on the clients.
;tls-auth ta.key 0 # This file is secret

I'm not really sure how this is supposed to prevent flooding. After all, you can't control what someone else sends you. Suffice it to say, I haven't used this.

# Select a cryptographic cipher.
# This config item must be copied to
# the client config file as well.
;cipher BF-CBC        # Blowfish (default)
;cipher AES-128-CBC   # AES
;cipher DES-EDE3-CBC  # Triple-DES

There have been a lot of issues concerning OpenSSL recently. Some were specific to the OpenSSL code, but some were just discoveries that certain ciphers had weaknesses that weren't discovered previously. If this concerns you, you can discover what ciphers are available using the command `openssl ciphers` and picking through the list until you find a combination you like. Make sure all clients are capable of using the same one.

# Enable compression on the VPN link.
# If you enable it here, you must also
# enable it in the client config file.
comp-lzo

Adding encryption to any traffic means that traffic gets heavier. You have to encode additional data about what you are sending so that the other end can properly identify and decode the packets. This means using more bandwidth than a plain text transmission. To help with this, use compression. This trades CPU time for network time. In this day and age, your CPU is probably much faster than your network, so the trade-off advantage should be obvious.

# The maximum number of concurrently connected
# clients we want to allow.
;max-clients 100

As stated above, in this example we are already limited to 63 clients due to the address space we chose. Honestly, I'm not sure why you would want to impose any other form of artificial limitation on your clients. Maybe if your gateway's CPU was just so taxed that 101 connections was just too much? Still, the option is there if you want it.

# It's a good idea to reduce the OpenVPN
# daemon's privileges after initialization.
#
# You can uncomment this out on
# non-Windows systems.
;user nobody
;group nobody
user nobody
group nogroup

Your distro may have a dedicated openvpn user and group. I copied this example from an installation which did not.

# The persist options will try to avoid
# accessing certain resources on restart
# that may no longer be accessible because
# of the privilege downgrade.
persist-key
persist-tun

As the comment says, ephemeral keys and tunnel devices might tear down across restarts. In my own experience, these parameters don't make a huge difference, but they make restarts a little quicker.

# Output a short status file showing
# current connections, truncated
# and rewritten every minute.
status /tmp/openvpn-status.log

This file is just a quick glance at the current state of affairs for the VPN server. I moved it to /tmp so that it would not continuously write to flash memory, wearing it out quicker.

# By default, log messages will go to the syslog (or
# on Windows, if running as a service, they will go to
# the "\Program Files\OpenVPN\log" directory).
# Use log or log-append to override this default.
# "log" will truncate the log file on OpenVPN startup,
# while "log-append" will append to it.  Use one
# or the other (but not both).
;log         openvpn.log
;log-append  openvpn.log
log-append /tmp/openvpn.log

My own OpenVPN server is running on a small router. I chose to write to a local log rather than syslog.

# Set the appropriate level of log
# file verbosity.
#
# 0 is silent, except for fatal errors
# 4 is reasonable for general usage
# 5 and 6 can help to debug connection problems
# 9 is extremely verbose
verb 3

This is the default. I haven't seen a need to change it.

# Silence repeating messages.  At most 20
# sequential messages of the same message
# category will be output to the log.
;mute 20

I have never had a reason to change this one.

Configure Clients

Each of the clients can have multiple servers which they connect to. For example you might have both an office LAN and a data center you regularly log in to. Or you might be a consultant with multiple clients you regularly have to do maintenance for. For this reason, I usually name the client configurations after the servers they connect to. So name this appropriately, home.conf, office.conf, or whatever. This time around I have stripped the comments aout of the configuration file. Examples should be available in /usr/share/doc.

client

This tells openvpn that it will be initiating an outbound connection.

remote gateway.example.com

When configured as a client, OpenVPN needs to know where to connect. Try to use a fully qualified domain name here. Also, this fully qualified domain name needs to be the common name or a subject alternative name for the server's certificate. If the clients must use the server's IP address, it must be part of the subject of the certificate.

ca ca.crt
cert client.example-com.crt
key client.example-com.key

A separate CA can be used to sign the certificates of both the server and the clients. If you do this, the clients need the CA certificate which matches the key that signed the server's certificate, and the server needs the CA certificate that matches the key which signs all the clients' certificates.

Since a client can connect to multiple servers, I name the local key and certificate files after the organization with which they will be used. It is possible to configure multiple servers to authenticate certificates signed by the same CA, so clients connecting to multiple servers like this will only need one certificate for the whole organization.

dev tun

Again, specify to use TUN networking. This must match the server.

proto udp

Again, specify to use UDP. This must also match the server.

nobind

This says not to specify the source port of the outgoing connection. It should not be necessary to do so unless the server is expecting only certain source ports.

persist-key
persist-tun

As with the server, this should reduce the amount of time it takes to restart the software. I don't think it will make a huge difference for clients, but it doesn't hurt and the example encourages it.

user openvpn
group openvpn

Specify to run as an unprivileged user.

Personal tools
Other sites