3

I am working with Linux Mint 20 and would like to run a script with root privileges using a key shortcut. I would like to do so without having to enter my root password. For this purpose I have registered the script in /etc/sudoers.d.

/etc/sudoers.d/myscript

username ALL=(ALL) NOPASSWD: /home/xoric/scripts/myscript.sh

For testing purposes, this script simply creates two folders. The first one requires root privileges, the second one does not.

/home/xoric/scripts/myscript.sh

#!/bin/bash
mkdir /etc/test1          #requires root
mkdir /home/xoric/test2   #does not require root

Next, I create a custom key shortcut that runs this script. I register the key combination Ctrl+Alt+W with it.


First attempt

/Command: /home/xoric/scripts/myscript.sh

This is just for testing purposes. When pressing Ctrl+Alt+W, the second folder is created. The first folder (which requires root privileges) is not created. This is to be expected since I run the script without root privileges.


Second attempt

Command: sudo /home/xoric/scripts/myscript.sh

When pressing Ctrl+Alt+W, nothing happens. None of the two folders is created, i.e. the script is not executed. My expectation was that the script would run with root privileges and without password prompt (since it is registered in sudoers.d). However, this is not the case.


Third attempt

Command: gnome-terminal -e "sudo /home/xoric/scripts/myscript.sh"

When pressing Ctrl+Alt+W, a terminal opens and asks me to enter my root password. When I do so, the script is executed and both folders are created. This is what I want to achieve, but without entering the password.


Does anybody with a better knowledge of Linux (Mint) know why the second attempt does not work? Does Linux Mint not allow key shortcuts with root privileges? Or did I make a mistake when registering my script in sudoers.d? Many thanks in advance.

1
  • Try adding sudo inside your script. sudo mkdir /etc/test1
    – annahri
    Nov 11, 2020 at 12:22

1 Answer 1

3

With the help of a friend I finally found the problem. My entry in sudoers.d is broken. It literally contains the string "username" instead of my actual username "xoric" - a classic copy & paste error.

After I corrected the entry, the second attempt works fine: Pressing Ctrl+Alt+W now creates both folders (script runs with root privileges without a password prompt).

2
  • hey xoric, i hope you don't mind that i point out a security hole in your setup. if you left your terminal logged in and unattended, i could escalate to root and create a user for me to use later. the problem is that your sudoers entry points to a file you can modify without being root. that means your account can effectively execute any root command without password, not just your intended script. you might as well tell sudo not to ask for a password on your account at all.
    – Lanchon
    Sep 20, 2022 at 15:45
  • the solution is of course limiting write access to the script, typically by changing its owner to root. but it is kind of strange (and potentially risky if you forget about it) to have root-owned files in your home; it would be better to place it outside. a good location might be /opt/scripts/myscript. since this is world-accessible, try making the script useful to all users. or if your script is way too personal or contains secrets, you could place it in your home maybe in ~/bin/root/myscript or ~/opt/root/myscript and making the whole 'root' dir owned by root.
    – Lanchon
    Sep 20, 2022 at 15:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .