HugeServer Knowledgebase

How to setup OpenVPN on Ubuntu and Debian (Server side and Client side)

If you want to access the internet safely and securely but you are connected to an untrusted network such as a hotel WiFi, a Virtual Private Network (VPN) allows you to use untrusted networks privately.
In this tutorial, we are going to show you how to make your own Linux OpenVPN server by installing the latest version:”openvpn-2.3″

We are assuming that you have root permission, otherwise, you may start commands with “sudo”.

Installing OpenVPN 2.3

First, you have to install the OpenVPN app:

apt-get install openvpn easy-rsa

Configuring easy-rsa

Now that you have installed OpenVPN successfully, you have to create keys and certificates, follow this section step by step:

Step 1- Copy easy-rsa script generation to OpenVPN folder:

cp -R /usr/share/easy-rsa/ /etc/openvpn/

Then go to the easy-rsa directory and edit the “vars” file:

cd /etc/openvpn/easy-rsa/
nano vars

you may edit the values to your information here is the example:

openvpn vars configurations linux

Now it’s time to generate the new keys and certificates:

source ./vars

Run the above command and you will get the following message:

NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/keys

It’s recommended to run “./clean-all” right away to ensure that we have a clean certificate setup:


Now you have to generate a “Certificate Authority (ca)” file. you will be asked for country name etc. that you edited in the “vars” file. you can hit “Enter” to accept your default values.

Now move to the following directory:

cd /etc/openvpn/easy-rsa/

And run the command below to create the “ca” files:


Step 2- Generating a server key and certificate.

Run the command below in the current directory:

./build-key-server server

You will be asked for information again you can accept your default values again.

you may leave the two following questions blank:

A challenge password []:
An optional company name []:

Answer the two last questions with “Y”.

Sign the certificate? [y/n]:
1 out of 1 certificate requests certified, commit? [y/n]

Step 3- Build a Diffie-Hellman key exchange.

Execute the following command in the current directory:


Please wait, it will take some time to generate the files.

build-dh openvpn keys certificates

Step 4- Generate client key and certificate.

Run the following command in the current directory to generate client key and certificate:

./build-key client

And like step 2 you have to leave two lines blank and answer two questions with “Y”:

Step 5- Move or copy the directory “/keys/” to “/etc/openvpn/”.

cd /etc/openvpn/easy-rsa/
cp -R keys/ /etc/openvpn/

Configure OpenVPN

Now you have to create an OpenVPN configuration file to make some changes:

cd /etc/openvpn/
nano server.conf

Paste the configurations below (you may change the values of port etc.):

#change with your port
port 1194

#You can use udp or tcp
proto udp

# "dev tun" will create a routed IP tunnel.
dev tun

#Certificate Configuration

#ca certificate
ca /etc/openvpn/keys/ca.crt

#Server Certificate
cert /etc/openvpn/keys/server.crt

#Server Key and keep this is secret
key /etc/openvpn/keys/server.key

#See the size a dh key in /etc/openvpn/keys/
dh /etc/openvpn/keys/dh2048.pem

#Internal IP will get when already connect

#this line will redirect all traffic through our OpenVPN
push "redirect-gateway def1"

#Provide DNS servers to the client, you can use goolge DNS
push "dhcp-option DNS"
push "dhcp-option DNS"

#Enable multiple client to connect with same key

keepalive 20 60

log-append /var/log/myvpn/openvpn.log

#Log Level
verb 3

Save and exit (Ctrl+O and Ctrl+X)

Create a folder for the log file and active it:

mkdir -p /var/log/myvpn/
touch /var/log/myvpn/openvpn.log

Disable firewalld

For configuring an OpenVPN server you need “iptables” firewall to trigger your preferred Port and give your client(s) access to the internet. you have to disable any other firewall that is already in use.

For disabling firewalld:

systemctl mask firewalld

systemctl stop firewalld

Finding the ethernet interface name

before any firewall configuration you have to find out your ethernet interface name with the command below:


The output should be something like the picture below:

As you see you can find your Ethernet interface name where you see your public IP address. (e.g. “ens3”)

Install and Configure UFW

Step 1- Installing the UFW

Simply excute:

apt-get install ufw

Step 2- Enable UFW

ufw allow ssh
ufw enable

Step 3- Enable port forwarding

Simply execute the following  command:

sysctl -w net.ipv4.ip_forward=1

Step 5- Restart OpenVPN

systemctl restart openvpn@server.service

Configuration of UFW

UFW is a user-friendly interface of IPtables, so you if you are using UFW you can stick to it. for setting up OpenVPN configuration in UFW you should follow the instruction below.

First, you should allow your OpenVPN port and the protocol stack you are using for example:

ufw allow 1194/udp

Then you have to open UFW configuration file with a text editor to make some changes:

nano /etc/default/ufw

Look for DEFAULT_FORWARD_POLICY=”DROP” this must be changed from DROP to ACCEPT


Next, we will add additional UFW rules for NAT and IP Masquerading of connected clients.

nano /etc/ufw/before.rules

Add the red area to the file exactly like below: (replace the subnet value and your ethernet interface name)

# rules.before
# Rules that should be run before the ufw command line added rules. Custom
# rules should be added to one of these chains:
#   ufw-before-input
#   ufw-before-output
#   ufw-before-forward

# NAT table rules
# Allow traffic from OpenVPN client to eth0

# Don't delete these required lines, otherwise there will be errors

Creating connection for client

Now we have to create a “.ovpn” connection for the client, we are going to create our connection in ./openvpn/ directory you can create it anywhere  you want:

cd /etc/openvpn/
nano client.ovpn

Then paste the text below in your file, change IP and port values to yours:

dev tun
proto udp

#Server IP and Port
remote 1194

resolv-retry infinite
ca ca.crt
cert client.crt
key client.key
ns-cert-type server

Now you have to add your “ca.crt” “client.crt” “client.key” contents in your connection.

Remove following red lines:

dev tun
proto udp

#Server IP and Port
remote 1194

resolv-retry infinite
ca ca.crt
cert client.crt
key client.key
ns-cert-type server

Replace those three lines with something like below:

(insert ca.crt here)
(insert client.crt here)
(insert client.key here)

Now you have to copy the content of files and replace them with red lines:


# cat /etc/openvpn/easy-rsa/2.0/keys/ca.crt


# cat /etc/openvpn/easy-rsa/2.0/keys/client.crt


# cat /etc/openvpn/easy-rsa/2.0/keys/client.key

At the end you should have something like this:

. . .

. . .
. . .

. . .

Save and exit.

You may find following tutorials useful if you want to connect to a VPN server from Windows or Linux. OpenVPN is available on most operating systems including smartphones (e.g. Android) and MacOS.  All you need is to install a OpenVPN client and run the VPN connection file on your device.

How to connect to OpenVPN server through windows
How to connect to OpenVPN server through Linux

You can find more information about OpenVPN on its official website!

Was this tutorial helpful?

Thank you for your vote.Thank you for your vote.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *