How To Set Up An OpenVPN Server On Ubuntu PDF
How To Set Up An OpenVPN Server On Ubuntu PDF
04
PostedMay 4, 2016 1.2mviews VPN UBUNTU UBUNTU 16.04
Introduction
Want to access the Internet safely and securely from your smartphone or laptop when connected to an untrusted network such as the
WiFi of a hotel or coffee shop? A Virtual Private Network (VPN) allows you to traverse untrusted networks privately and securely as if
you were on a private network. The traffic emerges from the VPN server and continues its journey to the destination.
When combined with HTTPS connections, this setup allows you to secure your wireless logins and transactions. You can circumvent
geographical restrictions and censorship, and shield your location and any unencrypted HTTP traffic from the untrusted network.
OpenVPN is a full-featured open source Secure Socket Layer (SSL) VPN solution that accommodates a wide range of configurations. In
this tutorial, we'll set up an OpenVPN server on a Droplet and then configure access to it from Windows, OS X, iOS and Android. This
tutorial will keep the installation and configuration steps as simple as possible for these setups.
Prerequisites
To complete this tutorial, you will need access to an Ubuntu 16.04 server.
You will need to configure a non-root user with sudo privileges before you start this guide. You can follow our Ubuntu 16.04 initial server
setup guide to set up a user with appropriate permissions. The linked tutorial will also set up a firewall, which we will assume is in place
during this guide.
When you are ready to begin, log into your Ubuntu server as your sudo user and continue below.
Step 1: Install OpenVPN
To start off, we will install OpenVPN onto our server. OpenVPN is available in Ubuntu's default repositories, so we can use apt for the
installation. We will also be installing the easy-rsa package, which will help us set up an internal CA (certificate authority) for use with our
VPN.
To update your server's package index and install the necessary packages type:
sudo apt-get update
Move into the newly created directory to begin configuring the CA:
cd ~/openvpn-ca
Inside, you will find some variables that can be adjusted to determine how your certificates will be created. We only need to worry about
a few of these.
Towards the bottom of the file, find the settings that set field defaults for new certificates. It should look something like this:
~/openvpn-ca/vars
...
export KEY_COUNTRY="US"
export KEY_PROVINCE="CA"
export KEY_CITY="SanFrancisco"
export KEY_ORG="Fort-Funston"
export KEY_EMAIL="[email protected]"
export KEY_OU="MyOrganizationalUnit"
...
Edit the values in red to whatever you'd prefer, but do not leave them blank:
~/openvpn-ca/vars
...
export KEY_COUNTRY="US"
export KEY_PROVINCE="NY"
export KEY_CITY="New York City"
export KEY_ORG="DigitalOcean"
export KEY_EMAIL="[email protected]"
export KEY_OU="Community"
...
While we are here, we will also edit the KEY_NAME value just below this section, which populates the subject field. To keep this simple,
we'll call it server in this guide:
~/openvpn-ca/vars
export KEY_NAME="server"
When you are finished, save and close the file.
Step 4: Build the Certificate Authority
Now, we can use the variables we set and the easy-rsa utilities to build our certificate authority.
Ensure you are in your CA directory, and then source the vars file you just edited:
cd ~/openvpn-ca
source vars
This will initiate the process of creating the root certificate authority key and certificate. Since we filled out the vars file, all of the values
should be populated automatically. Just press ENTER through the prompts to confirm the selections:
Output
Generating a 2048 bit RSA private key
..........................................................................................+++
...............................+++
writing new private key to 'ca.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [US]:
State or Province Name (full name) [NY]:
Locality Name (eg, city) [New York City]:
Organization Name (eg, company) [DigitalOcean]:
Organizational Unit Name (eg, section) [Community]:
Common Name (eg, your name or your server's hostname) [DigitalOcean CA]:
Name [server]:
Email Address [[email protected]]:
We now have a CA that can be used to create the rest of the files we need.
Step 5: Create the Server Certificate, Key, and Encryption Files
Next, we will generate our server certificate and key pair, as well as some additional files used during the encryption process.
Start by generating the OpenVPN server certificate and key pair. We can do this by typing:
Note: If you choose a name other than server here, you will have to adjust some of the instructions below. For instance, when copying
the generated files to the /etc/openvpn directroy, you will have to substitute the correct names. You will also have to modify
the /etc/openvpn/server.conf file later to point to the correct .crt and .key files.
./build-key-server server
Once again, the prompts will have default values based on the argument we just passed in (server) and the contents of our vars file we
sourced.
Feel free to accept the default values by pressing ENTER. Do not enter a challenge password for this setup. Towards the end, you will
have to enter y to two questions to sign and commit the certificate:
Output
...
source vars
./build-key client1
If instead, you wish to create a password-protected set of credentials, use the build-key-pass command:
cd ~/openvpn-ca
source vars
./build-key-pass client1
Again, the defaults should be populated, so you can just hit ENTER to continue. Leave the challenge password blank and make sure to
enter y for the prompts that ask whether to sign and commit the certificate.
Step 7: Configure the OpenVPN Service
Next, we can begin configuring the OpenVPN service using the credentials and files we've generated.
Copy the Files to the OpenVPN Directory
To begin, we need to copy the files we need to the /etc/openvpn configuration directory.
We can start with all of the files that we just generated. These were placed within the ~/openvpn-ca/keysdirectory as they were created.
We need to move our CA cert, our server cert and key, the HMAC signature, and the Diffie-Hellman file:
cd ~/openvpn-ca/keys
Next, we need to copy and unzip a sample OpenVPN configuration file into configuration directory so that we can use it as a basis for
our setup:
gunzip -c /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz | sudo tee /etc/openvpn/server.conf
Basic Configuration
First, find the HMAC section by looking for the tls-auth directive. Remove the ";" to uncomment the tls-auth line. Below this, add the key-
direction parameter set to "0":
/etc/openvpn/server.conf
tls-auth ta.key 0 # This file is secret
key-direction 0
Next, find the section on cryptographic ciphers by looking for the commented out cipher lines. The AES-128-CBC cipher offers a good
level of encryption and is well supported. Remove the ";" to uncomment the cipher AES-128-CBC line:
/etc/openvpn/server.conf
cipher AES-128-CBC
Below this, add an auth line to select the HMAC message digest algorithm. For this, SHA256 is a good choice:
/etc/openvpn/server.conf
auth SHA256
Finally, find the user and group settings and remove the ";" at the beginning of to uncomment those lines:
/etc/openvpn/server.conf
user nobody
group nogroup
(Optional) Push DNS Changes to Redirect All Traffic Through the VPN
The settings above will create the VPN connection between the two machines, but will not force any connections to use the tunnel. If you
wish to use the VPN to route all of your traffic, you will likely want to push the DNS settings to the client computers.
You can do this, uncomment a few directives that will configure client machines to redirect all web traffic through the VPN. Find
the redirect-gateway section and remove the semicolon ";" from the beginning of the redirect-gateway line to uncomment it:
/etc/openvpn/server.conf
push "redirect-gateway def1 bypass-dhcp"
Just below this, find the dhcp-option section. Again, remove the ";" from in front of both of the lines to uncomment them:
/etc/openvpn/server.conf
push "dhcp-option DNS 208.67.222.222"
push "dhcp-option DNS 208.67.220.220"
This should assist clients in reconfiguring their DNS settings to use the VPN tunnel for as the default gateway.
(Optional) Adjust the Port and Protocol
By default, the OpenVPN server uses port 1194 and the UDP protocol to accept client connections. If you need to use a different port
because of restrictive network environments that your clients might be in, you can change the port option. If you are not hosting web
content your OpenVPN server, port 443 is a popular choice since this is usually allowed through firewall rules.
/etc/openvpn/server.conf
# Optional!
port 443
Often if the protocol will be restricted to that port as well. If so, change proto from UDP to TCP:
/etc/openvpn/server.conf
# Optional!
proto tcp
If you have no need to use a different port, it is best to leave these two settings as their default.
(Optional) Point to Non-Default Credentials
If you selected a different name during the ./build-key-server command earlier, modify the cert and key lines that you see to point to the
appropriate .crt and .key files. If you used the default server, this should already be set correctly:
/etc/openvpn/server.conf
cert server.crt
key server.key
When you are finished, save and close the file.
Step 8: Adjust the Server Networking Configuration
Next, we need to adjust some aspects of the server's networking so that OpenVPN can correctly route traffic.
Allow IP Forwarding
First, we need to allow the server to forward traffic. This is fairly essential to the functionality we want our VPN server to provide.
We can adjust this setting by modifying the /etc/sysctl.conf file:
sudo nano /etc/sysctl.conf
Inside, look for the line that sets net.ipv4.ip_forward. Remove the "#" character from the beginning of the line to uncomment that setting:
/etc/sysctl.conf
net.ipv4.ip_forward=1
Save and close the file when you are finished.
To read the file and adjust the values for the current session, type:
sudo sysctl -p
Your public interface should follow the word "dev". For example, this result shows the interface named wlp11s0, which is highlighted
below:
Output
default via 203.0.113.1 dev wlp11s0 proto static metric 600
When you have the interface associated with your default route, open the /etc/ufw/before.rules file to add the relevant configuration:
sudo nano /etc/ufw/before.rules
This file handles configuration that should be put into place before the conventional UFW rules are loaded. Towards the top of the file,
add the highlighted lines below. This will set the default policy for the POSTROUTING chain in the nat table and masquerade any traffic
coming from the VPN:
Note: Remember to replace wlp11s0 in the -A POSTROUTING line below with the interface you found in the above command.
/etc/ufw/before.rules
#
# rules.before
#
# Rules that should be run before the ufw command line added rules. Custom
# rules should be added to one of these chains:
# ufw-before-input
# ufw-before-output
# ufw-before-forward
#
Inside, find the DEFAULT_FORWARD_POLICY directive. We will change the value from DROP to ACCEPT:
/etc/default/ufw
DEFAULT_FORWARD_POLICY="ACCEPT"
Save and close the file when you are finished.
Open the OpenVPN Port and Enable the Changes
Next, we'll adjust the firewall itself to allow traffic to OpenVPN.
If you did not change the port and protocol in the /etc/openvpn/server.conf file, you will need to open up UDP traffic to port 1194. If you
modified the port and/or protocol, substitute the values you selected here.
We'll also add the SSH port in case you forgot to add it when following the prerequisite tutorial:
sudo ufw allow 1194/udp
Now, we can disable and re-enable UFW to load the changes from all of the files we've modified:
sudo ufw disable
If everything went well, your output should look something that looks like this:
Output
[email protected] - OpenVPN connection to server
Loaded: loaded (/lib/systemd/system/[email protected]; disabled; vendor preset: enabled)
Active: active (running) since Tue 2016-05-03 15:30:05 EDT; 47s ago
Docs: man:openvpn(8)
https://community.openvpn.net/openvpn/wiki/Openvpn23ManPage
https://community.openvpn.net/openvpn/wiki/HOWTO
Process: 5852 ExecStart=/usr/sbin/openvpn --daemon ovpn-%i --status /run/openvpn/%i.status 10 --cd /etc/openvpn --script-security 2
--config /etc/openvpn/%i.conf --writepid /run/openvpn/%i.pid (code=exited, sta
Main PID: 5856 (openvpn)
Tasks: 1 (limit: 512)
CGroup: /system.slice/system-openvpn.slice/[email protected]
└─5856 /usr/sbin/openvpn --daemon ovpn-server --status /run/openvpn/server.status 10 --cd /etc/openvpn --script-security 2 --
config /etc/openvpn/server.conf --writepid /run/openvpn/server.pid
May 03 15:30:05 openvpn2 ovpn-server[5856]: /sbin/ip addr add dev tun0 local 10.8.0.1 peer 10.8.0.2
May 03 15:30:05 openvpn2 ovpn-server[5856]: /sbin/ip route add 10.8.0.0/24 via 10.8.0.2
May 03 15:30:05 openvpn2 ovpn-server[5856]: GID set to nogroup
May 03 15:30:05 openvpn2 ovpn-server[5856]: UID set to nobody
May 03 15:30:05 openvpn2 ovpn-server[5856]: UDPv4 link local (bound): [undef]
May 03 15:30:05 openvpn2 ovpn-server[5856]: UDPv4 link remote: [undef]
May 03 15:30:05 openvpn2 ovpn-server[5856]: MULTI: multi_init called, r=256 v=256
May 03 15:30:05 openvpn2 ovpn-server[5856]: IFCONFIG POOL: base=10.8.0.4 size=62, ipv6=0
May 03 15:30:05 openvpn2 ovpn-server[5856]: IFCONFIG POOL LIST
May 03 15:30:05 openvpn2 ovpn-server[5856]: Initialization Sequence Completed
You can also check that the OpenVPN tun0 interface is available by typing:
ip addr show tun0
Since our client configuration files will have the client keys embedded, we should lock down permissions on our inner directory:
chmod 700 ~/client-configs/files
KEY_DIR=~/openvpn-ca/keys
OUTPUT_DIR=~/client-configs/files
BASE_CONFIG=~/client-configs/base.conf
cat ${BASE_CONFIG} \
<(echo -e '<ca>') \
${KEY_DIR}/ca.crt \
<(echo -e '</ca>\n<cert>') \
${KEY_DIR}/${1}.crt \
<(echo -e '</cert>\n<key>') \
${KEY_DIR}/${1}.key \
<(echo -e '</key>\n<tls-auth>') \
${KEY_DIR}/ta.key \
<(echo -e '</tls-auth>') \
> ${OUTPUT_DIR}/${1}.ovpn
Save and close the file when you are finished.
Mark the file as executable by typing:
chmod 700 ~/client-configs/make_config.sh
./make_config.sh client1
If everything went well, we should have a client1.ovpn file in our ~/client-configs/files directory:
ls ~/client-configs/files
Output
client1.ovpn
Transferring Configuration to Client Devices
We need to transfer the client configuration file to the relevant device. For instance, this could be your local computer or a mobile device.
While the exact applications used to accomplish this transfer will depend on your choice and device's operating system, you want the
application to use SFTP (SSH file transfer protocol) or SCP (Secure Copy) on the backend. This will transport your client's VPN
authentication files over an encrypted connection.
Here is an example SFTP command using our client1.ovpn example. This command can be run from your local computer (OS X or
Linux). It places the .ovpn file in your home directory:
sftp sammy@openvpn_server_ip:client-configs/files/client1.ovpn ~/
Here are several tools and tutorials for securely transferring files from the server to a local computer:
WinSCP
How To Use SFTP to Securely Transfer Files with a Remote Server
How To Use Filezilla to Transfer and Manage Files Securely on your VPS
Step 12: Install the Client Configuration
Now, we'll discuss how to install a client VPN profile on Windows, OS X, iOS, and Android. None of these client instructions are
dependent on one another, so feel free to skip to whichever is applicable to you.
The OpenVPN connection will be called whatever you named the .ovpn file. In our example, this means that the connection will be
called client1.ovpn for the first client file we generated.
Windows
Installing
The OpenVPN client application for Windows can be found on OpenVPN's Downloads page. Choose the appropriate installer version for
your version of Windows.
Note
OpenVPN needs administrative privileges to install.
On CentOS you can enable the EPEL repositories and then install it by typing:
sudo yum install epel-release
Configuring
Check to see if your distribution includes a /etc/openvpn/update-resolv-conf script:
ls /etc/openvpn
Output
update-resolve-conf
Next, edit the OpenVPN client configuration file you transfered:
nano client1.ovpn
Uncomment the three lines we placed in to adjust the DNS settings if you were able to find an update-resolv-conf file:
client1.ovpn
script-security 2
up /etc/openvpn/update-resolv-conf
down /etc/openvpn/update-resolv-conf
If you are using CentOS, change the group from nogroup to nobody to match the distribution's available groups:
client1.ovpn
group nobody
Save and close the file.
Now, you can connect to the VPN by just pointing the openvpn command to the client configuration file:
sudo openvpn --config client1.ovpn
This should connect you to your server.
iOS
Installing
From the iTunes App Store, search for and install OpenVPN Connect, the official iOS OpenVPN client application. To transfer your iOS
client configuration onto the device, connect it directly to a computer.
Completing the transfer with iTunes will be outlined here. Open iTunes on the computer and click on iPhone > apps. Scroll down to the
bottom to the File Sharing section and click the OpenVPN app. The blank window to the right, OpenVPN Documents, is for sharing
files. Drag the .ovpn file to the OpenVPN Documents window.