Hostwinds Tutorials

Search results for:

Table of Contents

OpenVPN Preparation
Installing OpenVPN
Setting up the Certificate Authority
Configuring the OpenVPN Server
Configure OpenVPN routing using Firewall
Downloading the OVPN File
Setting Up Your Local OpenVPN Client

How to Install OpenVPN on a Cloud VPS

Tags: Cloud Servers,  VPS 

OpenVPN Preparation
Installing OpenVPN
Setting up the Certificate Authority
Configuring the OpenVPN Server
Configure OpenVPN routing using Firewall
Downloading the OVPN File
Setting Up Your Local OpenVPN Client

OpenVPN is a Virtual Private Network (VPN) software installed on a Virtual Private Server (VPS). VPNs take advantage of the network provided by the VPS without needing to connect through an SSH or RDP session. You can connect devices to the VPN server and use that network to mask your local area network.

OpenVPN Requirements

You will need root or administrator access to a server. You can install OpenVPN on either a Linux, Windows VPS, or Dedicated Server. Below are the steps to install OpenVPN. Note that you will need to use a command-line interface to do so.

OpenVPN Preparation

Step 1: Update your system.

Sudo yum update

Step 2: Edit the SELinux config file

nano /etc/selinux/config

Step 3: Set SELinux to Disabled

  • CTRL + X
  • Press the Y key
  • Press the Enter key

Step 4: Edit the sysctl.conf file

nano /etc/sysctl.conf

Step 5: Add the following line to the sysctl.conf file to enable IP Forwarding

net.ipv4.ip\_forward = 1


Press the Y key

Press the Enter key

Step 6: Apply the changes

sysctl -p

Installing OpenVPN

**Step 1: ** Install the OpenVPN Server

dnf install epel-release -y
dnf install openvpn -y

Setting up the Certificate Authority

Step 1: Download easy-rsa for managing SSL Certificates

cd /etc/openvpn
tar -xvzf EasyRSA-unix-v3.0.6.tgz
mv EasyRSA-v3.0.6 easy-rsa

Step 2: Setup Certificate Authority

cd /etc/openvpn/easy-rsa
nano vars

Step 3: Add the following and replace anything with \ with your VPS hostname (it looks something like hwsrv-\. Additionally, replace the "set_var EASYRSA_REQ_EMAIL" line with your email:

set_var EASYRSA                 "$PWD"
set_var EASYRSA_PKI             "$EASYRSA/pki"
set_var EASYRSA_DN              "cn_only"
set_var EASYRSA_REQ_PROVINCE    "Seatle"
set_var EASYRSA_REQ_CITY        "Seatle"
set_var EASYRSA_REQ_EMAIL       "<>"
set_var EASYRSA_REQ_OU          "<HOSTNAME> EASY CA"
set_var EASYRSA_KEY_SIZE        2048
set_var EASYRSA_ALGO            rsa
set_var EASYRSA_CA_EXPIRE       7500
set_var EASYRSA_CERT_EXPIRE     365
set_var EASYRSA_NS_SUPPORT      "no"
set_var EASYRSA_EXT_DIR         "$EASYRSA/x509-types"
set_var EASYRSA_SSL_CONF        "$EASYRSA/openssl-easyrsa.cnf"
set_var EASYRSA_DIGEST          "sha256"

Step 4: Initiate the PKI Directory

./easyrsa init-pki

Step 5: Build the CA Certificate

./easyrsa build-ca

It will ask you to provide a password. Note the password externally and input it into the prompt. This passphrase will be required in future steps.

You will also be asked to input a Common Name. You can press enter to use the default or input a custom common name of your choice.

The command will then generate two files named ca.key and ca.crt. These certificates will be used to sign the server and the clients' certificates.

Step 6: Generate the Server Certificate Files. Replace the \ with the server hostname. Looks like "hwsrv-\"

./easyrsa gen-req <HOSTNAME> nopass

This will also ask for a common name. You can input whatever you wish. Some names you could use would be your user, host, or server name. Alternatively, you can just press "Enter" to use the default.

Step 7: Sign the server key using the CA. Replace the \ with the server hostname. It looks like "hwsrv-\"

./easyrsa sign-req server <HOSTNAME>

Type "yes" then press Enter

Additionally, you'll need to provide a passphrase. Use the passphrase you created in Step 5

Step 8: Verify the generated certificate file with the following command. Replace the \ with the server hostname. It looks like "hwsrv-\":

openssl verify -CAfile pki/ca.crt pki/issued/<HOSTNAME>.crt

It should look something like this:

pki/issued/<HOSTNAME>.crt: OK

Step 9: Generate a strong Diffie-Hellman key to use for the key exchange:

./easyrsa gen-dh

Step 10: ** Copy the certificate files to /etc/openvpn/server/ and **replace the \ with the server hostname. It looks like "hwsrv-\"

cp pki/ca.crt /etc/openvpn/server/
cp pki/dh.pem /etc/openvpn/server/
cp pki/private/<HOSTNAME>.key /etc/openvpn/server/
cp pki/issued/<HOSTNAME>.crt /etc/openvpn/server/

Step 11: Build the client key file:

./easyrsa gen-req client nopass

Press enter or input a common name of your choice.

Step 12: Sign the Client Key using the CA Certificate:

./easyrsa sign-req client client

Type the word, 'yes' then press enter to confirm

Enter the passphrase you noted down in Step 5

Step 13: Copy all of the Client Certificate and Key Files to the /etc/openvpn/client/ directory

cp pki/ca.crt /etc/openvpn/client/
cp pki/issued/client.crt /etc/openvpn/client/
cp pki/private/client.key /etc/openvpn/client/

Configuring the OpenVPN Server

Step 1: Create a new OpenVPN Configuration file inside the /etc/openvpn/client/directory/ file path

nano /etc/openvpn/server/server.conf

Add the following in the text editor and replace anything labeled \ with your server hostname. It looks like "hwsrv-\

port 1194
proto udp
dev tun
ca /etc/openvpn/server/ca.crt
cert /etc/openvpn/server/<HOSTNAME>.crt
key /etc/openvpn/server/<HOSTNAME>.key
dh /etc/openvpn/server/dh.pem
push "redirect-gateway def1"

push "dhcp-option DNS"
push "dhcp-option DNS"
cipher AES-256-CBC
tls-version-min 1.2
auth SHA512
keepalive 20 60
compress lz4
user nobody
group nobody
log-append /var/log/openvpn.log
verb 3

Now do the following to save your file:

  • CTRL + X
  • Press the Y key
  • Press the Enter key

Step 2: Start the OpenVPN Server

systemctl start openvpn-server@server
systemctl enable openvpn-server@server
systemctl status openvpn-server@server

Step 3: Generate the Client Configuration File. This is needed to connect to the OpenVPN server from the client system.

nano /etc/openvpn/client/client.ovpn

Insert the following and replace \ with your servers Dedicated Main IPv4 Address

dev tun
proto udp
remote <Server IP> 1194
ca ca.crt
cert client.crt
key client.key
cipher AES-256-CBC
auth SHA512
tls-version-min 1.2
resolv-retry infinite
compress lz4
verb 3

Now do the following to save your file:

  • CTRL + X
  • Press the Y key
  • Press the Enter key

Configure OpenVPN routing using Firewall

Step 1: Add the OpenVPN service and tun0 interface to the trusted firewall zone:

firewall-cmd --permanent --add-service=openvpn
firewall-cmd --permanent --zone=trusted --add-service=openvpn
firewall-cmd --permanent --zone=trusted --add-interface=tun0

Step 2: Add the MASQUERADE on the default zone:

firewall-cmd --add-masquerade
firewall-cmd --permanent --add-masquerade

Step 3: Run the following command to masquerade the internet traffic coming from the VPN. Replace \ with your server hostname. It will looks like, "hwsrv\. In this case, you would not need the dash in between the "hwsrv" and the "\"

<HOSTNAME>ovpn=$(ip route get | awk 'NR==1 {print $(NF-2)}')
firewall-cmd --permanent --direct --passthrough ipv4 -t nat -A POSTROUTING -s -o $hwsrv702659ovpn -j MASQUERADE

Step 4: Implement the changes:

firewall-cmd --reload

Downloading the OVPN File

Step 1: You will need to download the OVPN directory to your local machine. You can do this in several different ways. The file path you need to download is the /etc/openvpn/client directory.

Open your Local Computer's terminal and type the following. Ensure you replace \ with your server's dedicated IPv4:

scp -r root@**\<SERVER IP>**:/etc/openvpn/client .

Alternatively, you can follow this guide on how to connect to your server through SFTP using FileZilla:

Setting Up Your Local OpenVPN Client

Step 1: Install the OpenVPN Client. If you have a Windows Machine, you can install it here:

Step 2: Import the OpenVPN profile using the downloaded file, "client.ovpn"

Step 3: Give your profile a name or leave it as the default. Click "Add" after importing the OVPN file.

Step 4: Connect to your VPN to start using the network!

Step 5: You should be connected to your VPN. Your interface should look like this:

Step 6: Congratulations! You're all done! You can verify your Public IP address from to make sure you are using your VPS network.

Written by Hostwinds Team  /  July 31, 2021