How do I auto-run a script when I change User?

theGreaterDuck

New Member
Credits
109
Mint 20.04, Ryzen 5-4500:

Hi, I am developing (Lazarus-FPC) some bespoke internal+external email-software for my Son's business and I need to isolate it from the Internet while testing. I have created a Standard-User "Test" to work there and I need it to crash at "no Internet" to handle errors etc.

Currently I am manually turning all-Network off/on, but that gets a little tedious as I have to do other daily-work on the Admin side many times a day.

No matter what I set within User Test, unless it is all-off, it still gets access to the Network and Internet.

Can someone please point me at, or help with a script that will auto-run when I TTY to Test (TTY2) that will disable the Network?

Then, when I TTY back to Admin (TTY1) another script auto-runs to reinstate the Network for my normal use?

Thanks
 


Condobloke

Well-Known Member
Credits
14,792

KGIII

Super Moderator
Staff member
Gold Supporter
Credits
25,737
Take a look at this answer:

 

f33dm3bits

Gold Member
Gold Supporter
Credits
24,076
You could write a script that checks which checks which TTY you are using and depending on which TTY number it gets back that it executes a certain command. Just something quick, haven't tried it but just off the top of my head.
Bash:
if [[  $XDG_VTNR = 2 ]] && [[ $UID = "testuser" ]]
then
    echo "Disabling networking"
    nmcli connection down eth0

elif [[ $XDG_VTNR = 1 ]] && [[ $UID = "f33dm3bits" ]]
then
   echo "Enabling networking"
   nmcli connection up eth0
fi
Then add to the ~/.bashrc or ~/.bash_profile of both of your users(test user and normal user) and it will be executed each time you login, if this doesn't work you will at least have something to help you get started. When you are talking about TTY I assume you are using ctrl+alt+f{1-12} to access the console to then login as the one of the two users.

If not and your are logging into the system using the display manager than you could just create a file with the contents of what I posted ealier to /usr/local/bin/checkusr
Bash:
#!/bin/bash

if [[  $XDG_VTNR = 2 ]] && [[ $UID = "testuser" ]]
then
    echo "Disabling networking"
    nmcli connection down eth0

elif [[ $XDG_VTNR = 1 ]] && [[ $UID = "f33dm3bits" ]]
then
   echo "Enabling networking"
   nmcli connection up eth0
fi
Make it executable(chmod +x /usr/local/bin/checkusr) and then you could add the command to your startup applications so that it is launched when you login, that would would then look like this.
Code:
/usr/local/bin/checkusr &
 
Last edited:

stan

Well-Known Member
Credits
7,319
I don't understand how that offers help.
@theGreaterDuck: What @Condobloke did by using the @ symbol with those users is call on them to come and help you. The forum software sends a notification that "you have been mentioned in a post" when this is done. And this was soon followed by 2 of the 3 people called upon arriving and offering you advice. People here are in time zones all over the world... be patient while waiting on replies.

If your edited "p.s." non-Einstein quote is meant as a disrespectful commentary... you may find people will stop helping you. Your lack of understanding should not give you cause to be insulting.
 

wizardfromoz

Administrator
Staff member
Gold Supporter
Credits
17,696
Agreed.

Moving this to Command Line, where scripting inquiries are also handled.

Chris Turner
wizardfromoz
 

Condobloke

Well-Known Member
Credits
14,792
@theGreaterDuck ....you silence confirms (perhaps) your comment was meant as disrespectful commentary.
Your continued absence sets it in concrete.

What say you ?
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Members online


Top