Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Automatically start vpn on boot up ?

If none of the specific sub-forums seem right for your thread, ask here.
Message
Author
parkerc
Posts: 22
Joined: 2020-02-22 09:19

Automatically start vpn on boot up ?

#1 Post by parkerc »

Hi

I have the Network Manager set on my Debian 10 and a couple of VPN connections configured.
Is there a way I can have the VPN start-up automatically on login - I could not see a setting listed anywhere for this

Chrisdb
Posts: 279
Joined: 2018-04-10 07:16

Re: Automatically start vpn on boot up ?

#2 Post by Chrisdb »

Take a look at 'DISPATCHER SCRIPTS':
https://manpages.debian.org/stretch/net ... .8.en.html

An example for VPN can be found here:
https://wiki.archlinux.org/index.php/Ne ... stablished

parkerc
Posts: 22
Joined: 2020-02-22 09:19

Re: Automatically start vpn on boot up ?

#3 Post by parkerc »

Thanks - ok - I’m still learning Linux - and while I have a rough idea what it’s trying to do - is there any chance you could help to make this relevant for a wired connection (not wireless)

Code: Select all

 
# file to be called /etc/NetworkManager/dispatcher.d/vpn-up
#!/bin/sh
VPN_NAME="name of VPN connection defined in NetworkManager"
ESSID="Wi-Fi network ESSID (not connection name)"

interface=$1 status=$2
case $status in
  up|vpn-down)
    if iwgetid | grep -qs ":\"$ESSID\""; then
      nmcli connection up id "$VPN_NAME"
    fi
    ;;
  down)
    if iwgetid | grep -qs ":\"$ESSID\""; then
      if nmcli connection show --active | grep "$VPN_NAME"; then
        nmcli connection down id "$VPN_NAME"
      fi
    fi
    ;;
esac

Chrisdb
Posts: 279
Joined: 2018-04-10 07:16

Re: Automatically start vpn on boot up ?

#4 Post by Chrisdb »

are you connecting with ethernet or wirelesss...
What's the name of your configured VPN

parkerc
Posts: 22
Joined: 2020-02-22 09:19

Re: Automatically start vpn on boot up ?

#5 Post by parkerc »

Chrisdb wrote:are you connecting with ethernet or wirelesss...
What's the name of your configured VPN
Hi, I’m connecting via Ethernet and looking in network manager , the vpn name is called uk-nodecentral-udp

Chrisdb
Posts: 279
Joined: 2018-04-10 07:16

Re: Automatically start vpn on boot up ?

#6 Post by Chrisdb »

Are you familiar with shell scripting...
If not, online are many tutorials to get you started.

parkerc
Posts: 22
Joined: 2020-02-22 09:19

Re: Automatically start vpn on boot up ?

#7 Post by parkerc »

I’ve read various online resources - but I don’t know whether it’s my age but anything programming language seems to be difficult for me - hence I have such admiration for those that just get it :)

I’d like to set up a Debian device as a sort of Kiosk mode - so that it only connects via a VPN, which I’ve been able to do (via ufw rules) - now the only hurdle left is the auto-connect - I naively thought that would be a simple setting in the UI (either enforce VPN or just to have VPN start on start up)

If you or anyone else could direct me in the changes required to the script above or event share a working example for wired connections that would be great :)

Chrisdb
Posts: 279
Joined: 2018-04-10 07:16

Re: Automatically start vpn on boot up ?

#8 Post by Chrisdb »

I'm using my android phone here (typing not always correct :-)), but it should be something like this after a quick look:

Code: Select all

#!/bin/sh
VPN_NAME="uk-nodecentral-udp"

interface=$1 status=$2
case $status in
  up|vpn-down)
      nmcli connection up id "$VPN_NAME"
    ;;
  down)
      if nmcli connection show --active | grep "$VPN_NAME"; then
        nmcli connection down id "$VPN_NAME"
      fi
    ;;
esac
Also to enable dispatcher, run:

Code: Select all

sudo systemctl enable NetworkManager-dispatcher.service

parkerc
Posts: 22
Joined: 2020-02-22 09:19

Re: Automatically start vpn on boot up ?

#9 Post by parkerc »

Many thanks @chrisDB

I had been looking at the original script over the past couple of days to see what would need to change - and was getting confused if I needed to confirm eth0 first; before the VPN - but logically now I guess that’s an irrelevant call - because if the wired/Ethernet interface is down ; not much is going to happen :)

parkerc
Posts: 22
Joined: 2020-02-22 09:19

Re: Automatically start vpn on boot up ?

#10 Post by parkerc »

Thinking about the associated Cron job

How frequently should something like this run?

* * * * * /etc/NetworkManager/dispatcher.d/vpn-up.sh

Chrisdb
Posts: 279
Joined: 2018-04-10 07:16

Re: Automatically start vpn on boot up ?

#11 Post by Chrisdb »

It shouldn't be scheduled because it is event triggered.
F.e. UP = when the interface has been activated.

parkerc
Posts: 22
Joined: 2020-02-22 09:19

Re: Automatically start vpn on boot up ?

#12 Post by parkerc »

Ok, that’s cool to know.

A few follow on questions

As it’s an event / does that mean within the above script if the vpn goes down - can I explore adding additional lines to notify me via e.g an email or some other messaging facility ?

In situations where the VPN does go down - but the failure is on the provider/server side - will it keep trying every x seconds or x minutes to restart the connection - will it try indefinitely ?

Is the enabling of dispatcher a one time thing or does it need to be done every time?

Chrisdb
Posts: 279
Joined: 2018-04-10 07:16

Re: Automatically start vpn on boot up ?

#13 Post by Chrisdb »

- yes you can add extra functionality in your script within the if block
- restart depends on options used with nmcli, there is a 'wait' option, but I would advice against it
- systemctl enable command is required only once

parkerc
Posts: 22
Joined: 2020-02-22 09:19

Re: Automatically start vpn on boot up ?

#14 Post by parkerc »

Hi @chrisDB

Before I try to enhance it - I’m sadly still unable to get the script to work :( Here’s what I’ve done so far..

1) Created a .sh file and called it up-vpn.sh - and is stored in /home/me/Documents/

Code: Select all

 # http://forums.debian.net/viewtopic.php?f=10&t=145355

#!/bin/sh
VPN_NAME="nl2-nodecentral-udp-udp"

interface=$1 status=$2
case $status in
  up|vpn-down)
      nmcli connection up id "$VPN_NAME"
    ;;
  down)
      if nmcli connection show --active | grep "$VPN_NAME"; then
        nmcli connection down id "$VPN_NAME"
      fi
    ;;
esac


# Also to enable dispatcher, run:
# sudo systemctl enable NetworkManager-dispatcher.service
2) Made it an executable by running this command

Code: Select all

 sudo chmod +x up-vpn.sh
3) And then finally I run this command ?

Code: Select all

 sudo systemctl enable NetworkManager-dispatcher.service
What am I missing to have this start automatically; does something not need to be set up to call it ?

parkerc
Posts: 22
Joined: 2020-02-22 09:19

Re: Automatically start vpn on boot up ?

#15 Post by parkerc »

Also I had tried to place the up-vpn.sh file into /etc/NetworkManager/dispatcher.d/ but it said I couldn’t as I didn’t have permission or something?

Chrisdb
Posts: 279
Joined: 2018-04-10 07:16

Re: Automatically start vpn on boot up ?

#16 Post by Chrisdb »

parkerc wrote:Also I had tried to place the up-vpn.sh file into /etc/NetworkManager/dispatcher.d/ but it said I couldn’t as I didn’t have permission or something?
The script should indeed be located at:

Code: Select all

etc/NetworkManager/dispatcher.d
Did you try with sudo?
What's the exact command and error you got?

parkerc
Posts: 22
Joined: 2020-02-22 09:19

Re: Automatically start vpn on boot up ?

#17 Post by parkerc »

I was using the UI and right click ‘copy to’ command option to move it over into that folder, but it wouldn’t let me

The message is along the lines that I do not have the permissions necessary to save the file there .

How do you ‘sudo’ via the UI ?

Chrisdb
Posts: 279
Joined: 2018-04-10 07:16

Re: Automatically start vpn on boot up ?

#18 Post by Chrisdb »

What desktop environment are you using? Gnome, xfce, ...

Anyway, the easiest way is to use the terminal and copy the file with 'sudo'.

parkerc
Posts: 22
Joined: 2020-02-22 09:19

Re: Automatically start vpn on boot up ?

#19 Post by parkerc »

I’m just using the standard (out of the box) desktop, sorry not sure what that is called..

I have now managed to copy the file over via the command line using..

Code: Select all

 sudo cp /home/me/Documents/up-vpn.sh /etc/NetworkManager/dispatcher.d/
However after a few reboots, it’s still not auto firing the VPN?

Any ideas? Is there anything else I need to do ?
Last edited by parkerc on 2020-03-01 10:21, edited 1 time in total.

Chrisdb
Posts: 279
Joined: 2018-04-10 07:16

Re: Automatically start vpn on boot up ?

#20 Post by Chrisdb »

What is the output of:

Code: Select all

ls -l /etc/NetworkManager/dispatcher.d/up-vpn.sh
And:

Code: Select all

systemctl status NetworkManager-dispatcher.service
What does the following command say if you run it from the terminal:

Code: Select all

sudo nmcli connection up id nl2-nodecentral-udp-udp

Post Reply