Run OpenVPN Client as Service
Run OpenVPN client as a background service that starts on boot
Install openVPN
apt update
apt install openvpn
Create an ovpn folder to hold your ovpn files
cd /etc/openvpn
mkdir ovpn
Place all the ovpn files you have, in this folder. This is especially important if your VPN provider gives you openVPN files to multiple regions
Provide VPN credentials
Create a .test.auth
file in the /etc/openvpn
folder. This file would have 2 lines with your username and password as follows:
< username >
< password >
Note The auth file name should be .<vpn conf name>.auth
. In this case, its .test.auth
since its meant to be used for test.conf
Edit the systemd file to use this auth file
systemctl edit --full openvpn@test
Find the following line in it (look for the one that starts with ExecStart
:
ExecStart=/usr/sbin/openvpn --daemon ovpn-%i --status /run/openvpn/%i.status 10 --cd /etc/openvpn --script-security 2 --config /etc/openvpn/%i.conf --writepid /run/openvpn/%i.pid
Append the following to it:
--auth-user-pass /etc/openvpn/.%i.auth
The final result should be as follows:
ExecStart=/usr/sbin/openvpn --daemon ovpn-%i --status /run/openvpn/%i.status 10 --cd /etc/openvpn --script-security 2 --config /etc/openvpn/%i.conf --writepid /run/openvpn/%i.pid --auth-user-pass /etc/openvpn/.%i.auth
Create a link to the ovpn file you want to use
Create a symbolic link to the ovpn file in the /etc/openvpn
folder but with a .conf
extension instead of the .ovpn
extension
cd /etc/openvpn
ln -s ovpn/test.ovpn test.conf
Reload systemd for changes to reflect
systemctl daemon-reload
Managing the VPN connection
Enable the connection so that its started on boot
systemctl enable openvpn@test
Start the VPN connection created above by:
systemctl start openvpn@test
Check the status by:
systemctl status openvpn@test
Disconnect by:
systemctl stop openvpn@test