OpenVPN Server On Raspberry Pi


Having a great geek out chat with Andrew Gorton and he gave me the idea of setting up an OpenVPN server using a Raspberry Pi. A simple, relatively cheap solution to get access to resources at home.


I will assume that the Raspberry Pi is already setup with power, keyboard or remote access to the terminal running Raspbian without X-Windows (command line online).

If not, have a look at Installing Operating System Images guide.

For outside access, allow port 1194 UDP on your router. You can use TCP by changing the configuration from UDP to TCP but for this I will be using UDP.

Also on the networking side, it’s advised to give your Pi a reservered DHCP address or a static IP because 1. you’ll need to port forward to the Pi and 2. there are steps in the installation that need it’s local IP address.


OpenVPN is a package in apt-get so to install the server:
sudo apt-get install openvpn

Server Configuration

The setup requires priviledged access so to save time switch to the super user until we exit / shutdown / restart:
sudo -s

Copy the example RSA configuration file:
cp -r /usr/share/doc/openvpn/examples/easy-rsa/2.0 /etc/openvpn/easy-rsa

Go to the new directory where the RSA configuration files were copied to:
cd /etc/openvpn/easy-rsa

This is optional but you can increase the strength of the key used to sign everything. Edit /etc/openvpn/easy-rsa/vars and replace:
export KEY_SIZE=1024
export KEY_SIZE=2048

Modify the defaults for certificate generation so that there are less typing required. These can be found at the end of the /etc/openvpn/easy-rsa/2.0/vars. For example, to configure a UK (GB) default:
export KEY_PROVINCE=”London”
export KEY_CITY=”London”
export KEY_ORG=””
export KEY_EMAIL=””

If there are 2 export KEY_EMAIL entries then change one and delete the other.

Save and exit the file.

Server Post Install Setup

Everytime you need to make a change to OpenVPN, do the following:
Switch to super user
sudo -s

Go to the base directory where the OpenVPN files are:
cd /etc/openvpn/easy-rsa

Load the helper bash script:
source vars

On first setup or if you want to start from fresh (i.e removing all keys and users created from before) run the following command:

Create a self signed certifcate authority:

Answer the prompts on screen. A tip for UK the correct Country Name is GB not UK. Make sure you can the common name to a domain address or the host name which by default is raspberrypi. Note down these because you’ll have to retype them in the next step. Also the answer you give should not have any spaces.

Create the OpenVPN server key. Replace [Server Name] with a suitable with the common name above:
./build-key-server [Server Name]

The following questions need extra attention:

  1. A challenge password? – Leave it empty
  2. Sign the certificate? [y/n] – Always say yes (y)

Create a key that will help with Denial of Service (DoS) attacks:
openvpn –-genkey –-secret keys/ta.key

Create a new file at /etc/openvpn/server.conf which will contain the server configuration. Copy and paste the contents of this. Here’s an extract:

dev tun
proto udp #Some people prefer to use tcp. Don’t change it if you don’t know.
port 1194
ca /etc/openvpn/easy-rsa/keys/ca.crt
cert /etc/openvpn/easy-rsa/keys/Server.crt # SWAP WITH YOUR CRT NAME
key /etc/openvpn/easy-rsa/keys/Server.key # SWAP WITH YOUR KEY NAME
dh /etc/openvpn/easy-rsa/keys/dh1024.pem # If you changed to 2048, change that here!
# server and remote endpoints
# Add route to Client routing table for the OpenVPN Server
push “route”
# Add route to Client routing table for the OpenVPN Subnet
push “route”
# your local subnet
# Set primary domain name server address to the SOHO Router
# If your router does not do DNS, you can use Google DNS
push “dhcp-option DNS” # This should already match your router address and not need to be changed.
# Override the Client default gateway by using and
# rather than This has the benefit of
# overriding but not wiping out the original default gateway.
push “redirect-gateway def1”
keepalive 10 120
tls-auth /etc/openvpn/easy-rsa/keys/ta.key 0
cipher AES-128-CBC
user nobody
group nogroup
status /var/log/openvpn-status.log 20
log /var/log/openvpn.log
verb 1

Modift /etc/sysctl.conf to allow traffic forwarding. Find the line and uncomment it from:

Save and exit the file. Run the following to re-load sysctl.conf:
sysctl -p

Configure the firewall to allow OpenVPN traffic by creating a file in /etc/ and add the following to it:


iptables -t nat -A POSTROUTING -s -o eth0 -j SNAT –to-source

Replace the ‘’s with the Raspberry Pi’s IP address.

Change the file permission and ownser of the firewall config file created above:
chmod 700 /etc/
chown root /etc/

Edit the network interface file at /etc/network/interfaces and add the new firewall rules. Find the line:
iface eth0 inet dhcp
and add the following. Note the (tab) indentation:
iface eth0 inet dhcp
pre-up /etc/

Create the default connection profile to be used by clients at /etc/openvpn/easy-rsa/keys/Default.txt and add the following:

dev tun
proto udp
remote [Internet IP or DDNS] 1194
resolv-retry infinite
ns-cert-type server
key-direction 1
cipher AES-128-CBC
verb 1
mute 20

Change the protocol from UDP to TCP (if applicable):
proto tcp

Replace [Internet IP or DDNS] with your external (hopefully static) IP address or put the Dynamic DNS address here. For example:
remote 1194
Some of the above can be overriden by the client but it saves a lot of time if the correct defaults are used.

Create a script to generate connection profile files at /etc/openvpn/easy-rsa/keys/ and copy and paste the contents of this. Extract:

# Default Variable Declarations
#Ask for a Client name
echo “Please enter an existing Client Name:”
read NAME
#1st Verify that client’s Public Key Exists
if [ ! -f $NAME$CRT ]; then
echo “[ERROR]: Client Public Key Certificate not found: $NAME$CRT”
echo “Client’s cert found: $NAME$CR”
#Then, verify that there is a private key for that client
if [ ! -f $NAME$KEY ]; then
echo “[ERROR]: Client 3des Private Key not found: $NAME$KEY”
echo “Client’s Private Key found: $NAME$KEY”

#Confirm the CA public key exists
if [ ! -f $CA ]; then
echo “[ERROR]: CA Public Key not found: $CA”
echo “CA public Key found: $CA”

#Confirm the tls-auth ta key file exists
if [ ! -f $TA ]; then
echo “[ERROR]: tls-auth Key not found: $TA”
echo “tls-auth Private Key found: $TA”
#Ready to make a new .opvn file – Start by populating with the
default file
#Now, append the CA Public Cert
echo “” >> $NAME$FILEEXT
echo “

#Next append the client Public Cert
echo “” >> $NAME$FILEEXT
echo “
#Then, append the client Private Key
echo “” >> $NAME$FILEEXT
echo “
#Finally, append the TA Private Key
echo “” >> $NAME$FILEEXT
echo “
echo “Done! $NAME$FILEEXT Successfully Created.”

Change the file permissions of the script:
chmod 700 /etc/openvpn/easy-rsa/keys/

The next few steps can be done after the client setup to save time but it needs to be done before using OpenVPN.

Create the key exchange:

WARNING: This can a LONG time (for 2048 server key it took over 15 minutes):


The server is now done and it’s time to reboot:
sudo reboot

User Setup

Whilst you can share users between devices, it is best to generate a user account per device so that any compromised accounts do not affect others. Also it’s free to setup as many users as you want. The only hassle is generating and remembering the usernames and passwords (which you should be using a password manager for!).

Switch to super user:
sudo -s

Go to the RSA directory:
cd /etc/openvpn/easy-rsa

Load the helper bash script:
source vars

Create key for new user
./build-key-pass [Username]

  1. Enter PEM pass phrase – This is the password for the account. It will ask for this twice.
  2. A challenge password? – Leave empty.
  3. Sign the certificate? [y/n] – Say (y) yes

Use des3 encryption. This is required for mobile devices and also more secure albeit for a little bit more command foo:

Go to the keys directory
cd keys

Run des3 on the generated user keys:
openssl rsa -in [Username].key -des3 -out [Username].3des.key
Each key is named by the username when creating the user account so replace [Username] with the correct username / file name. It will prompt for the user’s password.

Generate user connection profiles:

Look for the output Done! [username].opvn Successfully Created. for a successful run. It will create .ovpn files for each user usually named after their username.

The next challenge is getting these files off the server due to the restricted nature of the setup. Running the above script creats .ovpn files. These are helper files containing the setup profile needed on your device.

I use WinSCP to get these files off and temporarily changing the folder / file permissions to allow the pi user access:
sudo chmod 705 /etc/openvpn/easy-rsa/keys
sudo chmod 704 /etc/openvpn/easy-rsa/keys/*.ovpn

Copy the files then revert it back:
sudo chmod 700 /etc/openvpn/easy-rsa/keys
sudo chmod 700 /etc/openvpn/easy-rsa/keys/*.ovpn


It’s not the quickest or easiest thing to setup but following tutorial it worked first time (barring copying and paste errors). I tested the connection out using my phone over over GSM to check it worked. It’s a set and forget for the most part and it just needs to be kept up to date like most things using the apt system in Debian.

How to set up a Raspberry Pi VPN server

Building A Raspberry Pi VPN Part One: How And Why To Build A Server

About Danny

I.T software professional always studying and applying the knowledge gained and one way of doing this is to blog.

Danny also has participates in a part time project called Energy@Home [] for monitoring energy usage on a premise.

Dedicated to I.T since studying pure Information Technology since the age of 16, Danny Tsang working in the field that he has aimed for since leaving school.

View all posts by Danny → This entry was posted in Linux and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply