Maximizing Privacy: Setting Up OpenVPN on Your Linux System

Photo by Privecstasy on Unsplash

Maximizing Privacy: Setting Up OpenVPN on Your Linux System

How I Setup OpenVPN on my Linux Machine.

If you are reading this, there is a great chance you already know what a VPN is. You may use a Linux machine for development and have just searched “how to set up OpenVPN in Linux” or something along those lines.

So let’s get right into it.

💡
If you still want to know more about VPNs and why you should use them, check out the below resources.

Download Configuration File.

Head over to the Vpnbook website and download the configuration file for OpenVPN.

Navigate to the OpenVpn tab, then click on the configuration file with the arrow pointing at it(from the above image as that is what we will be using) to download it. Also, take note of the encircled username and password .
Once the download is complete, you will find the zip file (eg: vpnbook-openvpn-ca196.zip) in your downloads folder or the location specified as your default download directory.

Unzip/Extract the Config File.

In your terminal, cd into your downloads directory and unzip the file.

unzip <zipfile>

#eg: unzip vpnbook-openvpn-ca196.zip

After unzipping the file, navigate to the extracted folder and a new file pass.txt . This file will contain your openbookvpn username and password(as circled in the above image)

Copy and paste the username and password you are provided with(NB: this must have changed at the time you are reading this, so ensure you have a fresh one from the site)

#pass.txt
vpnbook
c28hes5

Your extracted folder should look like this by now.

Execute the following command.

Now select one of the .ovpn files (configuration files for the OpenVPN software that contain the settings and access configurations for a virtual private network) You will then run the below command using your file of choice.

sudo openvpn --config <your selected vpn config file.ovpn>  --auth-user-pass pass.txt

You should be prompted for your password, after which your VPN runs. You can confirm by visiting whatismyipaddress .

Command too long? Let’s turn it into a Bash Script.

Having to type the above command every time seems very tedious and there is a high chance you might not always remember every part of it. So let’s go ahead and make a bash script for it.

1. Create a Bash Script: Create a new file (e.g., start-vpn.sh) in your preferred directory.

nano start-vpn.sh

2. Add the command: Paste the command into the file we just created.

sudo openvpn --config /path/to/your_selected_vpn_config_file.ovpn --auth-user-pass /path/to/pass.txt

Make sure you have the correct file path name for both your selected .ovpn and pass.txt files.

3. Make the Script Executable: Using the chmod command, make the script executable

chmod +x start-vpn.sh

4. Add Script to PATH: Move the script to a directory in your PATH(eg: /usr/local/bin). This will help you run the script from any directory you are in.

sudo mv start-vpn.sh /usr/local/bin/start-vpn

5. Start your VPN: Now you can start your VPN with just a simple command in your terminal

start-vpn

Run Command in the background(Optional).

To run the VPN command or Bash Script in the background without blocking your terminal(ie your terminal remains free for other tasks while the VPN runs in the background), make the following adjustments to the command in your bash file.

sudo openvpn --config /path/to/your_selected_vpn_config_file.ovpn --auth-user-pass /path/to/pass.txt &> /dev/null &

Explanation for new additions

  • &> /dev/null redirects both standard output (stdout) and standard error (stderr) to /dev/null to suppress any logs or output in the terminal. If you want to log the output instead of suppressing it, you can redirect it to a file (e.g., &> vpn.log).

  • & runs the command in the background, so it doesn't block the terminal.

You can check if the OpenVPN process is running in the background using:

ps aux | grep openvpn

Final Words.

Congratulations, if you have read this far and followed each step, you’ve successfully set up your VPN connection using OpenVPN.

Go ahead and enjoy the internet with peace of mind!

If you find this helpful, do well to share it with your friends and leave a comment.

Stay safe and secure!!!