Graphic interfaces are cool, but sometimes they can get in the way of getting things done!

Maybe you need to connect to a VPN Server programmatically, or just hate the fancy colors of the UI.

Either way, we’re going to show you how to establish a VPN connection from the Ubuntu command line. In the examples below, I’ve used Ubuntu 18.04 LTS.

OpenVPN Command Line

OpenVPN, as the name suggests, it’s an OpenSource VPN Protocol. It has strong community backing, receiving constant updates and maintenance.

It’s regarded as being one of the most secure VPN options out there. Being easy to install and connect to, made it very popular in the community.

The fastest way to establish a connection with this protocol is to use an OpenVPN Configuration file (with the .ovpn extension). It contains all the information needed for the client to securely create a tunnel to the server.

Install the OpenVPN Client

Ubuntu 18.04 doesn’t come with OpenVPN already installed. We need to install it ourselves by running the command:

Download the “.OVPN” config file

Now, we need to get the VPN Server’s configuration file in order to establish a connection. If you don’t have already, you can get one from

Launch the OpenVPN client

To establish the VPN connection, run the OpenVPN client with the newly downloaded config and input your username and password.

If you don’t want to type in the password every time you connect to the VPN Server,  you can the vpn credentials in a file:

Add your username on one line and the password on the next one.

Now that we have the password file created, we can use it while connecting.

L2TP IPsec Connection

L2TP on its own is not secure enough, so we’ll need to pair this protocol with IPSec.

To establish an L2TP VPN connection from the Ubuntu command line, we must first install strongswan and xl2tpd

Install requirements

We’re going to use StrongSwan for IPSec.

Configure XL2TP

Open /etc/xl2tpd/xl2tpd.conf and configure it as seen below


Edit /etc/ppp/options.l2tpd.client 

Configure IPSec Strongswan

Configure Strongswan by editing /etc/ipsec.conf. Delete everything in that file and add the following:

Add the Secret

Add the IPSec Secret in /etc/ipsec.secrets. If you’re not using VPNBaron, make sure to replace with the server from the provider you are subscribed to.


Restart the services to reload the settings:

Establish the VPN Connection

This should add a ppp0 adapter. Run the ifconfig command to see it.

Redirecting traffic through the newly established VPN Connection:

Copy the IP from the output in default via ##.##.##.## . You’re going to need it to add a new route to that address.

Route the traffic

Redirect the traffic through the newly created tunnel:

Done! The connection is established and the traffic will be redirected through the VPN Server.