Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

VPNs prevent cyber threats by hiding the system’s IP address and encrypting all internet traffic. Therefore, by automatically connecting to a VPN in Linux, we can enhance the security and privacy of the system.

In this tutorial, we’ll cover two methods to connect to a VPN automatically in Linux.

2. Installing a VPN Tool

Before moving forward, first, let’s install a VPN tool in Linux. Here, we’re installing the RiseupVPN tool.

To proceed with the installation in the Debian-based systems, we utilize the apt tool and run a command from the Linux terminal:

$ sudo apt install riseup-vpn

In case we want to install the RiseupVPN tool on Arch and Arch-derivatives, we can use the yay command:

$ sudo yay riseup-vpn

After installing, we can open the tool by searching it from the Start menu. Alternatively, we can run a command:

$ riseup-vpn.launcher
qml: flavor: riseup-vpn
2025/02/20 23:38:51 Client expects anon auth
2025/02/20 23:38:51 Fetching MOTD for riseup.net
qml: delay...
2025/02/20 23:38:55 There are 4 pending messages
2025/02/20 23:38:55 firewall stop
2025/02/20 23:38:55 Fetching gateways...
qml: show motd
qml: configured locale: en
qml: platform: linux
...output truncated...

After the execution of the command, we can see the main page of the RiseupVPN tool:

RiseupVPN main page

Hence, we successfully installed the RiseupVPN tool in our system.

3. Using nmcli

nmcli is a command-line interface of the Network Manager utility in Linux. Additionally, it’s responsible for managing network connections, firewalls, routing, and network security.

First, let’s list all the connections using nmcli:

$ nmcli connection show
NAME     UUID                                  TYPE      DEVICE 
eduwifi  1eef7e45-3b9d-3043-bee3-fc5925c90273  ethernet  enp0s3 
tun0     3f107e93-d2d2-4adb-bd2e-b27fc1abd58b  tun       tun0   
lo       622431e0-3294-4d7a-aa30-28d853f0023a  loopback  lo 

As we can see, there are three available network connections in our system:

  • eduwifi represents the regular ethernet connection
  • tun0 is the VPN connection
  • lo denotes the loopback interface, which is typically used for local networking

Now, let’s view the details of the VPN connection:

$ nmcli connection show tun0
connection.id:                          tun0
connection.uuid:                        3f107e93-d2d2-4adb-bd2e-b27fc1abd58b
connection.stable-id:                   --
connection.type:                        tun
connection.interface-name:              tun0
connection.autoconnect:                 yes
connection.autoconnect-priority:        0
connection.autoconnect-retries:         -1 (default)
connection.multi-connect:               0 (default)
connection.auth-retries:                -1
connection.timestamp:                   1740138955
...output truncated...

Here, it’s important to note the name of the VPN connection. Moving forward, we modify the VPN network connection:

$ nmcli connection modify tun0 connection.autoconnect yes

Hence, we modified the tun0 VPN connection so that whenever we open our Linux system, the OS automatically turns on the RiseupVPN tool installed in our system.

Finally, we restart the Network Manager utility to save the changes we made with the VPN connection using the systemctl command:

$ sudo systemctl restart NetworkManager

After restart, the RiseupVPN tool will automatically start upon logging in to the system.

4. Using .bashrc

An alternative way to connect to a VPN automatically upon logging in to the system is to modify the .bashrc file.

.bashrc is a configuration file for the Bash shell in Linux. Generally, The OS executes it when we start an interactive, non-login shell. Furthermore, we use the .bashrc file to customize the shell environment, create command aliases, define custom functions, and enable command autocompletion.

Now, it’s important to note that the OS doesn’t execute the .bashrc file upon login. Therefore, the first step is ensuring the OS executes it when a user logs in to the system. Hence, to do this, we manually source it inside .bash_profile:

if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi

The next step is to open the .bashrc file:

$ nano ~/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
...output truncated... 

Furthermore, at the end of the file, we add a command that will ensure that the RiseupVPN starts automatically:

riseup-vpn &

Next, we save the file and exit from the text editor. Finally, we reload the file to reflect the changes we made:

$ source ~/.bashrc

This method works for a single user. In case of multiple users, we can use .profile instead of .bashrc to connect to a VPN tool automatically.

5. Conclusion

In this article, we discussed two ways to connect to a VPN in Linux automatically.

Both methods can efficiently enable the VPN autoconnection option in Linux. However, besides VPN connection, the nmcli tool provides us the option of managing other networks such as Wi-Fi, ethernet, and mobile broadband.