Automatically start vpn on boot up ?

If none of the more specific forums is the right place to ask

Automatically start vpn on boot up ?

Postby parkerc » 2020-02-22 09:47

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
parkerc
 
Posts: 22
Joined: 2020-02-22 09:19


Re: Automatically start vpn on boot up ?

Postby parkerc » 2020-02-22 12:08

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
parkerc
 
Posts: 22
Joined: 2020-02-22 09:19

Re: Automatically start vpn on boot up ?

Postby Chrisdb » 2020-02-22 17:54

are you connecting with ethernet or wirelesss...
What's the name of your configured VPN
Chrisdb
 
Posts: 273
Joined: 2018-04-10 07:16

Re: Automatically start vpn on boot up ?

Postby parkerc » 2020-02-22 20:05

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
parkerc
 
Posts: 22
Joined: 2020-02-22 09:19

Re: Automatically start vpn on boot up ?

Postby Chrisdb » 2020-02-23 07:09

Are you familiar with shell scripting...
If not, online are many tutorials to get you started.
Chrisdb
 
Posts: 273
Joined: 2018-04-10 07:16

Re: Automatically start vpn on boot up ?

Postby parkerc » 2020-02-23 08:45

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 :)
parkerc
 
Posts: 22
Joined: 2020-02-22 09:19

Re: Automatically start vpn on boot up ?

Postby Chrisdb » 2020-02-23 09:19

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
Chrisdb
 
Posts: 273
Joined: 2018-04-10 07:16

Re: Automatically start vpn on boot up ?

Postby parkerc » 2020-02-23 09:27

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 ?

Postby parkerc » 2020-02-23 09:40

Thinking about the associated Cron job

How frequently should something like this run?

* * * * * /etc/NetworkManager/dispatcher.d/vpn-up.sh
parkerc
 
Posts: 22
Joined: 2020-02-22 09:19

Re: Automatically start vpn on boot up ?

Postby Chrisdb » 2020-02-23 09:48

It shouldn't be scheduled because it is event triggered.
F.e. UP = when the interface has been activated.
Chrisdb
 
Posts: 273
Joined: 2018-04-10 07:16

Re: Automatically start vpn on boot up ?

Postby parkerc » 2020-02-23 10:04

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?
parkerc
 
Posts: 22
Joined: 2020-02-22 09:19

Re: Automatically start vpn on boot up ?

Postby Chrisdb » 2020-02-23 12:34

- 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
Chrisdb
 
Posts: 273
Joined: 2018-04-10 07:16

Re: Automatically start vpn on boot up ?

Postby parkerc » 2020-02-28 22:37

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 ?

Postby parkerc » 2020-02-29 12:17

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?
parkerc
 
Posts: 22
Joined: 2020-02-22 09:19

Next

Return to General Questions

Who is online

Users browsing this forum: No registered users and 13 guests

fashionable