Install OpenVPN On CentOS / RHEL 7

Security is most important aspect in internet. Outsiders can monitor internet traffic between your computer and the web. Here the importance of VPN comes. VPN, or virtual private network, is a secure method of connecting remote internet resources together as if they were under the same LAN. OpenVPN is a popular open source application that implements a virtual private network. works on Linux, Windows, and Mac operating systems. It can be utilized to create a secure connection between physically distributed servers.

This Article explains How to install and configure OpenVPN in centOS 7 server.


  • CentOS / RHEL 7 server.
  • root access to the server.
  • Domain or sub-domain that resolves to your server that you can use for the certificates

OpenVpn isn’t available in the default CentOS repositories. So we need to install Enterprise Linux (EPEL) repository. Use the following command to install EPEL repository.

Step 1 — Installing OpenVPN

First, We are going to install in the server by issuing the following command.

Step 2 —  Install Easy RSA

For generating our SSL key pairs, which will secure our VPN connections. Execute the following command:

Step 3 — Configuring OpenVPN

We can find an example configuration file in its documentation directory. We need to copy the sampleserver.confby the following command.

Open the file in your favorite editor, I’m using editor,

Most of the lines just need to be uncommented (remove the 😉 and some of there are to be changed.

Do the following changes.

We need to change the dh file name to dh2048.pem. Because the default Diffie-Hellman encryption length for Easy RSA will be 2048 bytes. We will do the key generation in next step.

Next, uncomment the push “redirect-gateway def1 bypass-dhcp” line, which tells the client to redirect all traffic through our OpenVPN.

Next we need to provide DNS servers to the client, as it will not be able to use the default DNS servers provided by your Internet service provider. We’re going to use Google’s public DNS servers, and8.8.4.4.

For this, uncomment the push “dhcp-option DNS lines and updating the IP addresses.

Change user and group to nobody

Save and exit the OpenVPN server configuration file.

Step 4 — Generating Keys and Certificates

Now, we’ll need to generate our keys and certificates. Easy RSA installs some scripts to generate these keys and certificates.

Create a directory for the keys by the following command

We also need to copy the key and certificate generation scripts into the directory.

Now, we’re going to edit the default values in the script. So we don’t have to type our information in each time. Open the file in vi editor.

Change values that start with KEY_. Update the following values to be accurate for your organization.

Some of the important value that should be change carefully are,

  • KEY_NAME: You should enter server here; you could enter something else, but then you would also have to update the configuration files that reference  and
  • KEY_CN: Enter the domain or subdomain that resolves to your server

Refer the sample file below,

OpenSSL configuration may not load due to the version being undetectable. To avoid this remove the version number from the openSSl file name.

Next, We are going to generate the keys and certificates. Move to easy-rsa directory and source in our new variables.

Then, we will clean up any keys and certificates which may already be in this folder and generate our certificate authority.

When you build the certificate authority, you will be asked to enter all the information we put into the vars file, but you will see that your options are already set as the defaults. So, you can just press ENTER for each one.

Next, We will generate the key and certificate for the server. Please press ENTER for each question as for the above step

Now we will generate Diffie-Hellman key exchange file. This command will take few to complete:

So, we completed the server keys and certificates generation process. Copy them all into our OpenVPN directory.

For authenticate our clients will also need certificates. These keys and certificates will be shared with your clients, and it’s best to generate separate keys and certificates for each client you intend on connecting.

Make sure that if you do this you give them descriptive names, but for now we’re going to have one client so we’ll just call it client.

Step 5 — Routing

Install the iptables and disablefirewalld by execute the following commands

Next, We need to add a rule to iptables to forward our routing to our OpenVPN subnet, and save this rule.

Next, enable  IP forwarding in sysctl. Open sysctl.conf in vi editor.

Add the following line at the top of the file:

For the IP forwarding will take effect. We need to restart the network service. Issue the following command

Step 6 — Starting OpenVPN

Now, we completed the installation and ready start the openVPN service. add it to systemctl using the command

Start OpenVPN:

So we have successfully completed all the server-side configuration done for OpenVPN.

Next Let’s see how to connect a client to the server.

Step 6 — Configuring a Client

To connect you will definitely need a copy of the ca certificate from the server, along with the client key and certificate.

Locate the following files on the server. In this article we used ‘client’ as the descriptive name for the client keys.

Copy these three files to your client machine. For this, Open the file in the server and copy the content of the file into a new file in the client system an save, or use SFTP.

We’re going to create a file called client.ovpn. This is a configuration file for an OpenVPN client, telling it how to connect to the server.

  • You’ll need to change the first line to reflect the name you gave the client in your key and certificate; in our case, this is just client
  • You also need to update the IP address from your_server_ip to the IP address of your server; port1194 can stay the same
  • Make sure the paths to your key and certificate files are correct

This file can now be used by any OpenVPN client to connect to your server.

Assume the client machine has Windows OS.

You will need the official OpenVPN Community Edition binaries which come with a GUI. Then, place your .ovpn configuration file into the proper directory, ex. C:\Program Files\OpenVPN\config, and click Connect in the GUI. OpenVPN GUI on Windows must be executed with administrative privileges.

That’s it!