Let's set up the second server first:
Step 1Install OpenVPN
QUOTE:
sudo apt update
sudo apt install openvpn easy-rsa
sudo apt install openvpn easy-rsa
Step 2: Create a CA Directory
OpenVPN is a virtual private network using TLS/SSL. This means that OpenVPN uses certificates to encrypt traffic between the server and clients. To issue trusted certificates, we will need to create our own CA.Create a user named e.g. openvpn-caand change to its home directory:
QUOTE:
sudo adduser openvpn-ca
sudo usermod -aG sudo openvpn-ca
sudo su -openvpn-ca
sudo usermod -aG sudo openvpn-ca
sudo su -openvpn-ca
QUOTE:
make-cadir ~ /openvpn-ca
cd ~ /openvpn-ca
cd ~ /openvpn-ca
Step 3: Set up CA variables
To set up our CA variables, we need to edit the vars. Open this file in your text editor:
QUOTE:
we whose
QUOTE:
~/openvpn-ca/vars
QUOTE:
. . .
export KEY_COUNTRY="US"
export KEY_PROVINCE="NY"
export KEY_CITY="New York City"
export KEY_ORG="CodeBy"
export KEY_EMAIL="admin@example.com"
export KEY_OU="Community"
. . .
export KEY_COUNTRY="US"
export KEY_PROVINCE="NY"
export KEY_CITY="New York City"
export KEY_ORG="CodeBy"
export KEY_EMAIL="admin@example.com"
export KEY_OU="Community"
. . .
QUOTE:
~/openvpn-ca/vars
QUOTE:
export KEY_NAME="vpnsrv2"
Step 4: Create a Certificate Authority
Now we can use the variables and easy-rsa utilities we've set up to create a certificate authority.Make sure you are in the CA directory and use the command sourceon the vars. In my case, I also needed to add a symlink to the file openssl-1.0.0.cnf:
QUOTE:
cd ~/openvpn-ca
ln -s ~/openvpn-ca/openssl-1.0.0.cnf openssl.cnf
source vars
ln -s ~/openvpn-ca/openssl-1.0.0.cnf openssl.cnf
source vars
QUOTE:
Conclusion
NOTE: If you run ./clean-all, I will be doing a rm -rf on /home/sammy/openvpn-ca/keys
NOTE: If you run ./clean-all, I will be doing a rm -rf on /home/sammy/openvpn-ca/keys
QUOTE:
./clean-all
QUOTE:
./build-ca
We now have a certificate authority that we can use to create all the other files we need.
Step 5: Create a certificate, key, and encryption files for the server
Next, let's create a certificate, a key pair, and some additional files used to implement encryption for our server.Let's start by creating an OpenVPN certificate and keys for the server. This can be done with the following command:
QUOTE:
Note: If you previously chose a name other than server, you will need to slightly change some of the instructions. For example, when copying the created files to the /etc/openvpn directory, you will have to replace the names with the ones you specify. You will also need to modify the /etc/openvpn/server.conf file to point to the correct .crt and .key files.
QUOTE:
./build-key-server vpnsrv2
Accept all defaults by pressing ENTER . Don't set a challenge password . At the end of the process, type y twice to sign and validate the creation of the certificate:
QUOTE:
Conclusion
. . .
Certificate is to be certified until May 1 17:51:16 2026 GMT (3650 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
. . .
Certificate is to be certified until May 1 17:51:16 2026 GMT (3650 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
QUOTE:
./build-dh
Next, we can generate an HMAC signature to enhance the server's ability to verify TSL integrity :
QUOTE:
sudo openvpn --genkey --secret keys/ta.key
sudo chown openvpn-ca
penvpn-ca keys/ta.key
sudo chown openvpn-ca
Step 6: Create a certificate and key pair for the client
Next, we can generate a certificate and a key pair for the client. In general, this can be done on the client machine and then signed by the server's certificate authority, but in this article, for simplicity, we will generate the signed key on the server.In this article, we will create a key and certificate for only one client. If you have multiple clients, you can repeat this process as many times as you like. Just pass a unique value to the script each time.
Since we can come back to this step later, we will repeat the command sourcefor the vars. We will use the option clientsrv2to generate the first certificate and key.
To create files without a password to facilitate automatic connections, use the command build-key:
QUOTE:
cd ~/openvpn-ca
source vars
./build-key clientsrv2
source vars
./build-key clientsrv2
Step 7Configure OpenVPN Service
Next, we will configure the OpenVPN service using the files we created earlier.Copying files to the OpenVPN directory We need to copy the files we need to the directory/etc/openvpn.
First, let's copy the files we created. They are in the directory ~/openvpn-ca/keysin which they were created. We need to copy the certificate and key of the certificate authority, the certificate and key of the server, the HMAC signature and the Diffie-Hellman file :
QUOTE:
cd ~/openvpn-ca/keys
sudo mkdir /etc/openvpn/keys
sudo cp ca.crt vpnsrv2.crt vpnsrv2.key dh2048.pem ta.key /etc/openvpn/keys/
sudo mkdir /etc/openvpn/keys
sudo cp ca.crt vpnsrv2.crt vpnsrv2.key dh2048.pem ta.key /etc/openvpn/keys/
QUOTE:
gunzip -c /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz | sudo tee /etc/openvpn/server.conf
QUOTE:
sudo you /etc/openvpn/server.conf
The address of the VPN server
QUOTE:
/etc/openvpn/server.conf
QUOTE:
# ethernet bridging. See the man page for more info.
server 10.8.1.0 255.255.255.0
server 10.8.1.0 255.255.255.0
QUOTE:
/etc/openvpn/server.conf
QUOTE:
tls-auth keys/ta.key 0 # This file is secret
key-direction 0
key-direction 0
QUOTE:
/etc/openvpn/server.conf
QUOTE:
cipher AES-256-CBC
QUOTE:
/etc/openvpn/server.conf
QUOTE:
auth SHA512
QUOTE:
/etc/openvpn/server.conf
QUOTE:
user nobody
group nogroup
group nogroup
QUOTE:
/etc/openvpn/server.conf
QUOTE:
;push "redirect-gateway def1 bypass-dhcp"
QUOTE:
QUOTE:
/etc/openvpn/server.conf
QUOTE:
;push "dhcp-option DNS 208.67.222.222"
;push "dhcp-option DNS 208.67.220.220"
;push "dhcp-option DNS 208.67.220.220"
QUOTE:
/etc/openvpn/server.conf
QUOTE:
port 1194
# TCP or UDP server?
proto tcp4
# TCP or UDP server?
proto tcp4
QUOTE:
/etc/openvpn/server.conf
QUOTE:
ca keys/ca.crt
cert keys/vpnsrv2.crt
key keys/vpnsrv2.key
dh keys/dh2048.pem
cert keys/vpnsrv2.crt
key keys/vpnsrv2.key
dh keys/dh2048.pem
Step 8. Setting up the network configuration of the server
Next, we need to set up the server's network configuration so that OpenVPN can forward traffic correctly.Configuring IP Redirection First let's allow the server to redirect traffic. This is the core functionality of our VPN server.
Set it up in a file /etc/sysctl.conf:
QUOTE:
sudo you /etc/sysctl.conf
QUOTE:
/etc/sysctl.conf
QUOTE:
net.ipv4.ip_forward=1
To apply the settings to the current session, type the command:
QUOTE:
sudo sysctl -p
QUOTE:
sudo apt update
sudo apt install ufw
sudo apt install ufw
QUOTE:
sudo you /etc/ufw/before.rules
QUOTE:
/etc/ufw/before.rules
QUOTE:
#
# 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
#
# START OPENVPN RULES
# NAT table rules
*nat
:POSTROUTING ACCEPT [0:0]
# Allow traffic from OpenVPN client to eth0
-A POSTROUTING -s 10.8.1.0/24 -o eth0 -j MASQUERADE
COMMIT
# END OPENVPN RULES
# Don't delete these required lines, otherwise there will be errors
. . .
# 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
#
# START OPENVPN RULES
# NAT table rules
*nat
:POSTROUTING ACCEPT [0:0]
# Allow traffic from OpenVPN client to eth0
-A POSTROUTING -s 10.8.1.0/24 -o eth0 -j MASQUERADE
COMMIT
# END OPENVPN RULES
# Don't delete these required lines, otherwise there will be errors
. . .
Now we need to tell UFW that it needs to allow forwarded packets by default. To do this, open the file /etc/default/ufw:
QUOTE:
sudo vi /etc/default/ufw
QUOTE:
/etc/default/ufw
QUOTE:
DEFAULT_FORWARD_POLICY="ACCEPT"
Opening the OpenVPN Port and Applying the Changes Next, we'll configure the firewall itself to allow traffic to OpenVPN .
If you did not change the port and protocol in the file , you will need to allow UDP/etc/openvpn/server.conf traffic on port 1194 . If you have changed these settings, enter the values you specified. In my case it is TCP port 1194
Also add your SSH port
QUOTE:
sudo ufw allow 22
sudo ufw allow 1194/tcp
sudo ufw allow 1194/tcp
QUOTE:
sudo ufw disable
sudo ufw enable
sudo ufw enable
Step 9Enable the OpenVPN service
We are ready to enable the OpenVPN service on our server. We can do this with systemd .We need to start the OpenVPN server by specifying the name of our configuration file as a variable after the systemd filename . The configuration file for our server is named /etc/openvpn/server.conf, so we'll add @serverto the end of the filename when we call it:
QUOTE:
sudo systemctl start openvpn@server
QUOTE:
sudo systemctl status openvpn@server
QUOTE:
sudo systemctl enable openvpn@server
Step 10: Create the Client Configuration Infrastructure
Next, let's set up the system to easily create configuration files for clients.Creating the client configuration directory structure In your home directory, create a directory structure for storing files:
QUOTE:
sudo su -openvpn-ca
mkdir -p ~/client-configs/files
mkdir -p ~/client-configs/files
QUOTE:
chmod 700 ~/client-configs/files
QUOTE:
cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf ~/client-configs/base.conf
QUOTE:
vi ~/client-configs/base.conf
First find the directive remote. This directive tells the client the address of our OpenVPN server . This should be the public IP address of your OpenVPN server . If you have changed the port that the OpenVPN server is listening on , change the default port to 1194your value:
QUOTE:
~/client-configs/base.conf
QUOTE:
. . .
# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
remote server_IP_address 1194
. . .
# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
remote server_IP_address 1194
. . .
QUOTE:
~/client-configs/base.conf
QUOTE:
proto tcp
QUOTE:
~/client-configs/base.conf
QUOTE:
# Downgrade privileges after initialization (non-Windows only)
user nobody
group nogroup
user nobody
group nogroup
QUOTE:
~/client-configs/base.conf
QUOTE:
# SSL/TLS parms.
# See the server config file for more
# description. It's best to use
# a separate .crt/.key file pair
# for each client. A single ca
# file can be used for all clients.
#ca ca.crt
#cert client.crt
#key client.key
# See the server config file for more
# description. It's best to use
# a separate .crt/.key file pair
# for each client. A single ca
# file can be used for all clients.
#ca ca.crt
#cert client.crt
#key client.key
QUOTE:
~/client-configs/base.conf
QUOTE:
cipher AES-256-CBC
auth SHA512
auth SHA512
QUOTE:
~/client-configs/base.conf
QUOTE:
key-direction 1
Create and open a file make_config.shinside the directory ~/client-configs:
QUOTE:
vi ~/client-configs/make_config.sh
QUOTE:
~/client-configs/make_config.sh
QUOTE:
#!/bin/bash
# First argument: Client identifier
KEY_DIR=~/openvpn-ca/keys
OUTPUT_DIR=~/client-configs/files
BASE_CONFIG=~/client-configs/base.conf
cat ${BASE_CONFIG} \
<(echo -e '<ca>') \
${KEY_DIR}/ca.crt \
<(echo -e '</ca>\n<cert>') \
${KEY_DIR}/${1}.crt \
<(echo -e '</cert>\n<key>') \
${KEY_DIR}/${1}.key \
<(echo -e '</key>\n<tls-auth>') \
${KEY_DIR}/ta.key \
<( echo -e'</tls-auth>') \
> ${OUTPUT_DIR}/${1}.ovpn
# First argument: Client identifier
KEY_DIR=~/openvpn-ca/keys
OUTPUT_DIR=~/client-configs/files
BASE_CONFIG=~/client-configs/base.conf
cat ${BASE_CONFIG} \
<(echo -e '<ca>') \
${KEY_DIR}/ca.crt \
<(echo -e '</ca>\n<cert>') \
${KEY_DIR}/${1}.crt \
<(echo -e '</cert>\n<key>') \
${KEY_DIR}/${1}.key \
<(echo -e '</key>\n<tls-auth>') \
${KEY_DIR}/ta.key \
<( echo -e'</tls-auth>') \
> ${OUTPUT_DIR}/${1}.ovpn
Make it executable with the command:
QUOTE:
chmod 700 ~/client-configs/make_config.sh
Step 11: Generating Client Configurations
Now we can easily generate client configuration files.If you followed all the steps in this article, you have generated clientsrv2.crtthe client certificate and key with the clientsrv2.keycommand ./build-key clientsrv2in step 6. You can generate the configuration for these files by navigating to the directory ~/client-configsand using the script we just created:
QUOTE:
cd ~/client-configs
./make_config.sh clientsrv2
./make_config.sh clientsrv2
QUOTE:
ls ~/client-configs/files
QUOTE:
Conclusion
clientsrv2.ovpn
clientsrv2.ovpn
On the first server, we do everything identically, except for the following points:
QUOTE:
sudo you /etc/ufw/before.rules
QUOTE:
/etc/ufw/before.rules
QUOTE:
#
# 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
#
# START OPENVPN RULES
# NAT table rules
*nat
:POSTROUTING ACCEPT [0:0]
# Allow traffic from OpenVPN client to tun1
-A POSTROUTING -s 10.8.0.0/24 -o tun1 -j MASQUERADE
COMMIT
# END OPENVPN RULES
# Don't delete these required lines, otherwise there will be errors
. . .
# 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
#
# START OPENVPN RULES
# NAT table rules
*nat
:POSTROUTING ACCEPT [0:0]
# Allow traffic from OpenVPN client to tun1
-A POSTROUTING -s 10.8.0.0/24 -o tun1 -j MASQUERADE
COMMIT
# END OPENVPN RULES
# Don't delete these required lines, otherwise there will be errors
. . .
Redirect all traffic through the VPN server
The address of the VPN server
QUOTE:
sudo you /etc/openvpn/server.conf
QUOTE:
/etc/openvpn/server.conf
QUOTE:
# ethernet bridging. See the man page for more info.
server 10.8.0.0 255.255.255.0
server 10.8.0.0 255.255.255.0
QUOTE:
/etc/openvpn/server.conf
QUOTE:
# TCP or UDP server?
proto udp4
proto udp4
QUOTE:
/etc/openvpn/server.conf
QUOTE:
push "redirect-gateway def1 bypass-dhcp"
QUOTE:
/etc/openvpn/server.conf
QUOTE:
push "dhcp-option DNS 208.67.222.222"
push "dhcp-option DNS 208.67.220.220"
push "dhcp-option DNS 208.67.220.220"
QUOTE:
explicit-exit-notify 1
Let's create a script /etc/openvpn/upstream-route.shcontaining the following commands:
QUOTE:
#! /bin/sh
ip rule add from 10.8.0.0/24 table 120
ip route add default dev tun1 table 120
exit 0
ip rule add from 10.8.0.0/24 table 120
ip route add default dev tun1 table 120
exit 0
QUOTE:
chmod +x /etc/openvpn/upstream-route.sh
QUOTE:
we clientsrv2.ovpn
QUOTE:
clientsrv2.ovpn
QUOTE:
script-security 2
up upstream-route.sh
up upstream-route.sh
QUOTE:
clientsrv2.ovpn
QUOTE:
dev tun1
QUOTE:
sudo cp clientsrv2.ovpn /etc/openvpn/client.conf
QUOTE:
# server
sudo systemctl start openvpn@server
sudo systemctl enable openvpn@server
# client to srv2
sudo systemctl start openvpn@client
sudo systemctl start openvpn@client
sudo systemctl start openvpn@server
sudo systemctl enable openvpn@server
# client to srv2
sudo systemctl start openvpn@client
sudo systemctl start openvpn@client
Connection on Linux
QUOTE:
sudo apt update
sudo apt install openvpn
sudo apt install openvpn
QUOTE:
sudo openvpn --config clientsrv1.ovpn