Install and Configure OpenVPN Client on CentOS 8/Ubuntu 18.04

0
2913

In order to connect to an OpenVPN server to allow you access your intranet local resources, you simply would need an OpenVPN client. In this guide, we are going to learn how to install and configure OpenVPN Client on CentOS 8/Ubuntu 18.04. Note that the OpenVPN software can be configured to either work as the server or the client.

Learn how to install and configure OpenVPN Server on CentOS 8 by following the link below;

Setup OpenVPN Server on CentOS 8

Install and Configure OpenVPN Client on CentOS 8/Ubuntu 18.04

To demonstrate the communication of two servers on different Intranets, we have two servers, Ubuntu 18.04 and CentOS 8 which cannot communicate as they are on different LAN networks only reachable via the OpenVPN Server. The two servers have NAT interfaces only attached.

Install OpenVPN Client on Ubuntu 18.04

OpenVPN maintains several OpenVPN (OSS) software repositories from which the latest and stable release version of OpenVPN client can be installed from. You however need to manually create these sources list as shown below;

Install OpenVPN repository signing key.

wget -O - https://swupdate.openvpn.net/repos/repo-public.gpg| sudo apt-key add -

Next, install the sources list for Ubuntu 18.04.

echo "deb http://build.openvpn.net/debian/openvpn/stable $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/openvpn-aptrepo.list

Update your package cache.

apt update

Install OpenVPN Client on Ubuntu 18.04

apt install openvpn

You can then verify the version by running the command below;

openvpn --version

Install OpenVPN Client on CentOS 8

On CentOS 8, the latest OpenVPN, version 2.4.8 as of this writing, is provided by the EPEL repos.

dnf info openvpn
Name         : openvpn
Version      : 2.4.8
Release      : 1.el8
Arch         : x86_64
Size         : 540 k
Source       : openvpn-2.4.8-1.el8.src.rpm
Repo         : epel
Summary      : A full-featured SSL VPN solution
URL          : https://community.openvpn.net/
License      : GPLv2
...

To install OpenVPN client on CentOS 8, you need to install EPEL repos, if you have not;

dnf install epel-release

You can then install OpenVPN client by executing the command;

dnf install openvpn

Configure OpenVPN Client on CentOS 8/Ubuntu 18.04

To be able to connect to OpenVPN server, you need to create the client’s configuration containing the CA certificate, the client server certificate and the key.

If you followed our guide on setting up OpenVPN server on CentOS 8, we described how to generate the clients certificate files and keys.

Once you have generated the keys, copy them to the client and take a note of the path where they are stored.

You also need to copy the HMAC key and the CA cert to the client.

You can then create the OpenVPN client configuration.

For example, to create an OpenVPN configuration file for the client, koromicha, whose certificates and keys are, koromicha.crt and koromicha.key;

vim koromicha.ovpn
client
tls-client
pull
dev tun
proto udp4
remote 192.168.2.132 1194
resolv-retry infinite
nobind
#user nobody
#group nogroup
persist-key
persist-tun
key-direction 1
remote-cert-tls server
auth-nocache
comp-lzo
verb 3
auth SHA512
tls-auth ta.key 1
ca ca.crt
cert koromicha.crt
key koromicha.key

Note that in this setup, the client certificate, the key, the CA certificate and the HMAC key are located on the same path as the OpenVPN client configuration itself, koromicha.ovpn.

ls
ca.crt koromicha.crt koromicha.key koromicha.ovpn ta.key

In order to avoid the issues with the paths to the certificates and the keys, you can put them inline on the configuration file;

client
tls-client
pull
dev tun
proto udp4
remote 192.168.2.132 1194
resolv-retry infinite
nobind
#user nobody
#group nogroup
persist-key
persist-tun
key-direction 1
remote-cert-tls server
auth-nocache
comp-lzo
verb 3
auth SHA512
<tls-auth>
-----BEGIN OpenVPN Static key V1-----
feb1af5407baa247d4e772c76aed6c75
...
-----END OpenVPN Static key V1-----
</tls-auth>
<ca>
-----BEGIN CERTIFICATE-----
MIIDTjCCAjagAwIBAgIUX0VQrHTgLDabUUIOAf7tD9cGp4YwDQYJKoZIhvcNAQEL
...
WA9BBk2shVWfR849Lmkep+GPyqHpU47dZAz37ARB2Gfu3w==
-----END CERTIFICATE-----
</ca>
<cert>
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
...
/7FvJaeLqmUHnvSs5eBlRZSgtOL19SCFkG0HXdnw3LtBaoHQXxgzOkDPW1+5
-----END CERTIFICATE-----
</cert>
<key>
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC+DI7kg6MsRoCs
...
6WdLcNtWKAcU294xJEZoOA8/
-----END PRIVATE KEY-----
</key>

Do the same on all the client servers for every user that needs to connect to the vpn.

If you noticed, the lines below are commented to avoid the error, ERROR: Linux route add command failed: external program exited with error status: 2 by flushing created routes before adding them again on reconnection.

#user nobody
#group nogroup

The OpenVPN client configuration file is now ready.

You can then connect to OpenVPN server on demand or configure your server to establish VPN configuration file whenever the system reboots.

To connect on demand, simple use the openvpn command as;

sudo openvpn client.ovpn

or

sudo openvpn --config client.ovpn

If the connection to the OpenVPN server is successful, you should see an Initialization Sequence Completed.

...
Wed Apr 14 15:23:19 2020 ROUTE_GATEWAY 10.0.2.2/255.255.255.0 IFACE=enp0s3 HWADDR=08:00:27:8d:b0:f8
Wed Apr 14 15:23:19 2020 TUN/TAP device tun0 opened
Wed Apr 14 15:23:19 2020 TUN/TAP TX queue length set to 100
Wed Apr 14 15:23:19 2020 /sbin/ip link set dev tun0 up mtu 1500
Wed Apr 14 15:23:19 2020 /sbin/ip addr add dev tun0 10.8.0.3/24 broadcast 10.8.0.255
Wed Apr 14 15:23:19 2020 /sbin/ip route add 192.168.2.132/32 via 10.0.2.2
Wed Apr 14 15:23:19 2020 /sbin/ip route add 0.0.0.0/1 via 10.8.0.1
Wed Apr 14 15:23:19 2020 /sbin/ip route add 128.0.0.0/1 via 10.8.0.1
Wed Apr 14 15:23:19 2020 Initialization Sequence Completed

To check the IP addresses;

[[email protected] ~]$ ip add show tun0
6: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 100
    link/none 
    inet 10.8.0.3/24 brd 10.8.0.255 scope global tun0
       valid_lft forever preferred_lft forever
    inet6 fe80::8d65:4038:acb0:b954/64 scope link stable-privacy 
       valid_lft forever preferred_lft forever
[email protected]:~$ ip add show tun0
10: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 100
    link/none 
    inet 10.8.0.2/24 brd 10.8.0.255 scope global tun0
       valid_lft forever preferred_lft forever
    inet6 fe80::bd3d:27ff:84d5:e587/64 scope link stable-privacy 
       valid_lft forever preferred_lft forever

Test connectivity between the two remote servers;

[email protected]:~$ ping 10.8.0.3 -c 4
PING 10.8.0.3 (10.8.0.3) 56(84) bytes of data.
64 bytes from 10.8.0.3: icmp_seq=1 ttl=64 time=3.78 ms
64 bytes from 10.8.0.3: icmp_seq=2 ttl=64 time=3.59 ms
64 bytes from 10.8.0.3: icmp_seq=3 ttl=64 time=3.61 ms
64 bytes from 10.8.0.3: icmp_seq=4 ttl=64 time=3.30 ms

--- 10.8.0.3 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3007ms
rtt min/avg/max/mdev = 3.307/3.576/3.786/0.172 ms
[[email protected] ~]$ ping 10.8.0.2 -c 4
PING 10.8.0.2 (10.8.0.2) 56(84) bytes of data.
64 bytes from 10.8.0.2: icmp_seq=1 ttl=64 time=6.77 ms
64 bytes from 10.8.0.2: icmp_seq=2 ttl=64 time=1.57 ms
64 bytes from 10.8.0.2: icmp_seq=3 ttl=64 time=4.37 ms
64 bytes from 10.8.0.2: icmp_seq=4 ttl=64 time=13.6 ms

--- 10.8.0.2 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 15ms
rtt min/avg/max/mdev = 1.571/6.572/13.577/4.443 ms

You should also be able to get internet access depending on your server routes setup.

Running OpenVPN Client as a Systemd Service

In order to establish connections automatically whenever the server reboots, you can enable OpenVPN client systemd service.

Before you can do this, change the extension of your VPN config file from .ovpn to .conf. Replace the file names accordingly.

cp koromicha.ovpn koromicha.conf

Copy the .conf file to OpenVPN client configurations directory, /etc/openvpn/client.

cp koromicha.conf /etc/openvpn/client

Next, start OpenVPN client systemd service. Replace the name koromicha with the name of your .conf configuration file.

systemctl start [email protected]koromicha

To check the status;

systemctl status [email protected]
[email protected] - OpenVPN tunnel for koromicha
   Loaded: loaded (/lib/systemd/system/[email protected]; disabled; vendor preset: enabled)
   Active: active (running) since Wed 2020-04-14 16:00:35 EAT; 8s ago
     Docs: man:openvpn(8)
           https://community.openvpn.net/openvpn/wiki/Openvpn24ManPage
           https://community.openvpn.net/openvpn/wiki/HOWTO
 Main PID: 6877 (openvpn)
   Status: "Initialization Sequence Completed"
    Tasks: 1 (limit: 2300)
   CGroup: /system.slice/system-openvpn\x2dclient.slice/[email protected]
           └─6877 /usr/sbin/openvpn --suppress-timestamps --nobind --config koromicha.conf

Elb 14 16:00:37 ubuntu1804.kifarunix-demo.com openvpn[6877]: Incoming Data Channel: Cipher 'AES-256-GCM' initialized with 256 bit key
Elb 14 16:00:37 ubuntu1804.kifarunix-demo.com openvpn[6877]: ROUTE_GATEWAY 10.0.2.2/255.255.255.0 IFACE=enp0s3 HWADDR=08:00:27:4b:ff:18
Elb 14 16:00:37 ubuntu1804.kifarunix-demo.com openvpn[6877]: TUN/TAP device tun0 opened
Elb 14 16:00:37 ubuntu1804.kifarunix-demo.com openvpn[6877]: TUN/TAP TX queue length set to 100
Elb 14 16:00:37 ubuntu1804.kifarunix-demo.com openvpn[6877]: /sbin/ip link set dev tun0 up mtu 1500
Elb 14 16:00:37 ubuntu1804.kifarunix-demo.com openvpn[6877]: /sbin/ip addr add dev tun0 10.8.0.2/24 broadcast 10.8.0.255
Elb 14 16:00:37 ubuntu1804.kifarunix-demo.com openvpn[6877]: /sbin/ip route add 192.168.2.132/32 via 10.0.2.2
Elb 14 16:00:37 ubuntu1804.kifarunix-demo.com openvpn[6877]: /sbin/ip route add 0.0.0.0/1 via 10.8.0.1
Elb 14 16:00:37 ubuntu1804.kifarunix-demo.com openvpn[6877]: /sbin/ip route add 128.0.0.0/1 via 10.8.0.1
Elb 14 16:00:37 ubuntu1804.kifarunix-demo.com openvpn[6877]: Initialization Sequence Completed

To enable it to run on system boot;

systemctl enable [email protected]

You have successfully installed and setup OpenVPN client on Ubuntu 18.04 and CentOS 8. That brings us to the end of our tutorial on how to install and configure OpenVPN Client on CentOS 8/Ubuntu 18.04.

Related Tutorials

Assign Static IP Addresses for OpenVPN Clients

Configure strongSwan VPN Client on Ubuntu 18.04/CentOS 8

Configure IPSEC VPN using StrongSwan on Ubuntu 18.04

Install and Configure OpenVPN Server FreeBSD 12

Install and Setup OpenVPN Server on Fedora 29/CentOS 7

LEAVE A REPLY

Please enter your comment!
Please enter your name here