{"id":613,"date":"2018-10-17T20:26:33","date_gmt":"2018-10-17T20:26:33","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=613"},"modified":"2018-10-18T14:48:24","modified_gmt":"2018-10-18T14:48:24","slug":"how-to-set-up-an-openvpn-server-on-ubuntu-18-04","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/17\/how-to-set-up-an-openvpn-server-on-ubuntu-18-04\/","title":{"rendered":"How To Set Up an OpenVPN Server on Ubuntu 18.04"},"content":{"rendered":"<p>Whether you want to access the Internet safely and securely while connected on an untrusty public Wi-Fi network, bypass Geo-restricted content or allow your coworkers to connect securely to your company network when working remotely, using a VPN is the best solution.<\/p>\n<p>VPN allows you to connect to remote VPN servers, making your connection encrypted and secure and surf the web anonymously by keeping your traffic data private.<\/p>\n<p>There are many commercial VPN providers you can choose from, but you can never be truly sure that the provider is not logging your activity. The safest option is to set up your own VPN server.<\/p>\n<p>This tutorial will walk you through the process of setting up your own VPN server by installing and configuring OpenVPN. We will also show you how to generate clients certificates and create configuration files<\/p>\n<p>OpenVPN is a fully featured, open-source Secure Socket Layer (SSL) VPN solution. It implements OSI layer 2 or 3 secure network extension using the SSL\/TLS protocol.<\/p>\n<h2>Prerequisites<\/h2>\n<p>To complete this tutorial, you will need:<\/p>\n<ul>\n<li><a href=\"\/post\/how-to-create-a-sudo-user-on-ubuntu\/\">Sudo access<\/a> to an Ubuntu 18.04 server to host your OpenVPN instance.<\/li>\n<li>The server should have a basic <a href=\"\/post\/how-to-setup-a-firewall-with-ufw-on-ubuntu-18-04\/\">UFW firewall<\/a> configured.<\/li>\n<li>Separate dedicated machine to serve as your CA (certificate authority). If you don\u2019t want to use a dedicated machine for your CA, you can build the CA on your OpenVPN server or your local machine. Once you are done building the CA it\u2019s recommended to move the CA directory somewhere secure or offline.<\/li>\n<\/ul>\n<p>This tutorial assumes that the CA is on a separate Ubuntu 18.04 machine. The same steps (with small modifications) will apply if you\u2019re using your server as a CA.<\/p>\n<p>The reason why we are using a separate CA machine, is to prevent attackers to infiltrate the server. If an attacker manages to access the CA private key they could use it to sign new certificates, which will give them access to the VPN server.<\/p>\n<h2>Building CA with EasyRSA<\/h2>\n<p>When setting up a new OpenVPN server the first step is to build a Public Key Infrastructure (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Public_key_infrastructure\">PKI<\/a>). To do so we\u2019ll need to create the following:<\/p>\n<ul>\n<li>A Certificate Authority (CA) certificate and private key.<\/li>\n<li>A separate certificate and private key pair for the server issued by our CA.<\/li>\n<li>A separate certificate and private key pair for each client issued by our CA.<\/li>\n<\/ul>\n<p>As mentioned in the prerequisites for security reasons, we\u2019ll build the CA on a standalone machine.<\/p>\n<p>To create CA, certificates requests and sign certificates we will use a CLI utility named EasyRSA.<\/p>\n<p>Perform the following steps on your CA machine.<\/p>\n<ol>\n<li>First, download the latest release of EasyRSA from the project <a href=\"https:\/\/github.com\/OpenVPN\/easy-rsa\">Github repository<\/a> with the following <a href=\"\/post\/wget-command-examples\/\">wget<\/a> command:\n<p>cd &amp;&amp; wget https:\/\/github.com\/OpenVPN\/easy-rsa\/releases\/download\/v3.0.5\/EasyRSA-nix-3.0.5.tgz<\/li>\n<li>Once the download is completed <a href=\"\/post\/how-to-create-and-extract-archives-using-the-tar-command-in-linux\/\">extract the archive<\/a> with:\n<p>tar xzf EasyRSA-nix-3.0.5.tgz<\/li>\n<li>Switch to the EasyRSA directory and create a configuration file named vars by copying the vars.example file:\n<p>cd ~\/EasyRSA-3.0.5\/<br \/>\ncp vars.example vars<\/li>\n<li>Open the file and uncomment and update the following entries to match your information.\n<p>nano ~\/EasyRSA-3.0.5\/vars<\/p>\n<p>~\/EasyRSA-3.0.5\/vars<\/p>\n<p>set_var EASYRSA_REQ_COUNTRY &#8220;US&#8221;<br \/>\nset_var EASYRSA_REQ_PROVINCE &#8220;Pennsylvania&#8221;<br \/>\nset_var EASYRSA_REQ_CITY &#8220;Pittsburgh&#8221;<br \/>\nset_var EASYRSA_REQ_ORG &#8220;Linuxize&#8221;<br \/>\nset_var EASYRSA_REQ_EMAIL &#8220;<a href=\"\/cdn-cgi\/l\/email-protection\">[email protected]<\/a>&#8221;<br \/>\nset_var EASYRSA_REQ_OU &#8220;Community&#8221;<\/li>\n<li>Before generating a CA keypair first we need to initialize a new PKI with:\n<p>init-pki complete; you may now create a CA or requests.<br \/>\nYour newly created PKI dir is: \/home\/causer\/EasyRSA-3.0.5\/pki<\/li>\n<li>The next step is to build the CA:\n<p>If you don\u2019t want to be prompted for a password each time you sign your certificates, run the build-ca command using the nopass option: .\/easyrsa build-ca nopass.<\/p>\n<p>&#8230;<br \/>\nEnter PEM pass phrase:<br \/>\nVerifying &#8211; Enter PEM pass phrase:<br \/>\n&#8212;&#8211;<br \/>\n&#8230;<br \/>\n&#8212;&#8211;<br \/>\nCommon Name (eg: your user, host, or server name) [Easy-RSA CA]:<\/p>\n<p>CA creation complete and you may now import and sign cert requests.<br \/>\nYour new CA certificate file for publishing is at:<br \/>\n\/home\/causer\/EasyRSA-3.0.5\/pki\/ca.crt<\/p>\n<p>You\u2019ll be asked to set a password for the CA key and enter a common name for your CA.<\/p>\n<p>Once completed, the script will create two files \u2014 CA public certificate ca.crt and CA private key ca.key.<\/p>\n<p>Now that the Certificate Authority (CA) is created, you can use it to sign certificate requests for one or multiple OpenVPN servers and clients.<\/li>\n<\/ol>\n<h2>Installing OpenVPN and EasyRSA<\/h2>\n<p>Our next step is to install the OpenVPN package which is available in Ubuntu\u2019s repositories and download the latest version of EasyRSA.<\/p>\n<p>The following steps are performed on the OpenVPN server.<\/p>\n<ol>\n<li>OpenVPN installation is pretty straightforward, just run the following commands on the OpenVPN server:\n<p>sudo apt update<br \/>\nsudo apt install openvpn<\/li>\n<li>Download the latest release of EasyRSA:\n<p>cd &amp;&amp; wget https:\/\/github.com\/OpenVPN\/easy-rsa\/releases\/download\/v3.0.5\/EasyRSA-nix-3.0.5.tgz<\/p>\n<p>Once the download is completed type the following command to extract the archive:<\/p>\n<p>tar xzf EasyRSA-nix-3.0.5.tgz<\/p>\n<p>Although we have already initialized a PKI on the CA machine, we also need to create a new PKI on the OpenVPN server. To do so, use the same commands as before:<\/p>\n<p>cd ~\/EasyRSA-3.0.5\/<br \/>\n.\/easyrsa init-pki<\/p>\n<p>If you still wonder why we need two EasyRSA installations, it is because we will use this EasyRSA instance to generate certificate requests which will be signed using the EasyRSA instance on the CA machine.<\/p>\n<p>It may sound complicated, and little confusing but once you read the whole tutorial you\u2019ll see that it really isn\u2019t complicated.<\/li>\n<\/ol>\n<h2>Creating Diffie-Hellman and HMAC keys<\/h2>\n<p>In this section we will generate a strong Diffie-Hellman key which will be used during the key exchange and a HMAC signature file to add an additional layer of security to the connection.<\/p>\n<ol>\n<li>First navigate to the EasyRSA directory on your OpenVPN server.<\/li>\n<li>Generate a Diffie-Hellman key:\n<p>The script will generate 2048-bit long DH parameters. This can take some time, especially on servers with little resources. Once completed the following message will be printed on your screen:<\/p>\n<p>DH parameters of size 2048 created at \/home\/serveruser\/EasyRSA-3.0.5\/pki\/dh.pem<\/p>\n<p>Copy the dh.pem file to the \/etc\/openvpn directory:<\/p>\n<p>sudo cp ~\/EasyRSA-3.0.5\/pki\/dh.pem \/etc\/openvpn\/<\/li>\n<li>Generate a HMAC signature:\n<p>sudo openvpn &#8211;genkey &#8211;secret ta.key<\/p>\n<p>Once completed copy the ta.key file to the \/etc\/openvpn directory:<\/p>\n<p>sudo cp ~\/EasyRSA-3.0.5\/ta.key \/etc\/openvpn\/<\/li>\n<\/ol>\n<h2>Creating Server Certificate and Private Key<\/h2>\n<p>This section describes how to generate a private key and certificate request for the OpenVPN server.<\/p>\n<ol>\n<li>Navigate to the EasyRSA directory on your OpenVPN server and generate a new private key for the server and a certificate request file:\n<p>cd ~\/EasyRSA-3.0.5\/<br \/>\n.\/easyrsa gen-req server1 nopass<\/p>\n<p>We are using the nopass argument because we want to start the OpenVPN server without a password input. Also in this example we are using server1 as a server name (entity) identifier. If you choose a different name for your server don\u2019t forget to adjust the instructions below where the server name is used.<\/p>\n<p>The command will create two files, a private key (server1.key) and a certificate request file (server1.req).<\/p>\n<p>&#8212;&#8211;<br \/>\nCommon Name (eg: your user, host, or server name) [server1]:<\/p>\n<p>Keypair and certificate request completed. Your files are:<br \/>\nreq: \/home\/serveruser\/EasyRSA-3.0.5\/pki\/reqs\/server1.req<br \/>\nkey: \/home\/serveruser\/EasyRSA-3.0.5\/pki\/private\/server1.key<\/li>\n<li>Copy the private key to the \/etc\/openvpn directory:\n<p>sudo cp ~\/EasyRSA-3.0.5\/pki\/private\/server1.key \/etc\/openvpn\/<\/li>\n<li>Transfer the certificate request file to your CA machine:\n<p>In this example we are using <a href=\"\/post\/how-to-use-scp-command-to-securely-transfer-files\/\">scp<\/a> to transfer the file, you can also use <a href=\"\/post\/how-to-use-rsync-for-local-and-remote-data-transfer-and-synchronization\/\">rsync<\/a> over ssh or any other secure method.<\/li>\n<li>Login to your CA machine, switch to the EasyRSA directory and import the certificate request file:\n<p>cd ~\/EasyRSA-3.0.5<br \/>\n.\/easyrsa import-req \/tmp\/server1.req server1<\/p>\n<p>The first argument is the path to the certificate request file and the second one is the server short (entity) name. In our case the server name is server1.<\/p>\n<p>The request has been successfully imported with a short name of: server1<br \/>\nYou may now use this name to perform signing operations on this request.<\/p>\n<p>This command just copies the request file into the pki\/reqs directory.<\/li>\n<li>While still in the EasyRSA directory on CA machine run the following command to sign the request:\n<p>cd ~\/EasyRSA-3.0.5<br \/>\n.\/easyrsa sign-req server server1<\/p>\n<p>The first argument can either be server or client and the second one is the server short (entity) name.<\/p>\n<p>You\u2019ll be prompted to verify that the request comes from a trusted source. Type yes and press enter to confirm:<\/p>\n<p>You are about to sign the following certificate.<br \/>\nPlease check over the details shown below for accuracy. Note that this request<br \/>\nhas not been cryptographically verified. Please be sure it came from a trusted<br \/>\nsource or that you have verified the request checksum with the sender.<\/p>\n<p>Request subject, to be signed as a server certificate for 1080 days:<\/p>\n<p>subject=<br \/>\ncommonName = server1<\/p>\n<p>Type the word &#8216;yes&#8217; to continue, or any other input to abort.<br \/>\nConfirm request details: yes<br \/>\n&#8230;<\/p>\n<p>If your CA key is password protected, you\u2019ll be prompted to enter the password. Once verified the script will generate the SSL certificate and print the full path to it.<\/p>\n<p>&#8230;<br \/>\nCertificate is to be certified until Sep 17 10:54:48 2021 GMT (1080 days)<\/p>\n<p>Write out database with 1 new entries<br \/>\nData Base Updated<\/p>\n<p>Certificate created at: \/home\/causer\/EasyRSA-3.0.5\/pki\/issued\/server1.crt<\/li>\n<li>Next step is to transfer the signed certificate server1.crt and ca.crt files back to your OpenVPN server. Again you can use scp, rsync or any other secure method:<\/li>\n<li>Login to your OpenVPN server, and move the server1.crt and ca.crt files into the \/etc\/openvpn\/ directory:\n<p>sudo mv \/tmp\/.crt \/etc\/openvpn\/<\/li>\n<\/ol>\n<p>Upon completing the steps outlined in this section, you should have the following new files on your OpenVPN server:<\/p>\n<ul>\n<li>\/etc\/openvpn\/ca.crt<\/li>\n<li>\/etc\/openvpn\/dh.pem<\/li>\n<li>\/etc\/openvpn\/ta.key<\/li>\n<li>\/etc\/openvpn\/server1.crt<\/li>\n<li>\/etc\/openvpn\/server1.key<\/li>\n<\/ul>\n<h2>Configuring the OpenVPN Service<\/h2>\n<p>Now that you have the server certificate signed by your CA and transferred to your OpenVPN server, it\u2019s time to configure the OpenVNP service.<\/p>\n<p>We will use the sample configuration file provided with OpenVNP installation package as a starting point and then add our own custom configuration options to it.<\/p>\n<p>Start by extracting the configuration file to the \/etc\/openvpn\/ directory:<\/p>\n<p>sudo sh -c &#8220;gunzip -c \/usr\/share\/doc\/openvpn\/examples\/sample-config-files\/server.conf.gz &gt; \/etc\/openvpn\/server1.conf&#8221;<\/p>\n<p>Open the file with your favorite <a href=\"\/post\/how-to-install-sublime-text-3-on-ubuntu-18-04\/\">text editor<\/a>:<\/p>\n<p>sudo nano \/etc\/openvpn\/server1.conf<\/p>\n<ul>\n<li>Find the Certificate, Key and DH parameters directives and change the file names:\n<p>\/etc\/openvpn\/server1.conf<\/p>\n<p>cert server1.crt<br \/>\nkey server1.key<\/p>\n<p>dh dh.pem<\/li>\n<li>To redirect the clients traffic through the VPN find and uncomment the redirect-gateway and dhcp-option options:\n<p>\/etc\/openvpn\/server1.conf<\/p>\n<p>push &#8220;redirect-gateway def1 bypass-dhcp&#8221;<\/p>\n<p>push &#8220;dhcp-option DNS 208.67.222.222&#8221;<br \/>\npush &#8220;dhcp-option DNS 208.67.220.220&#8221;<\/p>\n<p>By default OpenDNS resolvers are used. You can change it and use CloudFlare, Google or any other DNS resolvers you want.<\/li>\n<li>Find the user and group directives and uncomment these settings by removing the \u201c;\u201d at the beginning of each line:\n<p>\/etc\/openvpn\/server1.conf<\/p>\n<p>user nobody<br \/>\ngroup nogroup<\/li>\n<li>Append the following line at the end of the file. This directive will change the message authentication algorithm (HMAC) from SHA1 to SHA256\n<p>\/etc\/openvpn\/server1.conf<\/li>\n<\/ul>\n<p>Once you are done, the server configuration file (excluding comments) should look something like this:<\/p>\n<p>\/etc\/openvpn\/server1.conf<\/p>\n<p>port 1194<br \/>\nproto udp<br \/>\ndev tun<br \/>\nca ca.crt<br \/>\ncert server1.crt<br \/>\nkey server1.key # This file should be kept secret<br \/>\ndh dh.pem<br \/>\nserver 10.8.0.0 255.255.255.0<br \/>\nifconfig-pool-persist \/var\/log\/openvpn\/ipp.txt<br \/>\npush &#8220;redirect-gateway def1 bypass-dhcp&#8221;<br \/>\npush &#8220;dhcp-option DNS 208.67.222.222&#8221;<br \/>\npush &#8220;dhcp-option DNS 208.67.220.220&#8221;<br \/>\nkeepalive 10 120<br \/>\ntls-auth ta.key 0 # This file is secret<br \/>\ncipher AES-256-CBC<br \/>\nuser nobody<br \/>\ngroup nogroup<br \/>\npersist-key<br \/>\npersist-tun<br \/>\nstatus \/var\/log\/openvpn\/openvpn-status.log<br \/>\nverb 3<br \/>\nexplicit-exit-notify 1<br \/>\nauth SHA256<\/p>\n<h2>Starting OpenVPN Service<\/h2>\n<p>In this tutorial we\u2019ve used server1.conf as a configuration file. To start the OpenVPN service with this configuration we need to specify the configuration file name after the systemd unit file name:<\/p>\n<p>On your OpenVPN server run the following command to start the OpenVPN service:<\/p>\n<p>Verify whether the service has started successfully by typing:<\/p>\n<p>If the service is active and running, the output will look something like this:<\/p>\n<p>\u25cf <a href=\"\/cdn-cgi\/l\/email-protection\">[email protected]<\/a> &#8211; OpenVPN connection to server1<br \/>\nLoaded: loaded (\/lib\/systemd\/system\/<a href=\"\/cdn-cgi\/l\/email-protection\">[email protected]<\/a>; disabled; vendor preset: enabled)<br \/>\nActive: active (running) since Mon 2018-10-08 20:11:57 UTC; 6min ago<br \/>\nDocs: man:openvpn(8)<br \/>\nhttps:\/\/community.openvpn.net\/openvpn\/wiki\/Openvpn24ManPage<br \/>\nhttps:\/\/community.openvpn.net\/openvpn\/wiki\/HOWTO<br \/>\nMain PID: 26739 (openvpn)<br \/>\nStatus: &#8220;Initialization Sequence Completed&#8221;<\/p>\n<p>Enable the service to automatically start on boot with:<\/p>\n<p>If the OpenVPN service fails to s tart check the logs with sudo journalctl -u <a href=\"\/cdn-cgi\/l\/email-protection\">[email protected]<\/a><\/p>\n<p>When starting, the OpenVPN Server creates a tun device tun0. To check if the device is available type:<\/p>\n<p>The output should look something like this:<\/p>\n<p>4: tun0: &lt;POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP&gt; mtu 1500 qdisc fq state UNKNOWN group default qlen 100<br \/>\nlink\/none<br \/>\ninet 10.8.0.1 peer 10.8.0.2\/32 scope global tun0<br \/>\nvalid_lft forever preferred_lft forever<br \/>\ninet6 fe80::1627:9a20:bca8:e6a5\/64 scope link stable-privacy<br \/>\nvalid_lft forever preferred_lft forever<\/p>\n<p>At this point, your OpenVPN server is configured and running properly.<\/p>\n<h2>Firewall and Server Networking Configuration<\/h2>\n<p>In order to forward network packets properly we need to enable IP forwarding.<\/p>\n<p>The following steps are performed on the OpenVPN server.<\/p>\n<p>Open the \/etc\/sysctl.conf file and add or uncomment the line which reads net.ipv4.ip_forward = 0:<\/p>\n<p>sudo nano \/etc\/sysctl.conf<\/p>\n<p>\/etc\/sysctl.conf<\/p>\n<p># Uncomment the next line to enable packet forwarding for IPv4<br \/>\nnet.ipv4.ip_forward=1<\/p>\n<p>Once you are finished, save and close the file.<\/p>\n<p>Apply the new settings by running the following command:<\/p>\n<p>If you followed the prerequisites, you should already have a <a href=\"\/post\/how-to-setup-a-firewall-with-ufw-on-ubuntu-18-04\/\">UFW firewall<\/a> running on your server.<\/p>\n<p>Now we need to add firewall rules to enable masquerading. This will allow traffic to leave the VPN, giving your VPN clients access to the Internet.<\/p>\n<p>Before adding the rules you need to know the public network interface of your Ubuntu OpenVPN Server. You can easily find the interface by running the following command:<\/p>\n<p>ip -o -4 route show to default | awk &#8221;<\/p>\n<p>In our case the interface is named ens3 as shown on the output bellow. Your interface will probably have a different name.<\/p>\n<p>By default, when using UFW the forwarded packets are dropped. We\u2019ll need to change that and instruct our firewall to allow forwarded packets.<\/p>\n<p>Open the UFW configuration file, locate the DEFAULT_FORWARD_POLICY key and change the value from DROP to ACCEPT:<\/p>\n<p>sudo nano \/etc\/default\/ufw<\/p>\n<p>\/etc\/default\/ufw<\/p>\n<p>&#8230;<br \/>\n# Set the default forward policy to ACCEPT, DROP or REJECT. Please note that<br \/>\n# if you change this you will most likely want to adjust your rules<br \/>\nDEFAULT_FORWARD_POLICY=&#8221;ACCEPT&#8221;<br \/>\n&#8230;<\/p>\n<p>Next, we need to set the default policy for the POSTROUTING chain in the nat table and set the masquerade rule.<\/p>\n<p>To do so, open the \/etc\/ufw\/before.rules file and append the lines highlighted in yellow as shown below.<\/p>\n<p>sudo nano \/etc\/ufw\/before.rules<\/p>\n<p>Don\u2019t forget to replace ens3 in the -A POSTROUTING line to match the name of public network interface you found in the previous command. Paste the lines after the last line starting with COMMIT.<\/p>\n<p>\/etc\/ufw\/before.rules<\/p>\n<p>&#8230;<br \/>\n# don&#8217;t delete the &#8216;COMMIT&#8217; line or these rules won&#8217;t be processed<br \/>\nCOMMIT<\/p>\n<p>#NAT table rules<br \/>\n*nat<br \/>\n:POSTROUTING ACCEPT [0:0]<\/p>\n<p># Forward traffic through ens3 &#8211; Change to public network interface<br \/>\n-A POSTROUTING -s 10.8.0.0\/16 -o ens3 -j MASQUERADE<\/p>\n<p># don&#8217;t delete the &#8216;COMMIT&#8217; line or these rules won&#8217;t be processed<br \/>\nCOMMIT<\/p>\n<p>When you are done, save and close the file.<\/p>\n<p>We also need to open UDP traffic on port 1194 which is the default OpenVPN port. To do so, run the following command:<\/p>\n<p>In case you forgot to open the SSH port, to avoid being locked out run the following command to open the port:<\/p>\n<p>Finally reload the UFW rules by disabling and re-enabling UFW:<\/p>\n<p>sudo ufw disable<br \/>\nsudo ufw enable<\/p>\n<p>To verify the changes run the following command to list the POSTROUTING rules:<\/p>\n<p>sudo iptables -nvL POSTROUTING -t natChain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)<br \/>\npkts bytes target prot opt in out source destination<br \/>\n0 0 MASQUERADE all &#8212; * ens3 10.8.0.0\/16 0.0.0.0\/0<\/p>\n<h2>Creating the Client Configuration Infrastructure<\/h2>\n<p>In this tutorial we\u2019ll create a separate SSL certificate and generate a different configuration file for each VPN client.<\/p>\n<p>The client private key and certificate request can be generated either on the client machine or on the server. For simplicity we will generate the certificate request on the server and then send it to the CA to be signed.<\/p>\n<p>The whole process of generating the client certificate and configuration file is as follows:<\/p>\n<ol>\n<li>Generate private key and certificate request on the OpenVPN server.<\/li>\n<li>Send the request to the CA machine to be signed.<\/li>\n<li>Copy the signed SSL certificate to the OpenVPN server and generate a configuration file.<\/li>\n<li>Send the configuration file to the VPN client machine.<\/li>\n<\/ol>\n<p>Start by creating a set of directories to store the clients files:<\/p>\n<p>mkdir -p ~\/openvpn-clients\/<\/p>\n<ul>\n<li>base directory will store the base files and configuration that will be shared across all client files.<\/li>\n<li>configs directory will store the generated client configuration.<\/li>\n<li>files directory will store client specific certificate\/key pair.<\/li>\n<\/ul>\n<p>Copy the ca.crt and ta.key files to the ~\/openvpn-clients\/base directory:<\/p>\n<p>cp ~\/EasyRSA-3.0.5\/ta.key ~\/openvpn-clients\/base\/<br \/>\ncp \/etc\/openvpn\/ca.crt ~\/openvpn-clients\/base\/<\/p>\n<p>Next copy the sample VPN client configuration file into the client-~\/openvpn-clients\/base directory. We will use this file as a base configuration:<\/p>\n<p>cp \/usr\/share\/doc\/openvpn\/examples\/sample-config-files\/client.conf ~\/openvpn-clients\/base\/<\/p>\n<p>Now we need to edit the file to match our server settings and configuration. Open the configuration file with your text editor:<\/p>\n<p>nano ~\/openvpn-clients\/base.conf<\/p>\n<ul>\n<li>Find the remote directive and change the default placeholder with the public IP address of your OpenVPN server:\n<p>~\/openvpn-clients\/base.conf<\/p>\n<p># The hostname\/IP and port of the server.<br \/>\n# You can have multiple remote entries<br \/>\n# to load balance between the servers.<br \/>\nremote 45.76.22.45 1194<\/li>\n<li>Locate and comment the ca, cert, and key directives. The certs and keys will be added within the configuration file:\n<p>~\/openvpn-clients\/base.conf<\/p>\n<p># SSL\/TLS parms.<br \/>\n# See the server config file for more<br \/>\n# description. It&#8217;s best to use<br \/>\n# a separate .crt\/.key file pair<br \/>\n# for each client. A single ca<br \/>\n# file can be used for all clients.<br \/>\n# ca ca.crt<br \/>\n# cert client.crt<br \/>\n# key client.key<\/li>\n<li>Append the following line at the end of the file to match the server settings:\n<p>~\/openvpn-clients\/base.conf<\/li>\n<\/ul>\n<p>Once you are done, the server configuration file should look something like this:<\/p>\n<p>~\/openvpn-clients\/base.conf<\/p>\n<p>client<br \/>\ndev tun<br \/>\nproto udp<br \/>\nremote 45.76.22.45 1194<br \/>\nresolv-retry infinite<br \/>\nnobind<br \/>\npersist-key<br \/>\npersist-tun<br \/>\nremote-cert-tls server<br \/>\ntls-auth ta.key 1<br \/>\ncipher AES-256-CBC<br \/>\nverb 3<br \/>\nauth SHA256<br \/>\nkey-direction 1<\/p>\n<p>Next, create a simple bash script that will merge the base configuration and files with the client certificate and key, and store the generated configuration in the ~\/openvpn-clients\/configs directory.<\/p>\n<p>Open your text editor and create the following script:<\/p>\n<p>nano ~\/openvpn-clients\/gen_config.sh<\/p>\n<p>~\/openvpn-clients\/gen_config.sh<\/p>\n<p>#!\/bin\/bash<\/p>\n<p>FILES_DIR=$HOME\/openvpn-clients\/files<br \/>\nBASE_DIR=$HOME\/openvpn-clients\/base<br \/>\nCONFIGS_DIR=$HOME\/openvpn-clients\/configs<\/p>\n<p>BASE_CONF=$\/client.conf<br \/>\nCA_FILE=$\/ca.crt<br \/>\nTA_FILE=$\/ta.key<\/p>\n<p>CLIENT_CERT=$\/$.crt<br \/>\nCLIENT_KEY=$\/$.key<\/p>\n<p># Test for files<br \/>\nfor i in &#8220;$BASE_CONF&#8221; &#8220;$CA_FILE&#8221; &#8220;$TA_FILE&#8221; &#8220;$CLIENT_CERT&#8221; &#8220;$CLIENT_KEY&#8221;; do<br \/>\nif [[ ! -f $i ]]; then<br \/>\necho &#8221; The file $i does not exist&#8221;<br \/>\nexit 1<br \/>\nfi<\/p>\n<p>if [[ ! -r $i ]]; then<br \/>\necho &#8221; The file $i is not readable.&#8221;<br \/>\nexit 1<br \/>\nfi<br \/>\ndone<\/p>\n<p># Generate client config<br \/>\ncat &gt; $\/$.ovpn &lt;&lt;EOF<br \/>\n$(cat $)<br \/>\n&lt;key&gt;<br \/>\n$(cat $)<br \/>\n&lt;\/key&gt;<br \/>\n&lt;cert&gt;<br \/>\n$(cat $)<br \/>\n&lt;\/cert&gt;<br \/>\n&lt;ca&gt;<br \/>\n$(cat $)<br \/>\n&lt;\/ca&gt;<br \/>\n&lt;tls-auth&gt;<br \/>\n$(cat $)<br \/>\n&lt;\/tls-auth&gt;<br \/>\nEOF<\/p>\n<p>Save the file and make it executable by running:<\/p>\n<p>chmod u+x ~\/openvpn-clients\/gen_config.sh<\/p>\n<h2>Creating Client Certificate Private Key and Configuration<\/h2>\n<p>The process of generating a client private key and certificate request is same as we did when generating a server key and certificate request.<\/p>\n<p>As we already mentioned in the previous section, we\u2019ll generate the client private key and certificate request on the OpenVPN server. In this example the name of or firs VPN client will be client1.<\/p>\n<ol>\n<li>Navigate to the EasyRSA directory on your OpenVPN server and generate a new private key and a certificate request file for the client:\n<p>cd ~\/EasyRSA-3.0.5\/<br \/>\n.\/easyrsa gen-req client1 nopass<\/p>\n<p>The command will create two files, a private key (client1.key) and a certificate request file (client1.req).<\/p>\n<p>Common Name (eg: your user, host, or server name) [client1]:<\/p>\n<p>Keypair and certificate request completed. Your files are:<br \/>\nreq: \/home\/serveruser\/EasyRSA-3.0.5\/pki\/reqs\/client1.req<br \/>\nkey: \/home\/serveruser\/EasyRSA-3.0.5\/pki\/private\/client1.key<\/li>\n<li>Copy the private key client1.key to the ~\/openvpn-clients\/files directory you created in the previous section:\n<p>cp ~\/EasyRSA-3.0.5\/pki\/private\/client1.key ~\/openvpn-clients\/files\/<\/li>\n<li>Transfer the certificate request file to your CA machine:\n<p>In this example we are using <a href=\"\/post\/how-to-use-scp-command-to-securely-transfer-files\/\">scp<\/a> to transfer the file, you can also use <a href=\"\/post\/how-to-use-rsync-for-local-and-remote-data-transfer-and-synchronization\/\">rsync<\/a> over ssh or any other secure method.<\/li>\n<li>Login to your CA machine, switch to the EasyRSA directory and import the certificate request file:\n<p>cd ~\/EasyRSA-3.0.5<br \/>\n.\/easyrsa import-req \/tmp\/client1.req client1<\/p>\n<p>The first argument is the path to the certificate request file and the second one is the client name.<\/p>\n<p>The request has been successfully imported with a short name of: client1<br \/>\nYou may now use this name to perform signing operations on this request.<\/li>\n<li>From within the EasyRSA directory on CA machine run the following command to sign the request:\n<p>cd ~\/EasyRSA-3.0.5<br \/>\n.\/easyrsa sign-req client client1<\/p>\n<p>You\u2019ll be prompted to verify that the request comes from a trusted source. Type yes and press enter to confirm:<\/p>\n<p>If your CA key is password protected, you\u2019ll be prompted to enter the password. Once verified the script will generate the SSL certificate and print the full path to it.<\/p>\n<p>&#8230;<br \/>\nCertificate created at: \/home\/causer\/EasyRSA-3.0.5\/pki\/issued\/client1.crt<\/li>\n<li>Next, transfer the signed certificate client1.crt file back to your OpenVPN server. You can use <a href=\"\/post\/how-to-use-scp-command-to-securely-transfer-files\/\">scp<\/a>, <a href=\"\/post\/how-to-use-rsync-for-local-and-remote-data-transfer-and-synchronization\/\">rsync<\/a> or any other secure method:<\/li>\n<li>Login to your OpenVPN server, and move the client1.crt file into the ~\/openvpn-clients\/files directory:\n<p>mv \/tmp\/client1.crt ~\/openvpn-clients\/files<\/li>\n<li>The final step is to generate a client configuration using the gen_config.sh script. Switch to the ~\/openvpn-clients directory and run the script using the client name as an argument:\n<p>cd ~\/openvpn-clients<br \/>\n.\/gen_config.sh client1<\/p>\n<p>The script will create a file named client1.ovpn in the ~\/client-configs\/configs directory. You can check by listing the directory:<\/p>\n<p>ls ~\/client-configs\/configs<\/li>\n<\/ol>\n<p>At this point the client configuration is created. You can now transfer the configuration file to the device you intend to use as a client.<\/p>\n<p>For example to transfer the configuration file to your local machine with scp you should run the following command:<\/p>\n<p>sftp ~\/client-configs\/files\/client1.ovpn your_local_ip:\/<\/p>\n<p>To add additional clients, just repeat the same steps.<\/p>\n<h2>Connecting Clients<\/h2>\n<h3>Linux<\/h3>\n<p>Your distribution or desktop environment may provide a tool or graphic user interface to connect to OpenVPN servers. In this turtorial we will show you how to connect to the server using the openvpn tool.<\/p>\n<ul>\n<li>Install OpenVPN on Ubuntu and Debian\n<p>sudo apt update<br \/>\nsudo apt install openvpn<\/li>\n<li>Install OpenVPN on CentOS and Fedora\n<p>sudo yum install epel-release<br \/>\nsudo yum install openvpn<\/li>\n<\/ul>\n<p>Once the package is installed, to connect to the VPN server use the openvpn command and specify the client configuration file:<\/p>\n<p>sudo openvpn &#8211;config client1.ovpn<\/p>\n<h3>macOS<\/h3>\n<p><a href=\"https:\/\/tunnelblick.net\/\">Tunnelblick<\/a> is a free, open source graphic user interface for OpenVPN on OS X and macOS.<\/p>\n<h3>Windows<\/h3>\n<p>Download and install the latest build of OpenVPN application the <a href=\"https:\/\/openvpn.net\/community-downloads\/\">OpenVPN\u2019s Downloads page<\/a>.<\/p>\n<p>Copy the .ovpn file to to the OpenVPN config folder (Users&lt;Name&gt;OpenVPNConfig or Program FilesOpenVPNconfig).<\/p>\n<p>Launch the OpenVPN application.<\/p>\n<p>Right click on the OpenVPN system tray icon and the name of OpenVPN configiration file you copied will be listed on the menu. Click Connect.<\/p>\n<h3>Android &amp; iOS<\/h3>\n<p>A VPN application developed by OpenVPN is available for both Android and iOS. Install the application and import the client .ovp file.<\/p>\n<ul>\n<li><a href=\"https:\/\/play.google.com\/store\/apps\/details?id=net.openvpn.openvpn\">Android OpenVPN Connect<\/a><\/li>\n<li><a href=\"https:\/\/itunes.apple.com\/us\/app\/openvpn-connect\/id590379981\">iOS OpenVPN Connect<\/a><\/li>\n<\/ul>\n<h2>Revoking Client Certificates<\/h2>\n<p>Revoking a certificate means to invalidate a signed certificate so that it can no longer be used for accessing the OpenVPN server.<\/p>\n<p>To revoke a client certificate follow the steps below:<\/p>\n<ol>\n<li>Login to your CA machine and switch to the EasyRSA directory:<\/li>\n<li>Run the easyrsa script using the revoke argument, followed by the client name you want to revoke:\n<p>You\u2019ll be prompted to verify that you wish to revoke the certificate. Type yes and press enter to confirm:<\/p>\n<p>Please confirm you wish to revoke the certificate with the following subject:<\/p>\n<p>subject=<br \/>\ncommonName = client1<\/p>\n<p>Type the word &#8216;yes&#8217; to continue, or any other input to abort.<br \/>\nContinue with revocation: yes<br \/>\n&#8230;<\/p>\n<p>If your CA key is password protected, you\u2019ll be prompted to enter the password. Once verified the script will revoke the certificate.<\/p>\n<p>&#8230;<br \/>\nRevocation was successful. You must run gen-crl and upload a CRL to your<br \/>\ninfrastructure in order to prevent the revoked cert from being accepted.<\/li>\n<li>Use the gen-crl option to generate a certificate revocation list (CRL):\n<p>An updated CRL has been created.<br \/>\nCRL file: \/home\/causer\/EasyRSA-3.0.5\/pki\/crl.pem<\/li>\n<li>Upload the CRL file to the OpenVPN server:<\/li>\n<li>Login to your OpenVPN server server and move the file to the \/etc\/openvpn directory:\n<p>sudo mv \/tmp\/crl.pem \/etc\/openvpn<\/li>\n<li>Open the OpenVPN server configuration file:\n<p>sudo nano \/etc\/openvpn\/server1.conf<\/p>\n<p>Paste the following line at the end of the file<\/p>\n<p>~\/openvpn-clients\/gen_config.sh<\/p>\n<p>Save and close the file.<\/li>\n<li>Restart the OpenVPN service for the revocation directive to take efect:\n<p>At this point the client should no longer be able to access the OpenVPN server using the revoked certificate.<\/li>\n<\/ol>\n<p>If you need revoke additional client certificates just repeat the same steps.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this tutorial you learned how to install and configure an OpenVPN server on an Ubuntu 18.04 machine.<\/p>\n<p><a href=\"http:\/\/lxer.com\/module\/newswire\/ext_link.php?rid=261756\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Whether you want to access the Internet safely and securely while connected on an untrusty public Wi-Fi network, bypass Geo-restricted content or allow your coworkers to connect securely to your company network when working remotely, using a VPN is the best solution. VPN allows you to connect to remote VPN servers, making your connection encrypted &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/17\/how-to-set-up-an-openvpn-server-on-ubuntu-18-04\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How To Set Up an OpenVPN Server on Ubuntu 18.04&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-613","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/613","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=613"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/613\/revisions"}],"predecessor-version":[{"id":743,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/613\/revisions\/743"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}