- Protect your browsing data: Install OpenVPN on CentOS 7
- Prerequisites to install OpenVPN on Centos 7
- Step 1: Install OpenVPN and EasyRSA
- Step 2: Set up the Certificate Authority
- Step 3: Create the server certificate and public/private keys
- Step 4: Generating a Client Certificate and Key Pair
- Step 5: Configure the OpenVPN Service
- Step 6: Start and Enabling the OpenVPN Service
- Step 7: Configure clients
- You’re now using OpenVPN on CentOS 7 to keep your browsing data private
Written by Promise Akpan
Protect your browsing data: Install OpenVPN on CentOS 7
There are literally lists of reasons why you might want to use a VPN, but keeping your data safe is #1 on our list.
And while there are some paid VPN services out there that are pretty easy on your wallet.
If you’re trying to keep your data safe then why would you trust it to a uber-secretive company that may-or-may-not have connections to data mining operations?
Get 480GB NVMe extra storage for FREE
Celebrating our 9 year anniversary! Capacity is limited and some deals will sell out. Get yours before they are gone!
Answer: you shouldn’t.
Especially when it’s so easy and affordable to set up your own fast VPN on your SSD Nodes VPS.
So today, we’re going to show you how to install OpenVPN on CentOS 7 to keep your data truly safe.
Prerequisites to install OpenVPN on Centos 7
- Two VPS running CentOS 7, one to host the OpenVPN service and another to serve as your Certificate Authority (CA). It is not recommended to use your OpenVPN Server as your CA, this opens up your VPN to security vulnerabilities.
- A regular (non-root) account with sudo privileges. See our SSH keys tutorial for more information.
NOTE: If you disable password authentication while configuring these servers, you may run into difficulties when transferring files between them later on in this guide. To resolve this issue, you can re-enable password authentication on each server. Or, you can generate an SSH keypair for each server, then add the OpenVPN server’s public SSH key to the CA machine’s authorized_keys file and vice versa.
What’s the BEST DEAL in cloud hosting?
Develop at hyperspeed with a Performance VPS from SSD Nodes. We DOUBLED the amount of blazing-fast NVMe storage on our most popular plan and beefed up the CPU offering on these plans. There’s nothing else like it on the market, at least not at these prices.
Score a 16GB Performance VPS with 160GB of NVMe storage for just $99/year for a limited time!
Step 1: Install OpenVPN and EasyRSA
Let’s start by updating our apt cache and installing EPEL repository.
Update your package list again.
Next, install OpenVPN, wget and nano (or your favorite text editor).
OpenVPN uses SSL/TLS for authentication and key exchange to encrypt traffic between the server and clients.
To issue trusted certificates, you will set up your simple certificate authority (CA). To do this, we will download the latest version of EasyRSA, which we will use to build our CA public key infrastructure (PKI), from the project’s official GitHub repository.
NOTE: It is recommended that you keep the CA server turned off when not being used to sign keys as a further precautionary measure.
To begin building the CA and PKI infrastructure, use wget to download the latest version of EasyRSA on both your CA machine and your OpenVPN server.
Then extract the tarball:
You have successfully installed all the required software on your server and CA machine.
Continue to configure the variables used by EasyRSA and to set up a CA directory, from which you’ll us to generate the keys and certificates needed for your server and clients to access the VPN.
Step 2: Set up the Certificate Authority
EasyRSA comes packaged with a configuration file that can be edited to define several variables for your CA.
On your CA machine, navigate to the EasyRSA directory:
We can utilize the easy — rsa template by making a copy of an existing vars.example file in this directory and renaming it vars :
We need to edit some of the variables that help decide how to create the certificates. Use nano — or another favorite editor—to open the file. We’ll be editing some variables toward the end of the file.
Find the settings that set field defaults for new certificates. It will look something like this:
Uncomment these lines and update the highlighted values to whatever you’d prefer, but do not leave them blank:
Save and close the file after editing.
Inside the EasyRSA directory is a script called easyrsa which is used to perform a variety of tasks involved with building and managing the CA. Run this script with the init — pki option to initiate the public key infrastructure on the CA server:
After this, call the easyrsa script again, following it with the build — ca option. This builds the CA and creates two important files — ca.crt and ca.key — which make up the public and private sides of an SSL certificate.
If you don’t want to be prompted for a password every time you interact with your CA, you can run the build — ca command with the nopass option:
In the output, you’ll be asked to confirm the common name for your CA:
The common name is the name used to refer to this machine in the context of the certificate authority. You can enter any string of characters for the CA’s common name but, for simplicity’s sake, press ENTER to accept the default name.
With that, your CA is in place and it’s ready to start signing certificate requests.
Step 3: Create the server certificate and public/private keys
With the CA set up correctly, you can generate a private key and certificate request from your server and then transfer the request over to your CA to be signed, creating the required certificate.
Navigate to the EasyRSA directory on your OpenVPN server:
From here, run the easyrsa script with the init — pki option. Although you already ran this command on the CA machine, it’s necessary to run it here because your server and CA will have separate PKI directories:
Then call the easyrsa script again, this time with the gen — req option followed by a common name for the machine.
This can be anything you like but for the sake of this tutorial, we’re choosing vpnserver . Include the nopass option, failing to do so will password-protect the request file which could lead to permissions issues later on:
Note: If you choose a name other than “server” here, you will have to adjust some of the instructions below. For instance, when copying the generated files to the /etc/openvpn directory, you will have to substitute the correct names. You will also have to modify the /etc/openvpn/server.conf file later to point to the correct .crt and .key files.
This will create a private key for the server and a certificate request file called vpnserver.req. Copy the server key to the /etc/openvpn/ directory:
Using a secure method (like SCP, in our example below), transfer the vpnserver.req file to your CA machine:
Next, on your CA machine, navigate to the EasyRSA directory:
Using the easyrsa script again, import the vpnserver.req file, following the file path with its common name:
Then sign the request by running the easyrsa script with the sign — req option, followed by the request type and the common name. The request type can either be client or server , so for the OpenVPN server’s certificate request, be sure to use the server request type:
If you encrypted your CA key, you’ll be prompted for your password at this point.
Next, transfer the signed certificate back to your VPN server using a secure method:
Before logging out of your CA machine, transfer the ca.crt file to your server as well:
Next, log back into your OpenVPN server and copy the server.crt and ca.crt files into your /etc/openvpn/ directory:
Then navigate to your EasyRSA directory:
From there, create a strong Diffie-Hellman key to use during the key exchange by typing:
This may take a few minutes to complete. Once it does, generate an HMAC signature to strengthen the server’s TLS integrity verification capabilities:
When the command finishes, copy the two new files to your /etc/openvpn/ directory:
With all the needed certificate and key files generated, you are set to create the corresponding certificates and keys which will be used by your client machine to access your OpenVPN server.
Step 4: Generating a Client Certificate and Key Pair
Create a directory structure within your home directory to store the client certificate and key files:
Since your clients’ certificate/key pairs and configuration files will be stored in this directory, lock down its permissions as a security measure:
Next, navigate back to the EasyRSA directory and run the easyrsa script with the gen — req and nopass options, along with the common name for the client:
NOTE: You will need to pass a unique name value to the script for every client. Throughout this tutorial, the first certificate/key pair is referred to as clienta
Press ENTER to confirm the common name. Then, copy the clienta.key file to the /client — configs/keys/ directory you created earlier:
Next, securely transfer the clienta.req file to your CA machine:
Log in to your CA machine, navigate to the EasyRSA directory, and import the certificate request:
Then sign the request as you did for the server in the previous step. This time, though, be sure to specify the client request type:
At the prompt, enter yes to confirm that you intend to sign the certificate request and that it came from a trusted source. you’d get the following output
Again, if you encrypted your CA key, you’ll be prompted for your password here.
This will create a client certificate file named clienta.crt . Transfer this file back to the server:
SSH back into your OpenVPN server and copy the client certificate to the /client — configs/keys/ directory:
Next, copy the ca.crt and ta.key files to the /client — configs/keys/ directory as well:
Your server and client’s certificates and keys have all been generated and are stored in the appropriate directories on your server.
Step 5: Configure the OpenVPN Service
Now that both your client and server’s certificates and keys have been generated, you can start configuring the OpenVPN service to run on CentOS 7 using these credentials.
Begin by copying a sample OpenVPN configuration file into the configuration directory and then extract it to use it as a basis for your setup:
Open the server configuration file in your preferred text editor:
Find the HMAC section by looking for the tls — auth directive. This line should already be uncommented, but if isn’t then remove the ";" to uncomment it. Below this line, add the key — direction parameter, set to "0":
Next, find the section on cryptographic ciphers by looking for the commented out cipher lines. The AES-256-CBC cipher offers a good level of encryption and is well supported. Again, this line should already be uncommented, but if it isn’t then just remove the ";" preceding it:
Below this, add an auth directive to select the HMAC message digest algorithm. For this, SHA256 is a good choice:
If like in this tutorial you selected a different name during the ./build-key-server command earlier, modify the cert and key lines that you see to point to the appropriate .crt and .key files. The default is server , while vpnserver is used in this guide.
Next, find the line containing a dh directive which defines the Diffie-Hellman parameters. Because of some recent changes made to EasyRSA, the filename for the Diffie-Hellman key may be different than what is listed in the example server configuration file. If necessary, change the file name listed here by removing the 2048 so it aligns with the key you generated in the previous step:
Finally, find the user and group settings and remove the ";" at the beginning of each to uncomment these lines:
The changes you’ve made to the sample server.conf file up to this point are necessary for OpenVPN to function.
When you are finished, save and close the file.
After going through and making whatever changes to your server’s OpenVPN configuration are required for your specific use case, you can begin making some changes to your server’s networking.
Step 6: Start and Enabling the OpenVPN Service
Before we configure our clients, let’s make sure the OpenVPN server is running as we hope it will.
Make sure to turn on TUN/TAP in the SSD Nodes dashboard.
You can double-check that OpenVPN is running with the systemctl status command:
You will also need to set up iptables to properly direct traffic. First, look for the default interface.
Your output will look like this:
The eth0 field is what we’re looking for. And then we set up iptables . To ensure this rule is persistent between reboots, install the iptables — persistent package, which will prompt you to save existing rules. Choose Yes and your rules will be persisted moving forward.
Step 7: Configure clients
Lastly, you need to create client configurations. You can store these in any folder you’d like—they don’t need to be kept secret—as long as it isn’t the /etc/openvpn folder. We’ll create a directory in home for this purpose.
Now, copy the sample client configuration into this new directory, and then open it in nano for editing.
Look for the following block of lines. You’ll need to change the my — server — 1 to the public IP address of this VPS. You can find this information in the SSD Nodes dashboard, or by typing in the ifconfig command and looking for the inet field that does not look like 127.0.0.x.
Next, uncomment the following two lines by removing the semicolon.
Because we’ll be adding keys and certificates directly into the .ovpn file, let’s comment out the following lines by adding semicolons to the beginning.
Finally, jump to the bottom of the file and add the following lines. The first two mirror the cipher/auth options we added to the server.conf file earlier, and the third establishes that this files will be used to connect to the server, not the other way around.
We’re also adding three commented-out files that should be uncommented for Linux-based systems that use update — resolv — conf .
Finally, you need to embed the keys and certificates into an .ovpn file using base.conf as a framework. Copy this entire command and execute it to embed the keys and create a final client1.ovpn file.
This tutorial won’t cover client configurations in detail, but we’ll share one easy way to transfer the .ovpn file to your Linux or OS X client. This command will ssh into your VPS, and then use cat to write a new client1.ovpn file on your local machine.
Once you configure your client, you should be able to connect to the VPN and access the wider internet through it.
You’re now using OpenVPN on CentOS 7 to keep your browsing data private
Congrats! You’re can now browse the internet mostly anonymously. Your VPN can keep your ISP from seeing your browsing data and add an extra level of of encryption for critical information.
A VPN isn’t a complete invisibility cloak, however.
It doesn’t cause you to cease to exist. Some activity may still be traceable, especially by law enforcement entities. So study up on what it can and can’t do for you to avoid getting yourself into trouble.
If you’d like a more automated method of installing OpenVPN, plus a few other VPN options, consider trying out our Streisand tutorial to learn about another popular VPN you can use on your VPS.