Best practice for installing & autostarting OpenVPN client/server configurations on Ubuntu
This post details my systemd-based setup for installing and activating OpenVPN client or server configs on Ubuntu. It might also work for other Linux distributions that are based on systemd..
First, place the OpenVPN config (usually a .ovpn
file, but it can also be a .conf
file) in /etc/openvpn
. **You need to change the filename extension to .conf
- .ovpn
won’t work.**Furthermore, ensure that there are no spaces in the filename.
In this example, our original OpenVPN config will be called techoverflow.ovpn
, hence it needs to be copied to /etc/openvpn/techoverflow.conf
!
Now we can enable (i.e. autostart on boot - but not start immediately) the config using
sudo systemctl enable openvpn@techoverflow
For techoverflow.conf
you need to systemctl enable``openvpn@techoverflow
whereas for a hypothetical foo.conf
you would need to systemctl enable openvpn@foo
.
Now we can start the VPN config - i.e. run it immediately using
sudo systemctl start openvpn@techoverflow
Now your VPN client or server is running - or is it? We shall check the logs using
journalctl -xfu openvpn@techoverflow
In order to manually restartthe VPN client or server use
sudo systemctl restart openvpn@techoverflow
and similarly run this to stop the VPN client or server:
sudo systemctl stop openvpn@techoverflow
In order to show if the instance is running - i.e. show its status, use
sudo systemctl status openvpn@techoverflow
Example output for an OpenVPN client:
● [email protected] - OpenVPN connection to techoverflow
Loaded: loaded (/lib/systemd/system/[email protected]; enabled; vendor preset: enabled)
Active: active (running) since Sun 2020-11-29 03:37:52 CET; 953ms ago
Docs: man:openvpn(8)
https://community.openvpn.net/openvpn/wiki/Openvpn24ManPage
https://community.openvpn.net/openvpn/wiki/HOWTO
Main PID: 4123809 (openvpn)
Status: "Pre-connection initialization successful"
Tasks: 1 (limit: 18689)
Memory: 1.3M
CGroup: /system.slice/system-openvpn.slice/[email protected]
└─4123809 /usr/sbin/openvpn --daemon ovpn-techoverflow --status /run/openvpn/techoverflow.status 10 --cd /etc/openvpn --script-security 2 --config /etc/ope>
Nov 29 03:37:52 localgrid systemd[1]: Starting OpenVPN connection to techoverflow...
Nov 29 03:37:52 localgrid ovpn-techoverflow[4123809]: OpenVPN 2.4.7 x86_64-pc-linux-gnu [SSL (OpenSSL)] [LZO] [LZ4] [EPOLL] [PKCS11] [MH/PKTINFO] [AEAD] built on Sep >
Nov 29 03:37:52 localgrid ovpn-techoverflow[4123809]: library versions: OpenSSL 1.1.1f 31 Mar 2020, LZO 2.10
Nov 29 03:37:52 localgrid systemd[1]: Started OpenVPN connection to techoverflow.
Nov 29 03:37:52 localgrid ovpn-techoverflow[4123809]: TCP/UDP: Preserving recently used remote address: [AF_INET]83.135.163.227:19011
Nov 29 03:37:52 localgrid ovpn-techoverflow[4123809]: UDPv4 link local (bound): [AF_INET][undef]:1194
Nov 29 03:37:52 localgrid ovpn-techoverflow[4123809]: UDPv4 link remote: [AF_INET]83.135.163.22:19011
Nov 29 03:37:53 localgrid ovpn-techoverflow[4123809]: [nas-vpn.haar.techoverflow.net] Peer Connection Initiated with [AF_INET]83.135.163.227:19011