2

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 '20 at 12:22
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.