Linux – connect OpenVPN client at Ubuntu startup

linuxlinux-networkingopenvpnUbuntuvpn

I would like to connect OpenVPN on Startup.

I've added sudo openvpn /file.ovpn to the crontab, and it doesn't connect at startup.

What is the proper procedure?

Best Answer

I've added sudo openvpn /file.ovpn to the crontab, and it doesn't connect at startup.

As an admin you typically use cron to schedule recurring jobs, not to run daemons and services that need to start boot.

Additionally, when you have admin rights, you don't use sudo in cron jobs, you set the correct user in the <user> field of the /etc/crontab or the /etc/cron.d/job.spec configuration file.

What is the proper procedure?

Read the manual: https://openvpn.net/index.php/open-source/documentation/howto.html#startup

Linux
If you install OpenVPN via an RPM or DEB package on Linux, the installer will set up an initscript. When executed, the initscript will scan for .conf configuration files in /etc/openvpn, and if found, will start up a separate OpenVPN daemon for each file.

So it may already be sufficient to simply copy your config file to /etc/openvpn/connection-name.conf


I recently had to set up OpenVPN on an Ubuntu 16.04 LTS server and the process was slightly more complicated, since with systemd the init script isn't used any more but instead a templated systemd unit file is used.

  1. Still copy your config file to /etc/openvpn/connection-name.conf
  2. Test if the "connection-name" OpenVPN connection will start with
    systemctl start openvpn@connection-name
  3. Confirm if the link gets established with:
    ip addr list and ip route list
    systemctl status openvpn@connection-name and/or
    journalctl --identifier ovpn-connection-name and/or
  4. Ensure that the service will persist across reboots:
    systemctl enable openvpn@connection-name