2

Since my Asus VivoBook keyboard shortcut to change de keyboard lightning level is not working, I made a bash script and configured it to be activated when I press a combination of keys. This is my script:

level=$(cat /sys/class/leds/asus\:\:kbd_backlight/brightness)

[[ $level = 3 ]] && echo 0 | sudo tee /sys/class/leds/asus\:\:kbd_backlight/brightness || echo $(($level+1)) | sudo tee /sys/class/leds/asus\:\:kbd_backlight/brightness

The script is working when I run it on terminal, but when I press the keys, it doesn't, I think because of the sudo permission that requires a password. Is there any way I can change the script permissions so that when I run it in the background it doesn't request for the password?

2
  • 1
    Dear @EduardoTrápani, my question is about not needing to type password when running sudo in a specific script, not every time I write it on the terminal. Dec 5, 2019 at 18:10
  • Does this answer you question?
    – MadMike
    Dec 10, 2019 at 11:17

1 Answer 1

1

There may be several options to do so, but I am suggesting you three options:

One way is to use sudo -i instead of sudo The command is run with an environment similar to the one a user would receive at log in.

The other way is to use sudo -v instead of sudo which uses cached credentials.

The third option is to configure sudo to never ask you for a password. The visudo command edits the sudoers file, which is used by the sudo command. To change what users and groups are allowed to run sudo, run visudo

sudo visudo

In the bottom of the file, add the following line:

<username> ALL=(ALL) NOPASSWD: ALL

Replace the with your user name. Example, I will add the line

tejas ALL=(ALL) NOPASSWD: ALL 

Once this line is added, you can type sudo in a Terminal window without being prompted for the password.

Note that this solution applies only while to using the sudo command in the terminal.

To restrict the location in which the script can be excuted without password prompt, use the folllowing command:

<username> ALL=NOPASSWD:/bin/bash /path/to/script/*

Strict note: use this solution only for the scripts which you can trust.

5
  • The two first options don't work. With the first one the behaviour is the save: it asks for the password as normal. Running the second I get usage: sudo -h | -K | -k | -V usage: sudo -v [-AknS] [-g group] [-h host] [-p prompt] [-u user] usage: sudo -l [-AknS] [-g group] [-h host] [-p prompt] [-U user] [-u user] [command] usage: sudo [-AbEHknPS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p prompt] [-T timeout] [-u user] [VAR=value] [-i|-s] [<command>] usage: sudo -e [-AknS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p prompt] [-T timeout] [-u user] file ... Dec 5, 2019 at 18:02
  • Editting the sudoers works, but now when I run sudo, no matter with which app, it doesn't ask me for password either. Is there any way to write on sudoers that only my file /home/goncalo/scripts/keyboardLightning doesn't need password to run? Dec 5, 2019 at 18:06
  • No. I want to configure the script to execute every time I press a keyboard shortcut (FN+F12). It started working when I changed the sudoers file. The problem is that when I use sudo on terminal it doesn't ask for the passwork anymore. Dec 5, 2019 at 18:12
  • use this goncalo ALL=NOPASSWD:/bin/bash /home/goncalo/scripts/keyboardLightning Dec 5, 2019 at 18:27
  • When I changed goncalo ALL=(ALL) NOPASSWD: ALL on sudoers it stops working and when I run it on terminal it has the original behavior: asks for my password. Dec 5, 2019 at 18:43

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