20

I'm using Ubuntu 12.04 64bit. I have an *.ovpn file that works if I type:

sudo openvpn client.ovpn

Now I would like to start up openvpn when I boot the computer.

How can I do this?

6 Answers 6

34
  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.

4
  • 9
    If you're running systemd you will need to run systemctl daemon-reload before restarting the service Feb 16, 2017 at 5:32
  • Is this the for a server ? Sep 10, 2020 at 14:57
  • what about if my config doesn't have user and pass? May 20, 2022 at 15:16
  • 1
    @HenryNavarro in that case, you just skip to step 5 and no adjustments needed.
    – oemb1905
    May 22, 2023 at 6:40
6

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.

4

I got a bit stuck on this and ended up writing out all of the instructions for setting it up with systemd manually.

This worked for me using Ubuntu 16.10 and openvpn 2.3.11

Setting up your vpn to run from bash

These examples use expressvpn but most would work the same way

Download your vpn provider's ovpn config file e.g. my_express_vpn_amsterdam_2.ovpn

move that to /etc/openvpn/ and rename it to end in .conf

sudo mv ~/Downloads/my_express_vpn_amsterdam_2.ovpn /etc/openvpn/amsterdam-2.conf

Your VPN provider will provide you with a username and password for connecting over openvpn. Save the userename and then password each on their own line

sudo vim /etc/openvpn/express-vpn-crednetials.txt
# add these lines
YOUR_VPN_USERNAME
YOUR_VPN_PASSWORD
# save the file

now edit /etc/openvpn/amsterdam-2.conf look for a line that says auth-user-pass and replace it with the path to your credential file

auth-user-pass /etc/openvpn/express-vpn-crednetials.txt

Test your config! Start openvpn like this

sudo openvpn --config /etc/openvpn/amsterdam-2.conf

openvpn should connect without asking for username or password

Remove existing (broken) service config for openvpn

sudo rm /etc/systemd/system/multi-user.target.wants/openvpn.service

Set up openvpn to run as a systemd service

The config that came with openvpn was broken so I removed it and created a new one based on this answer

Create systemd service for openvpn

sudo vim /usr/lib/systemd/system/openvpn@service

add this config:

[Unit]
Description=OpenVPN Robust And Highly Flexible Tunneling Application On %I
After=syslog.target network.target

[Service]
PrivateTmp=true
Type=forking
PIDFile=/var/run/openvpn/%i.pid
ExecStart=/usr/sbin/openvpn --daemon --writepid /var/run/openvpn/%i.pid --cd /etc/openvpn/ --config %i.conf

[Install]
WantedBy=multi-user.target

the %iis used as a wildcard so that this service can be used for multiple vpn configurations. Set it up for the amsterdam-2.conf file that we created earlier

sudo systemctl start [email protected]

the systemd service should now be running on the amsterdam vpn. check its status like so

sudo systemctl status [email protected]

you should see several lines of output ending in Initialization Sequence Completed and your vpn should be running.

Hope this helps! related reading:

1
  • I execute your steps and things work as demonstrated by the "systemctl status" but after reboot when running the same status command it says "Active: inactive". Any thoughts? Jan 27 at 20:57
2

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

2

There are prebuild scripts if you take a look at /usr/lib/systemd/system/[email protected]

So you accutally just need 2 steps:

  1. Copy your config file to /etc/openvpn/client/: mv client.ovpn /etc/openvpn/client/client.conf
  2. Start the service: sudo systemctl start [email protected]

If there's a file /etc/openvpn/client/ttt.conf, so the service command should be sudo systemctl start [email protected]

0

If your client.ovpn file doesn't contain a password, then use the following:

  1. Copy your client.ovpn file to /etc/openvpn/

    sudo cp -i client.ovpn /etc/openvpn/

  2. Go to the openvpn folder

    cd /etc/openvpn/

  3. Rename the file from client.ovpn to be client.conf

    sudo mv -i client.ovpn client.conf

  • Now it's ready,

    • To connect use:

      sudo /etc/init.d/openvpn start

    • To stop openvpn use:

      sudo /etc/init.d/openvpn stop

    • To reconnect use:

      sudo /etc/init.d/openvpn restart

    • To check your ip:

      curl ifconfig.co

This should work on startup.

Note: when you put your computer to sleep you should restart or stop Openvpn to be able to connect to the internet

This answer derived from @Robert Polson post.

This is tested on Ubuntu 22.04

1
  • Does client.conf is mandatory name ?
    – emoxam
    Aug 4, 2023 at 13:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .