[Solved] How to make 'dm-tool lock' activate in a script?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
Midnight True
Level 7
Level 7
Posts: 1549
Joined: Wed Jul 20, 2022 3:23 am
Location: Southern and Southwestern area of Mato
Contact:

[Solved] How to make 'dm-tool lock' activate in a script?

Post by Midnight True »

I wrote a script for a keyboard shortcut that will:
1. Turn off networking
2. Turn off keyboard backlight
3. Lock my session

Code: Select all

#!/bin/bash

nmcli networking off
echo 0 | sudo tee /sys/class/leds/asus::kbd_backlight/brightness
dm-tool lock
exit
using this keyboard shortcut command

Code: Select all

sudo /usr/bin/0locking.sh
after making adjustment on /etc/sudoers it works except dm-tool lock
as well as light-locker-command -l and light-locker-command -a

Any ideas?
Thank you

Edit: Adding neofetch output

Code: Select all

             ...-:::::-...                  
          .-MMMMMMMMMMMMMMM-.              ------------------ 
      .-MMMM`..-:::::::-..`MMMM-.          OS: Linux Mint 21.1 x86_64 
    .:MMMM.:MMMMMMMMMMMMMMM:.MMMM:.        Host: VivoBook_ASUSLaptop X415JA_X415JA 1.0 
   -MMM-M---MMMMMMMMMMMMMMMMMMM.MMM-       Kernel: 5.15.0-60-generic 
 `:MMM:MM`  :MMMM:....::-...-MMMM:MMM:`    Uptime: 2 days, 12 hours, 5 mins 
 :MMM:MMM`  :MM:`  ``    ``  `:MMM:MMM:    Packages: 2226 (dpkg), 38 (flatpak) 
.MMM.MMMM`  :MM.  -MM.  .MM-  `MMMM.MMM.   Shell: bash 5.1.16 
:MMM:MMMM`  :MM.  -MM-  .MM:  `MMMM-MMM:   Resolution: 1366x768 
:MMM:MMMM`  :MM.  -MM-  .MM:  `MMMM:MMM:   DE: Xfce 4.16 
:MMM:MMMM`  :MM.  -MM-  .MM:  `MMMM-MMM:   WM: Xfwm4 
.MMM.MMMM`  :MM:--:MM:--:MM:  `MMMM.MMM.   WM Theme: Midnight-GreenNight 
 :MMM:MMM-  `-MMMMMMMMMMMM-`  -MMM-MMM:    Theme: Midnight-GreenNight [GTK2/3] 
  :MMM:MMM:`                `:MMM:MMM:     Icons: Mint-X-Dark [GTK2/3] 
   .MMM.MMMM:--------------:MMMM.MMM.      Terminal: xfce4-terminal 
     '-MMMM.-MMMMMMMMMMMMMMM-.MMMM-'       Terminal Font: Monospace 15 
       '.-MMMM``--:::::--``MMMM-.'         CPU: Intel i3-1005G1 (4) @ 3.400GHz 
            '-MMMMMMMMMMMMM-'              GPU: Intel Iris Plus Graphics G1 
               ``-:::::-``                 Memory: 1584MiB / 7692MiB 
Last edited by LockBot on Fri Sep 15, 2023 10:00 pm, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: How to make 'dm-tool lock' activate in a script?

Post by rene »

Code: Select all

rene@hp8k:~$ sudo dm-tool lock
Not running inside a display manager, XDG_SEAT_PATH not defined
I.e., when you run things through sudo you run them as user "root" and not as your own user, that is, as the user that needs its session in fact locked.

That's to say, by ways of you invoking /usr/bin/0locking.sh -- also consider placing it in /usr/local/bin/ instead -- through sudo you are invoking dm-lock through sudo whereas you should simply invoke it as your user. I take it you want to for the brightness thing (although you already sudo around that anyway) but the "right answer" is still to invoke the script simply as yourself, and then only arrange through /etc/sudoers.d to be allowed to tee into /sys for that issue.

If you insist you can as per the manpage also scrounge around dm-tool list-seats and specify the seat explicitly, i.e.,

Code: Select all

rene@hp8k:~$ dm-tool list-seats
Seat0
  CanSwitch=true
  HasGuestAccount=false
  Session0
    UserName='rene'
rene@hp8k:~$ sudo XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0 dm-tool lock
but don't do that.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: How to make 'dm-tool lock' activate in a script?

Post by rene »

Mmm. The above is the right answer, but if you insist on a shortcut you can also use sudo -E to invoke the script while preserving the user environment (and just kill the sudo from the tee; you're already sudoed after all).
User avatar
Midnight True
Level 7
Level 7
Posts: 1549
Joined: Wed Jul 20, 2022 3:23 am
Location: Southern and Southwestern area of Mato
Contact:

Re: How to make 'dm-tool lock' activate in a script?

Post by Midnight True »

rene wrote: Wed Mar 15, 2023 2:34 pm I.e., when you run things through sudo you run them as user "root" and not as your own user, that is, as the user that needs its session in fact locked.
Thank you for the explanation. No wonder why even xflock4 don't works. Too bad all the online articles never said anything about this 😅
rene wrote: Wed Mar 15, 2023 2:34 pm also consider placing it in /usr/local/bin/ instead
Thank you and i did it as you had suggested. But for some reason the shortcut do not work anymore 😞
keyboard shortcut =

Code: Select all

sudo -E /usr/local/bin/0locking.sh

Code: Select all

milla@DiggieIsBack:/usr/local/bin$ ls
0locking.sh    auto-cpufreq-install     gnome-help
0unlocking.sh  auto-cpufreq-remove      highlight-mint
apt            bat                      search
auto-cpufreq   cpufreqctl.auto-cpufreq  yelp
milla@DiggieIsBack:/usr/local/bin$ ls -l
total 2652
-rwxr-xr-x 1 milla milla     148 Mar 16 07:52 0locking.sh
-rwxr-xr-x 1 milla milla     103 Mar 14 15:14 0unlocking.sh
-rwxr-xr-x 1 root  root     7692 Nov 22  2021 apt
-rwxr-xr-x 1 root  root      646 Jan 21 11:51 auto-cpufreq
-rwxr-xr-x 1 root  root     3314 Jan 21 11:52 auto-cpufreq-install
-rwxr-xr-x 1 root  root     2431 Jan 21 11:52 auto-cpufreq-remove
-rwxr-xr-x 1 root  root  2658304 Mar  1 11:47 bat
-rwxr-xr-x 1 root  root    10671 Jan 21 11:52 cpufreqctl.auto-cpufreq
-rwxr-xr-x 1 root  root      536 Apr 26  2022 gnome-help
-rwxr-xr-x 1 root  root      196 Feb  7  2019 highlight-mint
-rwxr-xr-x 1 root  root      964 May 15  2019 search
-rwxr-xr-x 1 root  root      553 Apr 26  2022 yelp

i also edited /etc/sudoers

Code: Select all

# See sudoers(5) for more information on "@include" directives:

@includedir /etc/sudoers.d

milla ALL=(ALL) NOPASSWD: /usr/local/bin/0unlocking.sh, /usr/local/bin/0locking.sh

and my script

Code: Select all

milla@DiggieIsBack:~$ cat /usr/local/bin/0locking.sh
#!/bin/bash

nmcli networking off
echo 0 | tee /sys/class/leds/asus::kbd_backlight/brightness
dm-tool lock  #xflock4 #add back if fix is found
exit

rene wrote: Wed Mar 15, 2023 2:34 pm but don't do that.
may i know why?

Moreover, on /usr/bin a file titled 0locking.sh.save spawned, i have no idea why it was there now. Is this safe to remove?

Thank you so much again 🙏 i learned a lot

Edit note: Rectified typo
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: How to make 'dm-tool lock' activate in a script?

Post by rene »

Midnight True wrote: Wed Mar 15, 2023 8:19 pm But for some reason the shortcut do not work anymore 😞
Lovely. Was/am not on Mint but Ubuntu/Mint has then probably/seemingly configured sudo to out of the box not allow preserving environment. I expect that if you run sudo -E /usr/local/bin/0locking.sh directly from the command line you are told "nah, secuwity.". If indeed it should work to adjust the /etc/sudoers line to

Code: Select all

milla ALL=(ALL) NOPASSWD:SETENV: /usr/local/bin/0unlocking.sh, /usr/local/bin/0locking.sh
I assume that'll have things going, and it's fine so you may want to ignore the below in a practical sense, but also as to answering the "may I know why?" question:

When you run things through sudo you as said run things as user root whereas at least for the dm-tool call the idea is to run it as your own user. In this case the dm-tool manpage in fact (sort of) explicitly comments that it's fine as long as it has the correct XDG_SEAT_PATH defined but conceptually we'd like to not even need to know that; would rather just run dm-tool as ourselves.

You here need sudo (only) to be able to echo into /sys for the brightness things and it would therefore be better to...

1. Provide an executable /usr/local/bin/0brightness.sh with only that part,

Code: Select all

#!/bin/sh
echo 0 >/sys/class/leds/asus::kbd_backlight/brightness
2. Adjust your sudoers setup to allow passwordless sudo for that script instead,

Code: Select all

milla ALL=(ALL) NOPASSWD: /usr/local/bin/0brightness.sh
3. Invoke that script with sudo from your /usr/local/bin/0locking.sh

Code: Select all

#!/bin/sh
nmcli networking off
sudo 0brightness.sh
dm-tool lock
4. ... and then invoke 0locking.sh from the shortcut simply as yourself, i.e., without sudo.

The point of that all then is to only use sudo when/where in fact needed. Better conceptually, but, yes, feel free to in practice just do the sudo -E thing with the above adjustment to sudoers to in fact allow such.

... although now that you made me write that, do please use that simple > redirection as per 1 above for the echo into /sys: in the schema

Code: Select all

echo .. | sudo tee ...
that | sudo tee thing is there only to have a place to put a sudo; if you don't anymore need it due to invoking the script itself through sudo you can just use normal, simple redirection.

Good work showing back details by the way; why it didn't work with sudo -E in fact had me scratch head a bit...
User avatar
Midnight True
Level 7
Level 7
Posts: 1549
Joined: Wed Jul 20, 2022 3:23 am
Location: Southern and Southwestern area of Mato
Contact:

Re: How to make 'dm-tool lock' activate in a script?

Post by Midnight True »

rene wrote: Thu Mar 16, 2023 1:27 am
I follow your instructions and yeheyyyy its workinggg 🥳️🥳️🥳️

However, i am not confident enough to further modify /etc/sudoers so i just moved the script to /usr/local/sbin and they work.
Moreover, i just deleted 0locking.sh.save on /usr/binwith no ramification so far.

To sum up with @rene's help i was able to accomplish it:

1. Make a separate script just for turning off the brightness titled 0brightness.sh

Code: Select all

#!/bin/bash

echo 0 >/sys/class/leds/asus::kbd_backlight/brightness

2. Next modify the 0locking.sh

Code: Select all

#!/bin/bash

nmcli networking off
sudo /usr/local/sbin/0brightness.sh
dm-tool lock
exit
3. Adjust /etc/sudoers for the new and modified script

Code: Select all

# See sudoers(5) for more information on "@include" directives:

@includedir /etc/sudoers.d

milla ALL=(ALL) NOPASSWD: /usr/local/sbin/0unlocking.sh, /usr/local/sbin/0brightness.sh

4. Bind the script on a keyboard shortcut

Code: Select all

xfce4-terminal -e '/usr/local/sbin/0locking.sh'
(happiness 🥳️🍻🥳️🍻🥳️🍻)

for the sake of completeness
5. Create 0unlocking.sh to turn back on networking and keyboard backlight

Code: Select all

#!/bin/bash

nmcli networking on
echo 1 >/sys/class/leds/asus::kbd_backlight/brightness
exit
6. After unlocking session, bind the 0unlocking.sh to a keyboard shortcut

Code: Select all

sudo /usr/local/sbin/0unlocking.sh
Again, Thank you so much @rene 🍻🍻🍻 i learned a lot 🥳️🥳️🥳️
Locked

Return to “Scripts & Bash”