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
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
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
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.
sudo
inside your script.sudo mkdir /etc/test1