1

Running Debian 11 (Bullseye), I have a short BASH script that I'd like to run automatically when I log into my home PC. It's purpose is to start openvpn running as a daemon as long as my phone is connected and active as a network interface...

#!/bin/bash

until [[ -e "/sys/class/net/tun0" ]]; do
        if ethtool usb0 | grep -q "Link detected: yes"; then
        openvpn --log <myfile.log> --daemon --config <myfile.conf>; else
        sleep 15
        fi
done

I'd like to run this automatically upon logging in but I'm unsure how to best go about it. openvpn requires root privileges. From searching the internet the two methods that look most likely are to either place the path to the script inside /etc/rc.local, which, based on my very cursory knowledge of runlevels, will cause the script to be executed as root upon initialization of a multi-user runlevel. Otherwise I think I could edit the sudoers file to allow my user to run openvpn without requiring a password and then add it to the list of my DE's startup programs? or alternatively include it within my bash.rc?

I'd be really grateful for any guidance or critique of the script!

Edit: as @Gilles'SO-stopbeingevil' pointed out, it wasn't optimal to have the script run continuously upon login. A more targeted way is to run the script immediately upon the network interface being brought up. Since I'm using ifup, and per the ifup man page, I added a line directly after the usb0 interface configuration definition, beginning with "post-up" and followed by an argument of a command to be executed immediately after the interface is brought up. like so...

`This file describes the network interfaces available on your system
and how to activate them. For more information, see interfaces(5).

allow-hotplug usb0
iface usb0 inet dhcp
post-up bash /home/user/sh/vpnlauncher.sh`

The script above probably performs too much unnecessary validation when using this method but I'm leaving it as is.

Edit 2: unfortunately, while the solution above seemed to work perfectly yesterday, this morning it caused my system to create thousands of openvpn processes and cpu usage went through the roof; I had to hard reset the machine as it became unresponsive. I don't know why this should be but I'd advise anyone reading this not to use it

5
  • Please edit your question and tell us what operating system you are using.
    – terdon
    Commented Jan 24, 2022 at 15:25
  • to use a sudo command, for a fixed command run by sudo see unix.stackexchange.com/questions/395776/… (edited, didn't read carefully you were considering sudo)
    – Archemar
    Commented Jan 24, 2022 at 15:31
  • To run a script as root when a user logs in, use pam_exec in /etc/pam.d/<something>, possibly preceded by other lines to make the execution conditional to which user is logging in. Though I don't understand why you want to do this when the user logs in. Why not do it when the network interface comes up? (Which would depend on whether your system uses network-manager, ifup, systemd, …) Commented Jan 24, 2022 at 21:25
  • @Gilles'SO-stopbeingevil' Your right, if you read the edit to my original post, you'll see that your comment prompted me to find another solution via /etc/network/interfaces and the "post-up" keyword. Unfortunately, while that seemed to work yesterday, this morning it caused all kinds of instability, so I'm back to square one I'm afraid. Commented Jan 25, 2022 at 13:49
  • If only openvpn requires root privileges (ie. the rest of your script, especially ethtool can run without being root), then probably the easiest solution would be to set the openvpn executable setuid root (I wonder why it isn't already set so upon installation, if it requires root).
    – raj
    Commented Jan 25, 2022 at 16:53

0

You must log in to answer this question.

Browse other questions tagged .