Today I had a case where a coworker wanted a Linux server to connect to a particular VPN, and we didn’t want to make it use some hacky way like putting a script in /etc/rc.local for it to run on boot. By using systemd we learned how to use it to control connecting to the VPNs, using the OpenVPN client.
Steps
Install the OpenVPN client
Verify the openvpn client is installed
- Debian/Ubuntu
apt-get install openvpn- RHEL/CentOS
yum install openvpnGet VPN keys, certifiates and configs
- From your remote host, get the files or zip file that includes your VPN keys, certificates and configs
- Place the files (unzipping any archives) into
/etc/openvpnon your client
Configure OpenVPN session
- In the directory
/etc/openvpncopy the.opvnfile to.conf(renaming .conf with the host or filename of the .opvn file - openvpn is looking for any .conf file in this directory)
cd /etc/openvpncp file.opvn file.confThe new file.conf will be identical to file.opvn, and will include all connection steps. It will look somemthing like this (yours will be different, but should have similar steps):
clientdev tunproto udpremote 106.132.15.101 1876resolv-retry infinitenobindpersist-keypersist-tunca ca.crtcert default.crtkey default.keytls-auth tlsauth.key 1ns-cert-type servercipher AES-128-CBCcomp-lzoverb 4script-security 3route 10.122.17.0 255.255.255.0route 10.122.120.0 255.255.255.0Enable this config in OpenVPN, so systemd can use it
- edit /etc/default/openvpn, and uncomment the following line
AUTOSTART="all"- exit and save
/etc/default/openvpn - NOTICE: I’m just choosing
allbut if you had the files with different names you could call out specific ones. So for example, if you had/etc/openvpn/worksucks.confyou’d have:
AUTOSTART="worksucks"haha!
Reload / Restart services to use the new files
- Reload
systemddaemon so it will pickup the changes
systemctl daemon-reload- Restart OpenVPN so it will automatically connect to the VPN listed in
file.conf
systemctl restart openvpnDebug
- check logs to verify everything worked
tail -f /var/log/syslog- look for success messages, you should see something like:
Feb 22 13:01:03 localhost NetworkManager[996]: <info> [1487790063.9729] manager: (tun0): new Tun device (/org/freedesktop/NetworkManager/Devices/43)Feb 22 13:01:03 localhost ovpn-host[2915]: /sbin/ip addr add dev tun0 local 10.255.59.14 peer 10.255.59.13Feb 22 13:01:03 localhost ovpn-host[2915]: /sbin/ip route add 10.122.17.0/24 via 10.255.59.13Feb 22 13:01:03 localhost ovpn-host[2915]: /sbin/ip route add 10.122.120.0/24 via 10.255.59.13Feb 22 13:01:03 localhost ovpn-host[2915]: /sbin/ip route add 10.102.59.0/24 via 10.255.59.13Feb 22 13:01:03 localhost ovpn-host[2915]: /sbin/ip route add 10.255.59.1/32 via 10.255.59.13Feb 22 13:01:03 localhost ovpn-host[2915]: Initialization Sequence CompletedFeb 22 13:01:03 localhost NetworkManager[996]: <info> [1487790063.9801] devices added (path: /sys/devices/virtual/net/tun0, iface: tun0)Done
Now systemd is handling your VPN connections, and will keep them up for you. Notice you can stop/start them on demand, instead of just having them start at boot, by:
service openvpn@worksucks restartPretty fly, so that’s it for now, have fun out there, but stay safe!