{"id":338,"date":"2017-08-02T23:15:00","date_gmt":"2017-08-02T15:15:00","guid":{"rendered":"https:\/\/blog.jsjs.org\/?p=338"},"modified":"2017-08-02T23:15:00","modified_gmt":"2017-08-02T15:15:00","slug":"how-to-set-up-an-openvpn-server-on-ubuntu-16-04","status":"publish","type":"post","link":"https:\/\/blog.jsjs.org\/?p=338","title":{"rendered":"How To Set Up an OpenVPN Server on Ubuntu 16.04"},"content":{"rendered":"<h3 id=\"introduction\">Introduction<\/h3>\n<p>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\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Virtual_private_network\">Virtual Private Network<\/a>\u00a0(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.<\/p>\n<p>When combined with\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/HTTP_Secure\">HTTPS connections<\/a>, 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.<\/p>\n<p><a href=\"https:\/\/openvpn.net\/\">OpenVPN<\/a>\u00a0is a full-featured open source Secure Socket Layer (SSL) VPN solution that accommodates a wide range of configurations. In this tutorial, we&#8217;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.<\/p>\n<div data-unique=\"prerequisites\"><\/div>\n<h2 id=\"prerequisites\">Prerequisites<\/h2>\n<p>To complete this tutorial, you will need access to an Ubuntu 16.04 server.<\/p>\n<p>You will need to configure a non-root user with\u00a0<code>sudo<\/code>\u00a0privileges before you start this guide. You can follow our\u00a0<a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/initial-server-setup-with-ubuntu-16-04\">Ubuntu 16.04 initial server setup guide<\/a>\u00a0to set up a user with appropriate permissions. The linked tutorial will also set up a\u00a0<strong>firewall<\/strong>, which we will assume is in place during this guide.<\/p>\n<p>When you are ready to begin, log into your Ubuntu server as your\u00a0<code>sudo<\/code>\u00a0user and continue below.<\/p>\n<div data-unique=\"step-1-install-openvpn\"><\/div>\n<h2 id=\"step-1-install-openvpn\">Step 1: Install OpenVPN<\/h2>\n<p>To start off, we will install OpenVPN onto our server. OpenVPN is available in Ubuntu&#8217;s default repositories, so we can use\u00a0<code>apt<\/code>\u00a0for the installation. We will also be installing the\u00a0<code>easy-rsa<\/code>\u00a0package, which will help us set up an internal CA (certificate authority) for use with our VPN.<\/p>\n<p>To update your server&#8217;s package index and install the necessary packages type:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo apt-get update<\/li>\n<li class=\"line\">sudo apt-get install openvpn easy-rsa<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>The needed software is now on the server, ready to be configured.<\/p>\n<div data-unique=\"step-2-set-up-the-ca-directory\"><\/div>\n<h2 id=\"step-2-set-up-the-ca-directory\">Step 2: Set Up the CA Directory<\/h2>\n<p>OpenVPN is an TLS\/SSL VPN. This means that it utilizes certificates in order to encrypt traffic between the server and clients. In order to issue trusted certificates, we will need to set up our own simple certificate authority (CA).<\/p>\n<p>To begin, we can copy the\u00a0<code>easy-rsa<\/code>\u00a0template directory into our home directory with the\u00a0<code>make-cadir<\/code>command:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">make-cadir ~\/openvpn-ca<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Move into the newly created directory to begin configuring the CA:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">cd ~\/openvpn-ca<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<div data-unique=\"step-3-configure-the-ca-variables\"><\/div>\n<h2 id=\"step-3-configure-the-ca-variables\">Step 3: Configure the CA Variables<\/h2>\n<p>To configure the values our CA will use, we need to edit the\u00a0<code>vars<\/code>\u00a0file within the directory. Open that file now in your text editor:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">nano vars<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>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.<\/p>\n<p>Towards the bottom of the file, find the settings that set field defaults for new certificates. It should look something like this:<\/p>\n<div class=\"code-label \" title=\"~\/openvpn-ca\/vars\">~\/openvpn-ca\/vars<\/div>\n<pre class=\"code-pre \"><code>. . .\n\nexport KEY_COUNTRY=\"US\"\nexport KEY_PROVINCE=\"CA\"\nexport KEY_CITY=\"SanFrancisco\"\nexport KEY_ORG=\"Fort-Funston\"\nexport KEY_EMAIL=\"me@myhost.mydomain\"\nexport KEY_OU=\"MyOrganizationalUnit\"\n\n. . .\n<\/code><\/pre>\n<p>Edit the values in red to whatever you&#8217;d prefer, but do not leave them blank:<\/p>\n<div class=\"code-label \" title=\"~\/openvpn-ca\/vars\">~\/openvpn-ca\/vars<\/div>\n<pre class=\"code-pre \"><code>. . .\n\nexport KEY_COUNTRY=\"<span class=\"highlight\">US<\/span>\"\nexport KEY_PROVINCE=\"<span class=\"highlight\">NY<\/span>\"\nexport KEY_CITY=\"<span class=\"highlight\">New York City<\/span>\"\nexport KEY_ORG=\"<span class=\"highlight\">DigitalOcean<\/span>\"\nexport KEY_EMAIL=\"<span class=\"highlight\">admin@example.com<\/span>\"\nexport KEY_OU=\"<span class=\"highlight\">Community<\/span>\"\n\n. . .\n<\/code><\/pre>\n<p>While we are here, we will also edit the\u00a0<code>KEY_NAME<\/code>\u00a0value just below this section, which populates the subject field. To keep this simple, we&#8217;ll call it\u00a0<code>server<\/code>\u00a0in this guide:<\/p>\n<div class=\"code-label \" title=\"~\/openvpn-ca\/vars\">~\/openvpn-ca\/vars<\/div>\n<pre class=\"code-pre \"><code>export KEY_NAME=\"<span class=\"highlight\">server<\/span>\"\n<\/code><\/pre>\n<p>When you are finished, save and close the file.<\/p>\n<div data-unique=\"step-4-build-the-certificate-authority\"><\/div>\n<h2 id=\"step-4-build-the-certificate-authority\">Step 4: Build the Certificate Authority<\/h2>\n<p>Now, we can use the variables we set and the\u00a0<code>easy-rsa<\/code>\u00a0utilities to build our certificate authority.<\/p>\n<p>Ensure you are in your CA directory, and then source the\u00a0<code>vars<\/code>\u00a0file you just edited:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">cd ~\/openvpn-ca<\/li>\n<li class=\"line\">source vars<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>You should see the following if it was sourced correctly:<\/p>\n<pre class=\"code-pre \"><code><\/code><\/pre>\n<div class=\"secondary-code-label \" title=\"Output\">Output<\/div>\n<pre class=\"code-pre \"><code>NOTE: If you run .\/clean-all, I will be doing a rm -rf on \/home\/sammy\/openvpn-ca\/keys\n<\/code><\/pre>\n<p>Make sure we&#8217;re operating in a clean environment by typing:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">.\/clean-all<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Now, we can build our root CA by typing:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">.\/build-ca<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>This will initiate the process of creating the root certificate authority key and certificate. Since we filled out the\u00a0<code>vars<\/code>\u00a0file, all of the values should be populated automatically. Just press\u00a0<strong>ENTER<\/strong>\u00a0through the prompts to confirm the selections:<\/p>\n<pre class=\"code-pre \"><code><\/code><\/pre>\n<div class=\"secondary-code-label \" title=\"Output\">Output<\/div>\n<pre class=\"code-pre \"><code>Generating a 2048 bit RSA private key\n..........................................................................................+++\n...............................+++\nwriting new private key to 'ca.key'\n-----\nYou are about to be asked to enter information that will be incorporated\ninto your certificate request.\nWhat you are about to enter is what is called a Distinguished Name or a DN.\nThere are quite a few fields but you can leave some blank\nFor some fields there will be a default value,\nIf you enter '.', the field will be left blank.\n-----\nCountry Name (2 letter code) [US]:\nState or Province Name (full name) [NY]:\nLocality Name (eg, city) [New York City]:\nOrganization Name (eg, company) [DigitalOcean]:\nOrganizational Unit Name (eg, section) [Community]:\nCommon Name (eg, your name or your server's hostname) [DigitalOcean CA]:\nName [server]:\nEmail Address [admin@email.com]:\n<\/code><\/pre>\n<p>We now have a CA that can be used to create the rest of the files we need.<\/p>\n<div data-unique=\"step-5-create-the-server-certificate,-key,-and-encryption-files\"><\/div>\n<h2 id=\"step-5-create-the-server-certificate-key-and-encryption-files\">Step 5: Create the Server Certificate, Key, and Encryption Files<\/h2>\n<p>Next, we will generate our server certificate and key pair, as well as some additional files used during the encryption process.<\/p>\n<p>Start by generating the OpenVPN server certificate and key pair. We can do this by typing:<\/p>\n<p><span class=\"note\"><strong>Note<\/strong>: If you choose a name other than\u00a0<code>server<\/code>\u00a0here, you will have to adjust some of the instructions below. For instance, when copying the generated files to the\u00a0<code>\/etc\/openvpn<\/code>\u00a0directroy, you will have to substitute the correct names. You will also have to modify the\u00a0<code>\/etc\/openvpn\/server.conf<\/code>\u00a0file later to point to the correct\u00a0<code>.crt<\/code>\u00a0and\u00a0<code>.key<\/code>\u00a0files.<br \/>\n<\/span><\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">.\/build-key-server server<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Once again, the prompts will have default values based on the argument we just passed in (<code>server<\/code>) and the contents of our\u00a0<code>vars<\/code>\u00a0file we sourced.<\/p>\n<p>Feel free to accept the default values by pressing\u00a0<strong>ENTER<\/strong>. Do\u00a0<em>not<\/em>\u00a0enter a challenge password for this setup. Towards the end, you will have to enter\u00a0<strong>y<\/strong>\u00a0to two questions to sign and commit the certificate:<\/p>\n<pre class=\"code-pre \"><code><\/code><\/pre>\n<div class=\"secondary-code-label \" title=\"Output\">Output<\/div>\n<pre class=\"code-pre \"><code>. . .\n\nCertificate is to be certified until May  1 17:51:16 2026 GMT (3650 days)\nSign the certificate? [y\/n]:<span class=\"highlight\">y<\/span>\n\n\n1 out of 1 certificate requests certified, commit? [y\/n]<span class=\"highlight\">y<\/span>\nWrite out database with 1 new entries\nData Base Updated\n<\/code><\/pre>\n<p>Next, we&#8217;ll generate a few other items. We can generate a strong Diffie-Hellman keys to use during key exchange by typing:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">.\/build-dh<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>This might take a few minutes to complete.<\/p>\n<p>Afterwards, we can generate an HMAC signature to strengthen the server&#8217;s TLS integrity verification capabilities:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">openvpn &#8211;genkey &#8211;secret keys\/ta.key<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<div data-unique=\"step-6-generate-a-client-certificate-and-key-pair\"><\/div>\n<h2 id=\"step-6-generate-a-client-certificate-and-key-pair\">Step 6: Generate a Client Certificate and Key Pair<\/h2>\n<p>Next, we can generate a client certificate and key pair. Although this can be done on the client machine and then signed by the server\/CA for security purposes, for this guide we will generate the signed key on the server for the sake of simplicity.<\/p>\n<p>We will generate a single client key\/certificate for this guide, but if you have more than one client, you can repeat this process as many times as you&#8217;d like. Pass in a unique value to the script for each client.<\/p>\n<p>Because you may come back to this step at a later time, we&#8217;ll re-source the\u00a0<code>vars<\/code>\u00a0file. We will use\u00a0<code>client1<\/code>\u00a0as the value for our first certificate\/key pair for this guide.<\/p>\n<p>To produce credentials without a password, to aid in automated connections, use the\u00a0<code>build-key<\/code>command like this:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">cd ~\/openvpn-ca<\/li>\n<li class=\"line\">source vars<\/li>\n<li class=\"line\">.\/build-key <span class=\"highlight\">client1<\/span><\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>If instead, you wish to create a password-protected set of credentials, use the\u00a0<code>build-key-pass<\/code>command:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">cd ~\/openvpn-ca<\/li>\n<li class=\"line\">source vars<\/li>\n<li class=\"line\">.\/build-key-pass <span class=\"highlight\">client1<\/span><\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Again, the defaults should be populated, so you can just hit\u00a0<strong>ENTER<\/strong>\u00a0to continue. Leave the challenge password blank and make sure to enter\u00a0<strong>y<\/strong>\u00a0for the prompts that ask whether to sign and commit the certificate.<\/p>\n<div data-unique=\"step-7-configure-the-openvpn-service\"><\/div>\n<h2 id=\"step-7-configure-the-openvpn-service\">Step 7: Configure the OpenVPN Service<\/h2>\n<p>Next, we can begin configuring the OpenVPN service using the credentials and files we&#8217;ve generated.<\/p>\n<h3 id=\"copy-the-files-to-the-openvpn-directory\">Copy the Files to the OpenVPN Directory<\/h3>\n<p>To begin, we need to copy the files we need to the\u00a0<code>\/etc\/openvpn<\/code>\u00a0configuration directory.<\/p>\n<p>We can start with all of the files that we just generated. These were placed within the\u00a0<code>~\/openvpn-ca\/keys<\/code>\u00a0directory as they were created. We need to move our CA cert and key, our server cert and key, the HMAC signature, and the Diffie-Hellman file:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">cd ~\/openvpn-ca\/keys<\/li>\n<li class=\"line\">sudo cp ca.crt ca.key server.crt server.key ta.key dh2048.pem \/etc\/openvpn<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>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:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">gunzip -c \/usr\/share\/doc\/openvpn\/examples\/sample-config-files\/server.conf.gz | sudo tee \/etc\/openvpn\/server.conf<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<h3 id=\"adjust-the-openvpn-configuration\">Adjust the OpenVPN Configuration<\/h3>\n<p>Now that our files are in place, we can modify the server configuration file:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo nano \/etc\/openvpn\/server.conf<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<h4 id=\"basic-configuration\">Basic Configuration<\/h4>\n<p>First, find the HMAC section by looking for the\u00a0<code>tls-auth<\/code>\u00a0directive. Remove the &#8220;<strong>;<\/strong>&#8221; to uncomment the\u00a0<code>tls-auth<\/code>\u00a0line. Below this, add the\u00a0<code>key-direction<\/code>\u00a0parameter set to &#8220;0&#8221;:<\/p>\n<div class=\"code-label \" title=\"\/etc\/openvpn\/server.conf\">\/etc\/openvpn\/server.conf<\/div>\n<pre class=\"code-pre \"><code>tls-auth ta.key 0 # This file is secret\n<span class=\"highlight\">key-direction 0<\/span>\n<\/code><\/pre>\n<p>Next, find the section on cryptographic ciphers by looking for the commented out\u00a0<code>cipher<\/code>\u00a0lines. The\u00a0<code>AES-128-CBC<\/code>\u00a0cipher offers a good level of encryption and is well supported. Remove the &#8220;<strong>;<\/strong>&#8221; to uncomment the\u00a0<code>cipher AES-128-CBC<\/code>\u00a0line:<\/p>\n<div class=\"code-label \" title=\"\/etc\/openvpn\/server.conf\">\/etc\/openvpn\/server.conf<\/div>\n<pre class=\"code-pre \"><code>cipher AES-128-CBC\n<\/code><\/pre>\n<p>Below this, add an\u00a0<code>auth<\/code>\u00a0line to select the HMAC message digest algorithm. For this,\u00a0<code>SHA256<\/code>\u00a0is a good choice:<\/p>\n<div class=\"code-label \" title=\"\/etc\/openvpn\/server.conf\">\/etc\/openvpn\/server.conf<\/div>\n<pre class=\"code-pre \"><code>auth SHA256\n<\/code><\/pre>\n<p>Finally, find the\u00a0<code>user<\/code>\u00a0and\u00a0<code>group<\/code>\u00a0settings and remove the &#8220;<strong>;<\/strong>&#8221; at the beginning of to uncomment those lines:<\/p>\n<div class=\"code-label \" title=\"\/etc\/openvpn\/server.conf\">\/etc\/openvpn\/server.conf<\/div>\n<pre class=\"code-pre \"><code>user nobody\ngroup nogroup\n<\/code><\/pre>\n<h4 id=\"optional-push-dns-changes-to-redirect-all-traffic-through-the-vpn\">(Optional) Push DNS Changes to Redirect All Traffic Through the VPN<\/h4>\n<p>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.<\/p>\n<p>You can do this, uncomment a few directives that will configure client machines to redirect all web traffic through the VPN. Find the\u00a0<code>redirect-gateway<\/code>\u00a0section and remove the semicolon &#8220;<strong>;<\/strong>&#8221; from the beginning of the\u00a0<code>redirect-gateway<\/code>\u00a0line to uncomment it:<\/p>\n<div class=\"code-label \" title=\"\/etc\/openvpn\/server.conf\">\/etc\/openvpn\/server.conf<\/div>\n<pre class=\"code-pre \"><code>push \"redirect-gateway def1 bypass-dhcp\"\n<\/code><\/pre>\n<p>Just below this, find the\u00a0<code>dhcp-option<\/code>\u00a0section. Again, remove the &#8220;<strong>;<\/strong>&#8221; from in front of both of the lines to uncomment them:<\/p>\n<div class=\"code-label \" title=\"\/etc\/openvpn\/server.conf\">\/etc\/openvpn\/server.conf<\/div>\n<pre class=\"code-pre \"><code>push \"dhcp-option DNS 208.67.222.222\"\npush \"dhcp-option DNS 208.67.220.220\"\n<\/code><\/pre>\n<p>This should assist clients in reconfiguring their DNS settings to use the VPN tunnel for as the default gateway.<\/p>\n<h4 id=\"optional-adjust-the-port-and-protocol\">(Optional) Adjust the Port and Protocol<\/h4>\n<p>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\u00a0<code>port<\/code>\u00a0option. If you are not hosting web content your OpenVPN server, port 443 is a popular choice since this is usually allowed through firewall rules.<\/p>\n<div class=\"code-label \" title=\"\/etc\/openvpn\/server.conf\">\/etc\/openvpn\/server.conf<\/div>\n<pre class=\"code-pre \"><code># Optional!\nport <span class=\"highlight\">443<\/span>\n<\/code><\/pre>\n<p>Often if the protocol will be restricted to that port as well. If so, change\u00a0<code>proto<\/code>\u00a0from UDP to TCP:<\/p>\n<div class=\"code-label \" title=\"\/etc\/openvpn\/server.conf\">\/etc\/openvpn\/server.conf<\/div>\n<pre class=\"code-pre \"><code># Optional!\nproto <span class=\"highlight\">tcp<\/span>\n<\/code><\/pre>\n<p>If you have no need to use a different port, it is best to leave these two settings as their default.<\/p>\n<h4 id=\"optional-point-to-non-default-credentials\">(Optional) Point to Non-Default Credentials<\/h4>\n<p>If you selected a different name during the\u00a0<code>.\/build-key-server<\/code>\u00a0command earlier, modify the\u00a0<code>cert<\/code>and\u00a0<code>key<\/code>\u00a0lines that you see to point to the appropriate\u00a0<code>.crt<\/code>\u00a0and\u00a0<code>.key<\/code>\u00a0files. If you used the default\u00a0<code>server<\/code>, this should already be set correctly:<\/p>\n<div class=\"code-label \" title=\"\/etc\/openvpn\/server.conf\">\/etc\/openvpn\/server.conf<\/div>\n<pre class=\"code-pre \"><code>cert <span class=\"highlight\">server<\/span>.crt\nkey <span class=\"highlight\">server<\/span>.key\n<\/code><\/pre>\n<p>When you are finished, save and close the file.<\/p>\n<div data-unique=\"step-8-adjust-the-server-networking-configuration\"><\/div>\n<h2 id=\"step-8-adjust-the-server-networking-configuration\">Step 8: Adjust the Server Networking Configuration<\/h2>\n<p>Next, we need to adjust some aspects of the server&#8217;s networking so that OpenVPN can correctly route traffic.<\/p>\n<h3 id=\"allow-ip-forwarding\">Allow IP Forwarding<\/h3>\n<p>First, we need to allow the server to forward traffic. This is fairly essential to the functionality we want our VPN server to provide.<\/p>\n<p>We can adjust this setting by modifying the\u00a0<code>\/etc\/sysctl.conf<\/code>\u00a0file:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo nano \/etc\/sysctl.conf<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Inside, look for the line that sets\u00a0<code>net.ipv4.ip_forward<\/code>. Remove the &#8220;<strong>#<\/strong>&#8221; character from the beginning of the line to uncomment that setting:<\/p>\n<div class=\"code-label \" title=\"\/etc\/sysctl.conf\">\/etc\/sysctl.conf<\/div>\n<pre class=\"code-pre \"><code>net.ipv4.ip_forward=1\n<\/code><\/pre>\n<p>Save and close the file when you are finished.<\/p>\n<p>To read the file and adjust the values for the current session, type:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo sysctl -p<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<h3 id=\"adjust-the-ufw-rules-to-masquerade-client-connections\">Adjust the UFW Rules to Masquerade Client Connections<\/h3>\n<p>If you followed the Ubuntu 16.04 initial server setup guide in the prerequisites, you should have the UFW firewall in place. Regardless of whether you use the firewall to block unwanted traffic (which you almost always should do), we need the firewall in this guide to manipulate some of the traffic coming into the server. We need to modify the rules file to set up masquerading, an\u00a0<code>iptables<\/code>\u00a0concept that provides on-the-fly dynamic NAT to correctly route client connections.<\/p>\n<p>Before we open the firewall configuration file to add masquerading, we need to find the public network interface of our machine. To do this, type:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">ip route | grep default<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Your public interface should follow the word &#8220;dev&#8221;. For example, this result shows the interface named\u00a0<code>wlp11s0<\/code>, which is highlighted below:<\/p>\n<pre class=\"code-pre \"><code><\/code><\/pre>\n<div class=\"secondary-code-label \" title=\"Output\">Output<\/div>\n<pre class=\"code-pre \"><code>default via 203.0.113.1 dev <span class=\"highlight\">wlp11s0<\/span>  proto static  metric 600\n<\/code><\/pre>\n<p>When you have the interface associated with your default route, open the\u00a0<code>\/etc\/ufw\/before.rules<\/code>\u00a0file to add the relevant configuration:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo nano \/etc\/ufw\/before.rules<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>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\u00a0<code>POSTROUTING<\/code>\u00a0chain in the\u00a0<code>nat<\/code>\u00a0table and masquerade any traffic coming from the VPN:<\/p>\n<p><span class=\"note\"><strong>Note<\/strong>: Remember to replace\u00a0<code>wlp11s0<\/code>\u00a0in the\u00a0<code>-A POSTROUTING<\/code>\u00a0line below with the interface you found in the above command.<br \/>\n<\/span><\/p>\n<div class=\"code-label \" title=\"\/etc\/ufw\/before.rules\">\/etc\/ufw\/before.rules<\/div>\n<pre class=\"code-pre \"><code>#\n# rules.before\n#\n# Rules that should be run before the ufw command line added rules. Custom\n# rules should be added to one of these chains:\n#   ufw-before-input\n#   ufw-before-output\n#   ufw-before-forward\n#\n\n<span class=\"highlight\"># START OPENVPN RULES<\/span>\n<span class=\"highlight\"># NAT table rules<\/span>\n<span class=\"highlight\">*nat<\/span>\n<span class=\"highlight\">:POSTROUTING ACCEPT [0:0]<\/span>\n<span class=\"highlight\"># Allow traffic from OpenVPN client to wlp11s0 (change to the interface you discovered!)<\/span>\n<span class=\"highlight\">-A POSTROUTING -s 10.8.0.0\/8 -o wlp11s0 -j MASQUERADE<\/span>\n<span class=\"highlight\">COMMIT<\/span>\n<span class=\"highlight\"># END OPENVPN RULES<\/span>\n\n# Don't delete these required lines, otherwise there will be errors\n*filter\n. . .\n<\/code><\/pre>\n<p>Save and close the file when you are finished.<\/p>\n<p>We need to tell UFW to allow forwarded packets by default as well. To do this, we will open the\u00a0<code>\/etc\/default\/ufw<\/code>\u00a0file:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo nano \/etc\/default\/ufw<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Inside, find the\u00a0<code>DEFAULT_FORWARD_POLICY<\/code>\u00a0directive. We will change the value from\u00a0<code>DROP<\/code>\u00a0to\u00a0<code>ACCEPT<\/code>:<\/p>\n<div class=\"code-label \" title=\"\/etc\/default\/ufw\">\/etc\/default\/ufw<\/div>\n<pre class=\"code-pre \"><code>DEFAULT_FORWARD_POLICY=\"<span class=\"highlight\">ACCEPT<\/span>\"\n<\/code><\/pre>\n<p>Save and close the file when you are finished.<\/p>\n<h3 id=\"open-the-openvpn-port-and-enable-the-changes\">Open the OpenVPN Port and Enable the Changes<\/h3>\n<p>Next, we&#8217;ll adjust the firewall itself to allow traffic to OpenVPN.<\/p>\n<p>If you did not change the port and protocol in the\u00a0<code>\/etc\/openvpn\/server.conf<\/code>\u00a0file, 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.<\/p>\n<p>We&#8217;ll also add the SSH port in case you forgot to add it when following the prerequisite tutorial:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo ufw allow <span class=\"highlight\">1194<\/span>\/<span class=\"highlight\">udp<\/span><\/li>\n<li class=\"line\">sudo ufw allow OpenSSH<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Now, we can disable and re-enable UFW to load the changes from all of the files we&#8217;ve modified:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo ufw disable<\/li>\n<li class=\"line\">sudo ufw enable<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Our server is now configured to correctly handle OpenVPN traffic.<\/p>\n<div data-unique=\"step-9-start-and-enable-the-openvpn-service\"><\/div>\n<h2 id=\"step-9-start-and-enable-the-openvpn-service\">Step 9: Start and Enable the OpenVPN Service<\/h2>\n<p>We&#8217;re finally ready to start the OpenVPN service on our server. We can do this using systemd.<\/p>\n<p>We need to start the OpenVPN server by specifying our configuration file name as an instance variable after the systemd unit file name. Our configuration file for our server is called\u00a0<code>\/etc\/openvpn\/<span class=\"highlight\">server<\/span>.conf<\/code>, so we will add\u00a0<code><span class=\"highlight\">@server<\/span><\/code>\u00a0to end of our unit file when calling it:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo systemctl start openvpn@<span class=\"highlight\">server<\/span><\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Double-check that the service has started successfully by typing:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo systemctl status openvpn@server<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>If everything went well, your output should look something that looks like this:<\/p>\n<pre class=\"code-pre \"><code><\/code><\/pre>\n<div class=\"secondary-code-label \" title=\"Output\">Output<\/div>\n<pre class=\"code-pre \"><code>\u25cf openvpn@server.service - OpenVPN connection to server\n   Loaded: loaded (\/lib\/systemd\/system\/openvpn@.service; disabled; vendor preset: enabled)\n   Active: <span class=\"highlight\">active (running)<\/span> since Tue 2016-05-03 15:30:05 EDT; 47s ago\n     Docs: man:openvpn(8)\n           https:\/\/community.openvpn.net\/openvpn\/wiki\/Openvpn23ManPage\n           https:\/\/community.openvpn.net\/openvpn\/wiki\/HOWTO\n  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\n Main PID: 5856 (openvpn)\n    Tasks: 1 (limit: 512)\n   CGroup: \/system.slice\/system-openvpn.slice\/openvpn@server.service\n           \u2514\u25005856 \/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\n\nMay 03 15:30:05 openvpn2 ovpn-server[5856]: \/sbin\/ip addr add dev tun0 local 10.8.0.1 peer 10.8.0.2\nMay 03 15:30:05 openvpn2 ovpn-server[5856]: \/sbin\/ip route add 10.8.0.0\/24 via 10.8.0.2\nMay 03 15:30:05 openvpn2 ovpn-server[5856]: GID set to nogroup\nMay 03 15:30:05 openvpn2 ovpn-server[5856]: UID set to nobody\nMay 03 15:30:05 openvpn2 ovpn-server[5856]: UDPv4 link local (bound): [undef]\nMay 03 15:30:05 openvpn2 ovpn-server[5856]: UDPv4 link remote: [undef]\nMay 03 15:30:05 openvpn2 ovpn-server[5856]: MULTI: multi_init called, r=256 v=256\nMay 03 15:30:05 openvpn2 ovpn-server[5856]: IFCONFIG POOL: base=10.8.0.4 size=62, ipv6=0\nMay 03 15:30:05 openvpn2 ovpn-server[5856]: IFCONFIG POOL LIST\nMay 03 15:30:05 openvpn2 ovpn-server[5856]: Initialization Sequence Completed\n<\/code><\/pre>\n<p>You can also check that the OpenVPN\u00a0<code>tun0<\/code>\u00a0interface is available by typing:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">ip addr show tun0<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>You should see a configured interface:<\/p>\n<pre class=\"code-pre \"><code><\/code><\/pre>\n<div class=\"secondary-code-label \" title=\"Output\">Output<\/div>\n<pre class=\"code-pre \"><code>4: tun0: &lt;POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UNKNOWN group default qlen 100\n    link\/none\n    inet 10.8.0.1 peer 10.8.0.2\/32 scope global tun0\n       valid_lft forever preferred_lft forever\n<\/code><\/pre>\n<p>If everything went well, enable the service so that it starts automatically at boot:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo systemctl enable openvpn@server<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<div data-unique=\"step-10-create-client-configuration-infrastructure\"><\/div>\n<h2 id=\"step-10-create-client-configuration-infrastructure\">Step 10: Create Client Configuration Infrastructure<\/h2>\n<p>Next, we need to set up a system that will allow us to create client configuration files easily.<\/p>\n<h3 id=\"creating-the-client-config-directory-structure\">Creating the Client Config Directory Structure<\/h3>\n<p>Create a directory structure within your home directory to store the files:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">mkdir -p ~\/client-configs\/files<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Since our client configuration files will have the client keys embedded, we should lock down permissions on our inner directory:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">chmod 700 ~\/client-configs\/files<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<h3 id=\"creating-a-base-configuration\">Creating a Base Configuration<\/h3>\n<p>Next, let&#8217;s copy an example client configuration into our directory to use as our base configuration:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">cp \/usr\/share\/doc\/openvpn\/examples\/sample-config-files\/client.conf ~\/client-configs\/base.conf<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Open this new file in your text editor:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">nano ~\/client-configs\/base.conf<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Inside, we need to make a few adjustments.<\/p>\n<p>First, locate the\u00a0<code>remote<\/code>\u00a0directive. This points the client to our OpenVPN server address. This should be the public IP address of your OpenVPN server. If you changed the port that the OpenVPN server is listening on, change\u00a0<code>1194<\/code>\u00a0to the port you selected:<\/p>\n<div class=\"code-label \" title=\"~\/client-configs\/base.conf\">~\/client-configs\/base.conf<\/div>\n<pre class=\"code-pre \"><code>. . .\n# The hostname\/IP and port of the server.\n# You can have multiple remote entries\n# to load balance between the servers.\nremote <span class=\"highlight\">server_IP_address<\/span> <span class=\"highlight\">1194<\/span>\n. . .\n<\/code><\/pre>\n<p>Be sure that the protocol matches the value you are using in the server configuration:<\/p>\n<div class=\"code-label \" title=\"~\/client-configs\/base.conf\">~\/client-configs\/base.conf<\/div>\n<pre class=\"code-pre \"><code>proto <span class=\"highlight\">udp<\/span>\n<\/code><\/pre>\n<p>Next, uncomment the\u00a0<code>user<\/code>\u00a0and\u00a0<code>group<\/code>\u00a0directives by removing the &#8220;<strong>;<\/strong>&#8220;:<\/p>\n<div class=\"code-label \" title=\"~\/client-configs\/base.conf\">~\/client-configs\/base.conf<\/div>\n<pre class=\"code-pre \"><code># Downgrade privileges after initialization (non-Windows only)\nuser nobody\ngroup nogroup\n<\/code><\/pre>\n<p>Find the directives that set the\u00a0<code>ca<\/code>,\u00a0<code>cert<\/code>, and\u00a0<code>key<\/code>. Comment out these directives since we will be adding the certs and keys within the file itself:<\/p>\n<div class=\"code-label \" title=\"~\/client-configs\/base.conf\">~\/client-configs\/base.conf<\/div>\n<pre class=\"code-pre \"><code># SSL\/TLS parms.\n# See the server config file for more\n# description.  It's best to use\n# a separate .crt\/.key file pair\n# for each client.  A single ca\n# file can be used for all clients.\n<span class=\"highlight\">#<\/span>ca ca.crt\n<span class=\"highlight\">#<\/span>cert client.crt\n<span class=\"highlight\">#<\/span>key client.key\n<\/code><\/pre>\n<p>Mirror the\u00a0<code>cipher<\/code>\u00a0and\u00a0<code>auth<\/code>\u00a0settings that we set in the\u00a0<code>\/etc\/openvpn\/server.conf<\/code>\u00a0file:<\/p>\n<div class=\"code-label \" title=\"~\/client-configs\/base.conf\">~\/client-configs\/base.conf<\/div>\n<pre class=\"code-pre \"><code><span class=\"highlight\">cipher AES-128-CBC<\/span>\n<span class=\"highlight\">auth SHA256<\/span>\n<\/code><\/pre>\n<p>Next, add the\u00a0<code>key-direction<\/code>\u00a0directive somewhere in the file. This\u00a0<strong>must<\/strong>\u00a0be set to &#8220;1&#8221; to work with the server:<\/p>\n<div class=\"code-label \" title=\"~\/client-configs\/base.conf\">~\/client-configs\/base.conf<\/div>\n<pre class=\"code-pre \"><code><span class=\"highlight\">key-direction 1<\/span>\n<\/code><\/pre>\n<p>Finally, add a few\u00a0<strong>commented out<\/strong>\u00a0lines. We want to include these with every config, but should only enable them for Linux clients that ship with a\u00a0<code>\/etc\/openvpn\/update-resolv-conf<\/code>\u00a0file. This script uses the\u00a0<code>resolvconf<\/code>\u00a0utility to update DNS information for Linux clients.<\/p>\n<div class=\"code-label \" title=\"~\/client-configs\/base.conf\">~\/client-configs\/base.conf<\/div>\n<pre class=\"code-pre \"><code><span class=\"highlight\"># script-security 2<\/span>\n<span class=\"highlight\"># up \/etc\/openvpn\/update-resolv-conf<\/span>\n<span class=\"highlight\"># down \/etc\/openvpn\/update-resolv-conf<\/span>\n<\/code><\/pre>\n<p>If your client is running Linux and has an\u00a0<code>\/etc\/openvpn\/update-resolv-conf<\/code>\u00a0file, you should uncomment these lines from the generated OpenVPN client configuration file.<\/p>\n<p>Save the file when you are finished.<\/p>\n<h3 id=\"creating-a-configuration-generation-script\">Creating a Configuration Generation Script<\/h3>\n<p>Next, we will create a simple script to compile our base configuration with the relevant certificate, key, and encryption files. This will place the generated configuration in the\u00a0<code>~\/client-configs\/files<\/code>\u00a0directory.<\/p>\n<p>Create and open a file called\u00a0<code>make_config.sh<\/code>\u00a0within the\u00a0<code>~\/client-configs<\/code>\u00a0directory:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">nano ~\/client-configs\/make_config.sh<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Inside, paste the following script:<\/p>\n<div class=\"code-label \" title=\"~\/client-configs\/make_config.sh\">~\/client-configs\/make_config.sh<\/div>\n<pre class=\"code-pre \"><code class=\"code-highlight language-sh hljs bash\"><span class=\"hljs-shebang\">#!\/bin\/bash\n<\/span>\n<span class=\"hljs-comment\"># First argument: Client identifier<\/span>\n\nKEY_DIR=~\/openvpn-ca\/keys\nOUTPUT_DIR=~\/client-configs\/files\nBASE_CONFIG=~\/client-configs\/base.conf\n\ncat <span class=\"hljs-variable\">${BASE_CONFIG}<\/span> \\\n    &lt;(<span class=\"hljs-built_in\">echo<\/span> <span class=\"hljs-operator\">-e<\/span> <span class=\"hljs-string\">'&lt;ca&gt;'<\/span>) \\\n    <span class=\"hljs-variable\">${KEY_DIR}<\/span>\/ca.crt \\\n    &lt;(<span class=\"hljs-built_in\">echo<\/span> <span class=\"hljs-operator\">-e<\/span> <span class=\"hljs-string\">'&lt;\/ca&gt;\\n&lt;cert&gt;'<\/span>) \\\n    <span class=\"hljs-variable\">${KEY_DIR}<\/span>\/<span class=\"hljs-variable\">${1}<\/span>.crt \\\n    &lt;(<span class=\"hljs-built_in\">echo<\/span> <span class=\"hljs-operator\">-e<\/span> <span class=\"hljs-string\">'&lt;\/cert&gt;\\n&lt;key&gt;'<\/span>) \\\n    <span class=\"hljs-variable\">${KEY_DIR}<\/span>\/<span class=\"hljs-variable\">${1}<\/span>.key \\\n    &lt;(<span class=\"hljs-built_in\">echo<\/span> <span class=\"hljs-operator\">-e<\/span> <span class=\"hljs-string\">'&lt;\/key&gt;\\n&lt;tls-auth&gt;'<\/span>) \\\n    <span class=\"hljs-variable\">${KEY_DIR}<\/span>\/ta.key \\\n    &lt;(<span class=\"hljs-built_in\">echo<\/span> <span class=\"hljs-operator\">-e<\/span> <span class=\"hljs-string\">'&lt;\/tls-auth&gt;'<\/span>) \\\n    &gt; <span class=\"hljs-variable\">${OUTPUT_DIR}<\/span>\/<span class=\"hljs-variable\">${1}<\/span>.ovpn\n<\/code><\/pre>\n<p>Save and close the file when you are finished.<\/p>\n<p>Mark the file as executable by typing:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">chmod 700 ~\/client-configs\/make_config.sh<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<div data-unique=\"step-11-generate-client-configurations\"><\/div>\n<h2 id=\"step-11-generate-client-configurations\">Step 11: Generate Client Configurations<\/h2>\n<p>Now, we can easily generate client configuration files.<\/p>\n<p>If you followed along with the guide, you created a client certificate and key called\u00a0<code>client1.crt<\/code>\u00a0and\u00a0<code>client1.key<\/code>\u00a0respectively by running the\u00a0<code>.\/build-key\u00a0<span class=\"highlight\">client1<\/span><\/code>\u00a0command in step 6. We can generate a config for these credentials by moving into our\u00a0<code>~\/client-configs<\/code>\u00a0directory and using the script we made:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">cd ~\/client-configs<\/li>\n<li class=\"line\">.\/make_config.sh <span class=\"highlight\">client1<\/span><\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>If everything went well, we should have a\u00a0<code>client1.ovpn<\/code>\u00a0file in our\u00a0<code>~\/client-configs\/files<\/code>directory:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">ls ~\/client-configs\/files<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<pre class=\"code-pre \"><code><\/code><\/pre>\n<div class=\"secondary-code-label \" title=\"Output\">Output<\/div>\n<pre class=\"code-pre \"><code>client1.ovpn\n<\/code><\/pre>\n<h3 id=\"transferring-configuration-to-client-devices\">Transferring Configuration to Client Devices<\/h3>\n<p>We need to transfer the client configuration file to the relevant device. For instance, this could be your local computer or a mobile device.<\/p>\n<p>While the exact applications used to accomplish this transfer will depend on your choice and device&#8217;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&#8217;s VPN authentication files over an encrypted connection.<\/p>\n<p>Here is an example SFTP command using our\u00a0<span class=\"highlight\">client1.ovpn<\/span>\u00a0example. This command can be run from your local computer (OS X or Linux). It places the\u00a0<code>.ovpn<\/code>\u00a0file in your home directory:<\/p>\n<pre class=\"code-pre custom_prefix\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sftp <span class=\"highlight\">sammy<\/span>@<span class=\"highlight\">openvpn_server_ip<\/span>:client-configs\/files\/client1.ovpn ~\/<\/li>\n<\/ul>\n<pre class=\"code-pre custom_prefix\"><code><\/code><\/pre>\n<p>Here are several tools and tutorials for securely transferring files from the server to a local computer:<\/p>\n<ul>\n<li><a href=\"http:\/\/winscp.net\/\">WinSCP<\/a><\/li>\n<li><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-use-sftp-to-securely-transfer-files-with-a-remote-server\">How To Use SFTP to Securely Transfer Files with a Remote Server<\/a><\/li>\n<li><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-use-filezilla-to-transfer-and-manage-files-securely-on-your-vps\">How To Use Filezilla to Transfer and Manage Files Securely on your VPS<\/a><\/li>\n<\/ul>\n<div data-unique=\"step-12-install-the-client-configuration\"><\/div>\n<h2 id=\"step-12-install-the-client-configuration\">Step 12: Install the Client Configuration<\/h2>\n<p>Now, we&#8217;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.<\/p>\n<p>The OpenVPN connection will be called whatever you named the\u00a0<code>.ovpn<\/code>\u00a0file. In our example, this means that the connection will be called\u00a0<code>client1.ovpn<\/code>\u00a0for the first client file we generated.<\/p>\n<h3 id=\"windows\">Windows<\/h3>\n<p><strong>Installing<\/strong><\/p>\n<p>The OpenVPN client application for Windows can be found on\u00a0<a href=\"https:\/\/openvpn.net\/index.php\/open-source\/downloads.html\">OpenVPN&#8217;s Downloads page<\/a>. Choose the appropriate installer version for your version of Windows.<\/p>\n<div class=\"code-label notes-and-warnings note\" title=\"Note\">Note<\/div>\n<p><span class=\"note\">OpenVPN needs administrative privileges to install.<br \/>\n<\/span><\/p>\n<p>After installing OpenVPN, copy the\u00a0<code>.ovpn<\/code>\u00a0file to:<\/p>\n<pre class=\"code-pre \"><code>C:\\Program Files\\OpenVPN\\config\n<\/code><\/pre>\n<p>When you launch OpenVPN, it will automatically see the profile and makes it available.<\/p>\n<p>OpenVPN must be run as an administrator each time it&#8217;s used, even by administrative accounts. To do this without having to right-click and select\u00a0<strong>Run as administrator<\/strong>\u00a0every time you use the VPN, you can preset this, but this must be done from an administrative account. This also means that standard users will need to enter the administrator&#8217;s password to use OpenVPN. On the other hand, standard users can&#8217;t properly connect to the server unless the OpenVPN application on the client has admin rights, so the elevated privileges are necessary.<\/p>\n<p>To set the OpenVPN application to always run as an administrator, right-click on its shortcut icon and go to\u00a0<strong>Properties<\/strong>. At the bottom of the\u00a0<strong>Compatibility<\/strong>\u00a0tab, click the button to\u00a0<strong>Change settings for all users<\/strong>. In the new window, check\u00a0<strong>Run this program as an administrator<\/strong>.<\/p>\n<p><strong>Connecting<\/strong><\/p>\n<p>Each time you launch the OpenVPN GUI, Windows will ask if you want to allow the program to make changes to your computer. Click\u00a0<strong>Yes<\/strong>. Launching the OpenVPN client application only puts the applet in the system tray so that the VPN can be connected and disconnected as needed; it does not actually make the VPN connection.<\/p>\n<p>Once OpenVPN is started, initiate a connection by going into the system tray applet and right-clicking on the OpenVPN applet icon. This opens the context menu. Select\u00a0<strong>client1<\/strong>\u00a0at the top of the menu (that&#8217;s our\u00a0<code>client1.ovpn<\/code>\u00a0profile) and choose\u00a0<strong>Connect<\/strong>.<\/p>\n<p>A status window will open showing the log output while the connection is established, and a message will show once the client is connected.<\/p>\n<p>Disconnect from the VPN the same way: Go into the system tray applet, right-click the OpenVPN applet icon, select the client profile and click\u00a0<strong>Disconnect<\/strong>.<\/p>\n<h3 id=\"os-x\">OS X<\/h3>\n<p><strong>Installing<\/strong><\/p>\n<p><a href=\"https:\/\/tunnelblick.net\/\">Tunnelblick<\/a>\u00a0is a free, open source OpenVPN client for Mac OS X. You can download the latest disk image from the\u00a0<a href=\"https:\/\/tunnelblick.net\/downloads.html\">Tunnelblick Downloads page<\/a>. Double-click the downloaded\u00a0<code>.dmg<\/code>\u00a0file and follow the prompts to install.<\/p>\n<p>Towards the end of the installation process, Tunnelblick will ask if you have any configuration files. It can be easier to answer\u00a0<strong>No<\/strong>\u00a0and let Tunnelblick finish. Open a Finder window and double-click\u00a0<code>client1.ovpn<\/code>. Tunnelblick will install the client profile. Administrative privileges are required.<\/p>\n<p><strong>Connecting<\/strong><\/p>\n<p>Launch Tunnelblick by double-clicking Tunnelblick in the\u00a0<strong>Applications<\/strong>\u00a0folder. Once Tunnelblick has been launched, there will be a Tunnelblick icon in the menu bar at the top right of the screen for controlling connections. Click on the icon, and then the\u00a0<strong>Connect<\/strong>\u00a0menu item to initiate the VPN connection. Select the\u00a0<strong>client1<\/strong>\u00a0connection.<\/p>\n<h3 id=\"linux\">Linux<\/h3>\n<h4 id=\"installing\">Installing<\/h4>\n<p>If you are using Linux, there are a variety of tools that you can use depending on your distribution. Your desktop environment or window manager might also include connection utilities.<\/p>\n<p>The most universal way of connecting, however, is to just use the OpenVPN software.<\/p>\n<p>On Ubuntu or Debian, you can install it just as you did on the server by typing:<\/p>\n<pre class=\"code-pre custom_prefix\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo apt-get update<\/li>\n<li class=\"line\">sudo apt-get install openvpn<\/li>\n<\/ul>\n<pre class=\"code-pre custom_prefix\"><code><\/code><\/pre>\n<p>On CentOS you can enable the EPEL repositories and then install it by typing:<\/p>\n<pre class=\"code-pre custom_prefix\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo yum install epel-release<\/li>\n<li class=\"line\">sudo yum install openvpn<\/li>\n<\/ul>\n<pre class=\"code-pre custom_prefix\"><code><\/code><\/pre>\n<h4 id=\"configuring\">Configuring<\/h4>\n<p>Check to see if your distribution includes a\u00a0<code>\/etc\/openvpn\/update-resolv-conf<\/code>\u00a0script:<\/p>\n<pre class=\"code-pre custom_prefix\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">ls \/etc\/openvpn<\/li>\n<\/ul>\n<pre class=\"code-pre custom_prefix\"><code><\/code><\/pre>\n<pre class=\"code-pre \"><code><\/code><\/pre>\n<div class=\"secondary-code-label \" title=\"Output\">Output<\/div>\n<pre class=\"code-pre \"><code>update-resolve-conf\n<\/code><\/pre>\n<p>Next, edit the OpenVPN client configuration file you transfered:<\/p>\n<pre class=\"code-pre custom_prefix\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">nano <span class=\"highlight\">client1<\/span>.ovpn<\/li>\n<\/ul>\n<pre class=\"code-pre custom_prefix\"><code><\/code><\/pre>\n<p>Uncomment the three lines we placed in to adjust the DNS settings if you were able to find an\u00a0<code>update-resolv-conf<\/code>\u00a0file:<\/p>\n<div class=\"code-label \" title=\"client1.ovpn\">client1.ovpn<\/div>\n<pre class=\"code-pre \"><code>script-security 2\nup \/etc\/openvpn\/update-resolv-conf\ndown \/etc\/openvpn\/update-resolv-conf\n<\/code><\/pre>\n<p>If you are using CentOS, change the\u00a0<code>group<\/code>\u00a0from\u00a0<code>nogroup<\/code>\u00a0to\u00a0<code>nobody<\/code>\u00a0to match the distribution&#8217;s available groups:<\/p>\n<div class=\"code-label \" title=\"client1.ovpn\">client1.ovpn<\/div>\n<pre class=\"code-pre \"><code>group <span class=\"highlight\">nobody<\/span>\n<\/code><\/pre>\n<p>Save and close the file.<\/p>\n<p>Now, you can connect to the VPN by just pointing the\u00a0<code>openvpn<\/code>\u00a0command to the client configuration file:<\/p>\n<pre class=\"code-pre custom_prefix\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo openvpn &#8211;config <span class=\"highlight\">client1<\/span>.ovpn<\/li>\n<\/ul>\n<pre class=\"code-pre custom_prefix\"><code><\/code><\/pre>\n<p>This should connect you to your server.<\/p>\n<h3 id=\"ios\">iOS<\/h3>\n<p><strong>Installing<\/strong><\/p>\n<p>From the iTunes App Store, search for and install\u00a0<a href=\"https:\/\/itunes.apple.com\/us\/app\/id590379981\">OpenVPN Connect<\/a>, the official iOS OpenVPN client application. To transfer your iOS client configuration onto the device, connect it directly to a computer.<\/p>\n<p>Completing the transfer with iTunes will be outlined here. Open iTunes on the computer and click on\u00a0<strong>iPhone<\/strong>\u00a0&gt;\u00a0<strong>apps<\/strong>. Scroll down to the bottom to the\u00a0<strong>File Sharing<\/strong>\u00a0section and click the OpenVPN app. The blank window to the right,\u00a0<strong>OpenVPN Documents<\/strong>, is for sharing files. Drag the\u00a0<code>.ovpn<\/code>\u00a0file to the OpenVPN Documents window.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/assets.digitalocean.com\/articles\/openvpn_ubunutu\/1.png\" alt=\"iTunes showing the VPN profile ready to load on the iPhone\" \/><\/p>\n<p>Now launch the OpenVPN app on the iPhone. There will be a notification that a new profile is ready to import. Tap the green plus sign to import it.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/assets.digitalocean.com\/articles\/openvpn_ubunutu\/2.png\" alt=\"The OpenVPN iOS app showing new profile ready to import\" \/><\/p>\n<p><strong>Connecting<\/strong><\/p>\n<p>OpenVPN is now ready to use with the new profile. Start the connection by sliding the\u00a0<strong>Connect<\/strong>\u00a0button to the\u00a0<strong>On<\/strong>\u00a0position. Disconnect by sliding the same button to\u00a0<strong>Off<\/strong>.<\/p>\n<div class=\"code-label notes-and-warnings note\" title=\"Note\">Note<\/div>\n<p><span class=\"note\">The VPN switch under\u00a0<strong>Settings<\/strong>\u00a0cannot be used to connect to the VPN. If you try, you will receive a notice to only connect using the OpenVPN app.<br \/>\n<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/assets.digitalocean.com\/articles\/openvpn_ubunutu\/3.png\" alt=\"The OpenVPN iOS app connected to the VPN\" \/><\/p>\n<h3 id=\"android\">Android<\/h3>\n<p><strong>Installing<\/strong><\/p>\n<p>Open the Google Play Store. Search for and install\u00a0<a href=\"https:\/\/play.google.com\/store\/apps\/details?id=net.openvpn.openvpn\">Android OpenVPN Connect<\/a>, the official Android OpenVPN client application.<\/p>\n<p>The\u00a0<code>.ovpn<\/code>\u00a0profile can be transferred by connecting the Android device to your computer by USB and copying the file over. Alternatively, if you have an SD card reader, you can remove the device&#8217;s SD card, copy the profile onto it and then insert the card back into the Android device.<\/p>\n<p>Start the OpenVPN app and tap the menu to import the profile.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/assets.digitalocean.com\/articles\/openvpn_ubunutu\/4.png\" alt=\"The OpenVPN Android app profile import menu selection\" \/><\/p>\n<p>Then navigate to the location of the saved profile (the screenshot uses\u00a0<code>\/sdcard\/Download\/<\/code>) and select the file. The app will make a note that the profile was imported.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/assets.digitalocean.com\/articles\/openvpn_ubunutu\/5.png\" alt=\"The OpenVPN Android app selecting VPN profile to import\" \/><\/p>\n<p><strong>Connecting<\/strong><\/p>\n<p>To connect, simply tap the\u00a0<strong>Connect<\/strong>\u00a0button. You&#8217;ll be asked if you trust the OpenVPN application. Choose\u00a0<strong>OK<\/strong>\u00a0to initiate the connection. To disconnect from the VPN, go back to the OpenVPN app and choose\u00a0<strong>Disconnect<\/strong>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/assets.digitalocean.com\/articles\/openvpn_ubunutu\/6.png\" alt=\"The OpenVPN Android app ready to connect to the VPN\" \/><\/p>\n<div data-unique=\"step-13-test-your-vpn-connection\"><\/div>\n<h2 id=\"step-13-test-your-vpn-connection\">Step 13: Test Your VPN Connection<\/h2>\n<p>Once everything is installed, a simple check confirms everything is working properly. Without having a VPN connection enabled, open a browser and go to\u00a0<a href=\"https:\/\/www.dnsleaktest.com\/\">DNSLeakTest<\/a>.<\/p>\n<p>The site will return the IP address assigned by your internet service provider and as you appear to the rest of the world. To check your DNS settings through the same website, click on\u00a0<strong>Extended Test<\/strong>\u00a0and it will tell you which DNS servers you are using.<\/p>\n<p>Now connect the OpenVPN client to your Droplet&#8217;s VPN and refresh the browser. The completely different IP address of your VPN server should now appear. That is now how you appear to the world. Again,\u00a0<a href=\"https:\/\/www.dnsleaktest.com\/\">DNSLeakTest&#8217;s<\/a>\u00a0<strong>Extended Test<\/strong>\u00a0will check your DNS settings and confirm you are now using the DNS resolvers pushed by your VPN.<\/p>\n<div data-unique=\"step-14-revoking-client-certificates\"><\/div>\n<h2 id=\"step-14-revoking-client-certificates\">Step 14: Revoking Client Certificates<\/h2>\n<p>Occasionally, you may need to revoke a client certificate to prevent further access to the OpenVPN server.<\/p>\n<p>To do so, enter your CA directory and re-source the\u00a0<code>vars<\/code>\u00a0file:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">cd ~\/openvpn-ca<\/li>\n<li class=\"line\">source vars<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Next, call the\u00a0<code>revoke-full<\/code>\u00a0command using the client name that you wish to revoke:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">.\/revoke-full <span class=\"highlight\">client3<\/span><\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>This will show some output, ending in\u00a0<code>error 23<\/code>. This is normal and the process should have successfully generated the necessary revocation information, which is stored in a file called\u00a0<code>crl.pem<\/code>\u00a0within the\u00a0<code>keys<\/code>subdirectory.<\/p>\n<p>Transfer this file to the\u00a0<code>\/etc\/openvpn<\/code>\u00a0configuration directory:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo cp ~\/openvpn-ca\/keys\/crl.pem \/etc\/openvpn<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Next, open the OpenVPN server configuration file:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo nano \/etc\/openvpn\/server.conf<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>At the bottom of the file, add the\u00a0<code>crl-verify<\/code>\u00a0option, so that the OpenVPN server checks the certificate revocation list that we&#8217;ve created each time a connection attempt is made:<\/p>\n<div class=\"code-label \" title=\"\/etc\/openvpn\/server.conf\">\/etc\/openvpn\/server.conf<\/div>\n<pre class=\"code-pre \"><code>crl-verify crl.pem\n<\/code><\/pre>\n<p>Save and close the file.<\/p>\n<p>Finally, restart OpenVPN to implement the certificate revocation:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo systemctl restart openvpn@server<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>The client should now longer be able to successfully connect to the server using the old credential.<\/p>\n<p>To revoke additional clients, follow this process:<\/p>\n<ol>\n<li>Generate a new certificate revocation list by sourcing the\u00a0<code>vars<\/code>\u00a0file in the\u00a0<code>~\/openvpn-ca<\/code>\u00a0directory and then calling the\u00a0<code>revoke-full<\/code>\u00a0script on the client name.<\/li>\n<li>Copy the new certificate revocation list to the\u00a0<code>\/etc\/openvpn<\/code>\u00a0directory to overwrite the old list.<\/li>\n<li>Restart the OpenVPN service.<\/li>\n<\/ol>\n<p>This process can be used to revoke any certificates that you&#8217;ve previously issued for your server.<\/p>\n<div data-unique=\"conclusion\"><\/div>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Congratulations! You are now securely traversing the internet protecting your identity, location, and traffic from snoopers and censors.<\/p>\n<p>To configure more clients, you only need to follow steps\u00a0<strong>6<\/strong>, and\u00a0<strong>11-13<\/strong>\u00a0for each additional device. To revoke access to clients, follow step\u00a0<strong>14<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Want to access the Internet safely and securely from your smartphone or laptop when connected to an untrusted network [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-338","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=338"}],"version-history":[{"count":0,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/338\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}