{"id":30,"date":"2016-06-14T01:25:25","date_gmt":"2016-06-14T01:25:25","guid":{"rendered":"https:\/\/www.lucaswilliams.net\/?p=30"},"modified":"2016-06-14T01:25:25","modified_gmt":"2016-06-14T01:25:25","slug":"openvpn-server-on-ubuntu-16-04","status":"publish","type":"post","link":"https:\/\/www.lucaswilliams.net\/index.php\/2016\/06\/14\/openvpn-server-on-ubuntu-16-04\/","title":{"rendered":"OpenVPN Server on Ubuntu 16.04"},"content":{"rendered":"<p>Hello everyone! Hope everyone is having a good start to summer. I&#8217;ve been extremely busy as usual, but I had a moment of time to start this new HOWTO, How to Install OpenVPN in Ubuntu 16.04 so that you can connect to your home machines or browse the Internet safely from anywhere in the world. If you don&#8217;t know what a VPN, or Virtual Private Network is, this is a simple answer. Its a Network that allows encrypted information between the VPN server and your machine so that it appears like it is on the same network as the rest of your home equipment, but are over the internet. This is useful if you are working remote and need access to your servers at home, but don&#8217;t have them connected directly to the Internet with their own IP address.<\/p>\n<p>The main reason I am writing this, is because I had to setup a VPN connection to my home lab so that my co-workers could connect to the various network equipment I have in my lab and test on this equipment. So I setup a VPN so that they can connect into my lab, get on the switches, get on the console concentrator, and power up, power down, and work on the switches remotely. It&#8217;s extremely secure since I have to give the user a certificate to connect to my VPN server and I control them so that if they don&#8217;t need access anymore, I kill that certificate in my Certificate Authority and they can no longer login on my network.<\/p>\n<p>This HowTo is going to show how I setup OpenVPN on Ubuntu 16.04, and secured the system using UFW so that only 2 ports are exposed to the world to limit the attack surface of my VPN server.First thing I did was install Ubuntu Server 16.04. I used Virtual Machines quite extensively, so that is how this started. I created a VM, made sure to set it&#8217;s network interface to my external IP pool, gave it 1GB of RAM and 1 vCPU, 16GB of storage and installed Ubuntu on it. The only other software I installed was OpenSSH-Server and that was completed. I then modified the \/etc\/network\/interfaces file so that it had a static IP address, gateway and DNS server information, subnet range, and what the device was called. This is important since it will come into play when you are setting up the the VPN server so that it knows what to tunnel through for the firewall rules. In this example, the device is ens160, but it will be whatever your system calls it, typically this is eth0.<\/p>\n<p>After the server was installed, I ran the following to make sure it was all up to date and had the latest repositories:<\/p>\n<p><code>sudo apt update &amp;&amp; sudo apt upgrade -y<\/code><\/p>\n<p>I reboot the server after this so that it used the new IP address, and was running with the latest updates.<\/p>\n<p>I than ran <code>sudo apt install openvpn easy-rsa<\/code>\u00a0to install the required binaries I needed.<\/p>\n<p>I than ran <code>make-cadir ~\/openvpn-ca<\/code>. This command creates the minimum config files and sources so that you can build a Certificate Authority (CA) on the system. This is required to create the certificates that will be used by the server and the clients to connect and verify the systems so that they trust each other.<\/p>\n<p>Once that completes, change directory to the CA folder <code>cd ~\/openvpn-ca<\/code>, and modify the\u00a0<em>vars<\/em> file <code>vi vars<\/code>. Go to the section that looks like this:<\/p>\n<pre><code>export KEY_COUNTRY=\"US\"\r\nexport KEY_PROVINCE=\"CA\"\r\nexport KEY_CITY=\"SanFrancisco\"\r\nexport KEY_ORG=\"Fort-Funston\"\r\nexport KEY_EMAIL=\"me@myhost.mydomain\"\r\nexport KEY_OU=\"MyOrganizationalUnit\"<\/code><\/pre>\n<p>Modify these variables for your needs. Also, find the variable KEY_NAME and change it to the name of your server.<\/p>\n<p><code>export KEY_NAME=\"server\"<\/code><\/p>\n<p>Now, you are ready to build the CA. Run <code>source vars<\/code>\u00a0and you should get the following output:<\/p>\n<p><code>NOTE: If you run .\/clean-all, I will be doing a rm -rf on \/home\/wililupy\/openvpn-ca\/keys<\/code><\/p>\n<p>Go ahead and run <code>.\/clean-all<\/code> to make sure that the environment is good to go. Now we are ready to build the CA. Run the command <code>.\/build-ca<\/code>.<\/p>\n<p>You will be given a bunch of options, most of which you already set in the\u00a0<em>vars\u00a0<\/em>file, so just hit enter to accept them.<\/p>\n<p>We now are ready to create the server certificate, the key and encryption files. This is done with the command <code>.\/build-key-server server<\/code>\u00a0where\u00a0<em>server<\/em> is the name of your VPN server. Once again, it looks at the\u00a0<em>vars<\/em> file and uses those for the defaults, and then it will have two prompts you need to answer. The first one is:<\/p>\n<pre><code>Certificate is to be certified until June 13 15:26:11 2026 GMT (3650 days)\r\nSign the certificate? [y\/n]:y<\/code><\/pre>\n<p>The second one is:<\/p>\n<pre><code>1 out of 1 certificate requests certified, commit? [y\/n]y<\/code><\/pre>\n<p>It will update the database and now we are ready to generate the encryption key. Use the command <code>.\/build-dh<\/code>\u00a0to do this. It takes about 2 minutes for this command to complete. You will see &#8230;. and * while it randomizes. Lastly, we need to generate the HMAC signature. To do this use the following command:<\/p>\n<p><code>openvpn --genkey --secret keys\/ta.key<\/code><\/p>\n<p>Now we are ready to build the client certificate so that you can connect to your VPN server. While still in the ~\/openvpn-ca directory, and while you are still sourced to vars, run <code>.\/build-key client<\/code>\u00a0where\u00a0<em>client<\/em> is the hostname of the client machine\/username. Make sure you say Y at the prompts to sign the certificate and commit the certificate.<\/p>\n<p>You are now ready to copy the required files to the \/etc\/openvpn directory so that we can configure openvpn to run.<\/p>\n<p>Go into the keys directory:<\/p>\n<p><code>cd ~\/openvpn-ca\/keys<\/code>\u00a0and copy the certificates and keys to \/etc\/openvpn<\/p>\n<p><code>sudo cp ca.crt ca.key server.crt server.key ta.key dh2048.pem \/etc\/openvpn<\/code><\/p>\n<p>We are now ready to copy the example server.conf file to the \/etc\/openvpn directory so that we can configure the server. You have to uncompress it first:<\/p>\n<p><code>gunzip -c \/usr\/share\/doc\/openvpn\/examples\/sample-config-files\/server.conf.gz | sudo tee \/etc\/openvpn\/server.conf<\/code><\/p>\n<p>Now we have to modify the file so that it works with our environment.<\/p>\n<p><code>sudo vi \/etc\/openvpn\/server.conf<\/code><\/p>\n<p>Search for <code>redirect-gateway<\/code>\u00a0and remove the ; to uncomment the setting so that it looks like this:<\/p>\n<p><code>push \"redirect-gateway def1 bypass-dhcp\"<\/code><\/p>\n<p>Then below that is the &#8220;dhcp-option DNS&#8221; settings. Uncomment them and set them to your DNS servers or leave them as the defaults. I changed them to my internal DNS so that users can use my internal names of my systems and get to them easier than searching around for IP addresses. Next, uncomment the HMAC section by searching for <code>tls-auth<\/code>\u00a0and just under that variable, add <code>key-direction 0<\/code>. Last, search for <code>user<\/code>\u00a0and uncomment <code>user nobody<\/code>\u00a0and <code>group nogroup<\/code>\u00a0so that the service knows who to run as.<\/p>\n<p>Now we have to allow the system to do IP Forwarding and modify the Firewall to secure the system. First, modify \/etc\/sysctl.conf and uncomment <code>net.ipv4.ip_forward=1<\/code>\u00a0and then save the file and run <code>sudo sysctl -p<\/code>\u00a0to make the changes take effect.<\/p>\n<p>Next, modify the \/etc\/ufw\/before.rules so we can setup Masquerading for the VPN server. Right after the <code># \u00a0ufw-before-forward<\/code>\u00a0option, enter the following:<\/p>\n<pre><code>*nat\r\n:POSTROUTING ACCEPT [0:0]\r\n-A POSTROUTING -s 10.8.0.0\/8 -o ens160 -j MASQUERADE\r\nCOMMIT<\/code><\/pre>\n<p>Remember when I said to remember your network device from when we were setting up the static IP of the server? After the <code>-o<\/code>\u00a0option in the before-rules file, that is where the name of your device goes. Save the file. Now we have to set UFW to forward by default. Modify the <code>\/etc\/default\/ufw<\/code>\u00a0file and find the <code>DEFAULT_FORWARD_POLICY<\/code>\u00a0and set it to <code>\"ACCEPT\"<\/code>. Save this file and now all we have to do is allow ufw the openvpn port and protocol and enable the ssh variable:<\/p>\n<pre><code>sudo ufw allow 1194\/udp\r\nsudo ufw allow 22\/tcp<\/code><\/pre>\n<p>Now we need to disable and re-enable ufw so that it will read the changes in the files we modified:<\/p>\n<pre><code>sudo ufw disable\r\nsudo ufw enable<\/code><\/pre>\n<p>Now we are ready to start OpenVPN. Since our configuration is called\u00a0<em>server.conf<\/em>, when we start openvpn, we will tell it <code>@server<\/code>\u00a0so that it will use that configuration. Nice this about openvpn, is that we can have multiple configuration, and multiple instances of the VPN server running, all we have to do is trail <code>@configname<\/code>\u00a0after it and it will run that config. To start openvpn, run the following command:<\/p>\n<p><code>sudo systemctl start openvpn@server<\/code><\/p>\n<p>Check that it is running by running <code>sudo systemctl status openvpn@server<\/code>\u00a0and look for the <code>Active: active (running)<\/code>. If everything looks good, set it to run at startup by running <code>sudo systemctl enable openvpn@server<\/code>.<\/p>\n<p>Now we are ready to setup the clients. First thing I did was create a new directory for the client files so that I could scp them to my colleagues and my different machines and devices (OpenVPN works on Windows, MacOSX, Linux, iPhone, and Android)<\/p>\n<p><code>mkdir -p ~\/client-configs\/files<\/code><\/p>\n<p>Also, because there will be multiple keys in this folder for different machines, I locked it down so that only I had access to that folder: <code>chmod 700 ~\/client-configs\/files<\/code>.<\/p>\n<p>Next, I copied the example configuration for clients to this location:<\/p>\n<p><code>cp \/usr\/share\/doc\/openvpn\/examples\/sample-config-files\/client.conf ~\/client-configs\/base.conf<\/code>\u00a0and then edited the file to meet my client needs.<\/p>\n<p>First thing is to search for <code>remote<\/code>\u00a0in the file and change the <code>server_IP_address<\/code>\u00a0to the public IP address of your VPN server. Next uncomment the user and group variables by deleting the leading &#8216;;&#8217;.<\/p>\n<p>Next, search for the ca.crt and client.crt sections and comment them out with the &#8216;#&#8217;, and finally, add the <code>key-direction 1<\/code>\u00a0in the file somewhere so that it knows how to use the keys. Save the file and you&#8217;re done.<\/p>\n<p>Now, I found this really cool script at\u00a0<a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-openvpn-server-on-ubuntu-16-04\" target=\"_blank\">https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-an-openvpn-server-on-ubuntu-16-04<\/a>.<\/p>\n<pre><code>#!\/bin\/bash\r\n\r\n# First argument: Client identifier\r\n\r\nKEY_DIR=~\/openvpn-ca\/keys\r\nOUTPUT_DIR=~\/client-configs\/files\r\nBASE_CONFIG=~\/client-configs\/base.conf\r\n\r\ncat ${BASE_CONFIG} \\\r\n    &lt;(echo -e '') \\\r\n    ${KEY_DIR}\/ca.crt \\\r\n    &lt;(echo -e '\\n') \\\r\n    ${KEY_DIR}\/${1}.crt \\\r\n    &lt;(echo -e '\\n') \\\r\n    ${KEY_DIR}\/${1}.key \\\r\n    &lt;(echo -e '\\n') \\\r\n    ${KEY_DIR}\/ta.key \\\r\n    &lt;(echo -e '') \\\r\n    &gt; ${OUTPUT_DIR}\/${1}.ovpn<\/code><\/pre>\n<p>Create a file called <code>make_config.sh<\/code>\u00a0and paste the script into that file. Save the file, then make it executable by running <code>chmod 700 ~\/client-configs\/make_config.sh<\/code>.<\/p>\n<p>If you remember, we created a client certificate and key previously, using the <code>build-key client<\/code>\u00a0command. This created a\u00a0<em>client.key\u00a0<\/em>file in the ~\/openvpn-ca\/keys directory. We are now going to build a configuration for the VPN that uses these keys. Make sure you are in the ~\/client-configs directory and run <code>.\/make_config.sh client<\/code>\u00a0where\u00a0<em>client<\/em> is the name of the client configuration you are creating. The name should match what you entered in the <code>build-key<\/code> command previously. This will generate a file called\u00a0<em>client.ovpn<\/em> which needs to be copied to the client. I use SCP or SFTP to transfer the files between Linux and MacOSX, but for Windows or IOS or Android, getting the certificate file on the system may be a little trickier. For Windows, I use FileZilla or WinSCP. Just login to the VPN server and copy the ovpn file to your home directory on the system.<\/p>\n<p>In Ubuntu Desktop 16.04, make sure you have OpenVPN installed, (<code>sudo apt install network-manager-openvpn-gnome<\/code>) open up Network Manager, go to VPN Connections, Configure VPN, and click Add. From the drop down, select Import a saved VPN configuration&#8230; and browse to your .ovpn file. Select Open and verify that everything looks right, the vpn server&#8217;s IP address, the name of the certificates, and click Save. Now you are ready to test. Connect your new VPN and verify that you connect successfully. Check your network devices for the new <code>tun0<\/code> device and IP address of 10.8.0.x (<code>ifconfig tun0<\/code>). Try to connect to a server in your internal network and verify that everything is working as normal.<\/p>\n<p>And thats it. It really isn&#8217;t that difficult to setup. If you have any questions, or if this blog helped you in anyway, let me know. I like to think that I&#8217;m helping someone out there.<\/p>\n<p>Thanks!<\/p>\n<p>[ayssocial_buttons id=&#8221;2&#8243;]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone! Hope everyone is having a good start to summer. I&rsquo;ve been extremely busy as usual, but I had a moment of time to start this new HOWTO, How to Install OpenVPN in Ubuntu 16.04 so that you can connect to your home machines or browse the Internet safely from anywhere in the world. [&hellip;]<\/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-30","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/posts\/30","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/comments?post=30"}],"version-history":[{"count":2,"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/posts\/30\/revisions"}],"predecessor-version":[{"id":32,"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/posts\/30\/revisions\/32"}],"wp:attachment":[{"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/media?parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/categories?post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lucaswilliams.net\/index.php\/wp-json\/wp\/v2\/tags?post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}