Home Linux How to Connect VPN on Boot in Linux ( VPN auto connect...

How to Connect VPN on Boot in Linux ( VPN auto connect )


The purpose of this “Connect VPN on Boot in Linux ( VPN auto connect )” is to guide you through managing command VPN scripts. We will use the bash script to run at startup, check if it connects to the VPN every 5 seconds, and if not, try to connect.

There is a very simple way to connect to a VPN from Ubuntu. Many VPNs offer excellent Linux clients. However, if you want to connect without a GUI, or if you want the connection to run automatically as a service for all users, these options will not work.

Most of the best VPN services are built on OpenVPN. OpenVPN is a free open source VPN server that you can actually use to set up your own VPN. However, there is no need to do this. You only need the client part of OpenVPN.

When you install OpenVPN on Ubuntu, you can also get the client. You can connect to any OpenVPN server using the OpenVPN client, regardless of whether the VPN service has a GUI client. OpenVPN is a service, so it can run when it starts up on your computer, which means you don’t have to remember to start it, and you don’t have to configure the connection separately for each user.

Step-by-Step Process to Connect VPN on Boot in Linux

Step 1:- Create a VPN connection in the Network Manager as usual. Let’s assume we have a vyprVPN account:

Username: [email protected]

Password: 123-456

Gateway: hk1.vpn.technicalustad.com

We built a VPN connection called VPN-HK1.

Step 2:- Modify the connection configuration.

Use Vim to edit /etc/NetworkManager/system-connections/VPN-HK1.

How to Connect VPN on Boot in Linux ( VPN auto connect )

$ sudo vim / etc / NetworkManager / system-connections / VPN-HK1

Change password-flags = 1 to password-flags = 0

Password flag = 0

Add the following at the bottom:


Password = 123-456

Be sure to change the VPNName and VPNPassword to match your configuration. Modified example:



UUID = 7594c25b-b7fe-46ae-91ac-a523ec97e26e

Type = VPN

Automatic connection = FALSE


Service Type = org.freedesktop.NetworkManager.pptp

Gateway = hk1.vpn.goldenfrog.com

Request -MPPE = YES

[email protected]

LCP echo failure = 5

Password flag = 0

LCP echo interval = 30


Method = car


Password = 123-456

How to Connect VPN on Boot in Linux ( VPN auto connect )

Step 3:- Obtain the VPN UUID

$ sudo nmcli con

The UUID in the NAcli query and configuration file is the same.


VPN-HK1 7594c25b-b7fe-46ae-91ac-a523ec97e26e vpn ……

Wired connection 1 2ab388f8-8170-4b9e-951b-893644b0ffd8 802-3-ethernet ……

Step 4:- Create bash scripts

After obtaining the UUID, you have edited the VPN configuration file. You need to create a script called autovpn.sh and save it in / root / vpn by doing the following:

$ sudo mkdir / root / vpn

$ sudo vim /root/vpn/autovpn.sh

Paste the following and make sure to replace VPN-PARAM with the UUID of the VPN:

#! /bin/celebration



NMCLI = “/usr/bin/nmcli”;

GREP = “/bin/grep”;

AWK = “/usr/bin/AWK”;

SLEEP = “/bin/sleep”;

ECHO = “/bin/echo”;

And [“real”]


VPNCON = $($ NMCLI con status uuid”$ VPN_UUID”| $ GREP VPN.VPN-STATE | $ AWK'{print $ 2}’);

If [[$ VPNCON! = “5”]]; then

$ SLEEP 1;

$ NMCLI con up uuid “$ VPN_UUID”;

Science fiction



Then, change the script user permissions (based on your automatic login user):

$ sudo chown root:root /root/vpn/autovpn.sh

Step 5:- Enable automatic connection

$ sudo vim / etc / profile

Add the following to the end of this file (run the command in the background, discard stdout and stderr):

/bin / bash /root/vpn/autovpn.sh> / dev / null 2>&1&

If you want to debug this script, you should redirect stdout and stderr to the log file:

/bin / bash /root/vpn/autovpn.sh >> / var / log / vpnLog 2>&1&


VPN auto connect

$ chmod 666 / var / log / vpnLog

Restart the computer, the script will launch and automatically check if it connects to the VPN every 5 seconds, if not, will try to establish a connection!

That’s it. Let us know if you face any difficulty.