How to auto start openvpn (client) on Ubuntu CLI?

  1. Download the OpenVPNConfigFile.ovpn. Note that you can rename the file to anything you like.

  2. Move the ovpn file to /etc/openvpn

  3. cd /etc/openvpn folder and enter sudo nano yourserver.txt

    your_server_user_name
    your_server_passowrd
    

    Save and Close

  4. sudo nano OpenVPNConfigFile.ovpn

    Find auth-user-pass and add yourserver.txt next to it so that it becomes

    auth-user-pass yourserver.txt
    

    This will allow you to skip entering your credentials everytime you start openvpn connection

  5. Rename OpenVPNConfigFile.ovpn to OpenVPNConfigFile.conf

    sudo mv OpenVPNConfigFile.ovpn OpenVPNConfigFile.conf
    
  6. sudo nano /etc/default/openvpn

    Uncomment AUTOSTART="all"

  7. sudo service openvpn start

    You should see a message saying that you are connected. The connection will be established every time you start your computer.


The openvpn package comes with an init script /etc/init.d/openvpn. This script automatically sets up connection for every .conf (mind the extension) file in /etc/openvpn.

Found this based on information here: https://openvpn.net/index.php/open-source/documentation/howto.html#startup

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.


It would be nice to have a un hacker way of doing it, but this will have to do for now.

  1. Create file myopenvpn in /etc/init.d/

    nano /etc/init.d/myopenvpn
    
  2. Insert into myopenvpn and save:

    # OpenVPN autostart on boot script
    
    start on runlevel [2345]
    stop on runlevel [!2345]
    
    respawn
    
    exec /usr/sbin/openvpn --status /var/run/openvpn.client.status 10 --cd /etc/openvpn --config /etc/openvpn/client.conf --syslog openvpn
    

SOURCE: How to auto start OpenVPN client in Debian 6 and Ubuntu 12.04