How to Connect to a VPN Automatically on Linux

There are easy ways to connect to your VPN from Ubuntu. A lot of VPNs provide excellent Linux clients. Even if they don’t, you can use the Network Manager applet to set up a VPN connection. However, if you want to connect without a GUI or you want your connection to run automatically as a service for all users, those options won’t really work.

Most of the best VPN services are built on OpenVPN. OpenVPN is a free and open-source VPN server that you can actually use to set up your own VPN. There’s no need to do that, though. You only need the client part of OpenVPN.

When you install OpenVPN on Ubuntu, you also get the client. You can use the OpenVPN client to connect to any OpenVPN server, regardless of the VPN service or whether they have a GUI client or not. OpenVPN is a service, so it can run on startup on your computer, which means you don’t have to remember to start it up, and you won’t have to configure the connection separately for every user.

Before you can connect to your VPN service, you need to install OpenVPN on Ubuntu. It’s available right in the repositories, so use apt to get that.

sudo apt install openvpn

You can also install Openvpn from your package manager, regardless of the distro you are using.

Most VPN providers have available configurations for OpenVPN. Check to see if your VPN provider supports OpenVPN (it probably does) and look around for their configuration files.

VPN Provider Downloads

They may have a page dedicated to providing them, or they might just have a big “.zip” package with all of their available configurations. Either way, you can download what you want or need. If they are individual files, they’re usually named after the server location, and they end in the “.ovpn” extension.

Once you have your file or archive, you’ll need to place it in OpenVPN’s directory. If you need to extract the files from a zip file, do that first.

unzip openvpn.zip

Copy the file to the OpenVPN directory and rename it “openvpn.conf.”

sudo cp ~/Downloads/OpenVPN/'Northeast US.ovpn' /etc/openvpn/openvpn.conf

Your path and file name will be different. The examples here are made up, but a lot of VPNs use a structure like the examples.

It sort of defeats the purpose of having OpenVPN start automatically if you need to enter your username and password every time you connect. Thankfully, OpenVPN supports automatic login with a file.

OpenVPN Configuration

Open “/etc/openvpn/openvpn.conf” with sudo. Look for a line that contains “auth-user-pass.” It’ll probably be sitting there alone with nothing else on the line. After it, add “auth.txt” on the same line. Save the file and close it.

Create a new file in “/etc/openvpn” called “auth.txt.” On the first line of the file, place your username. Include your password on the second line. There is no need for anything else in the file. Save and close it.

That’s all you need to do to connect to your VPN. Restart the OpenVPN service for the changes to take effect.

OpenVPN Service Running

sudo systemctl restart openvpn

To make sure that OpenVPN starts up every time you start your computer, enable it with systemd. It is probably already enabled, but it’s not a bad idea to double-check.

sudo systemctl enable openvpn

DNS Leak Test Result

Finally, test out your VPN connection with dnsleaktest.com. When you arrive at the site, you should see the IP address and location of your VPN. Click on the button below to run the extended test and see that no DNS information is leaking out.

Image credit: Vpn written on a wooden cubes

Leave a Reply

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.