How do I add sudo commands to a keyboard shortcut?
You have three options. Pick the one of them which you like best:
-
Install the
gksu
package if not done already and usegksudo
to get a GUI password entry dialog pop-up instead of being asked on the terminal console likesudo
does (which is not present when you run it as keyboard shortcut and therefore does not work).sudo apt install gksu
The command you need to bind to your shortcut would be this:
gksudo -- bash -c 'echo VALUE > /sys/devices/platform/sony-laptop/kbd_backlight'
-
Use
pkexec
instead ofsudo
. It works similar togksudo
and should be preinstalled, but it only works for terminal commands, if you wanted to run a GUI application as root with it, it needed special configuration. But for your command it would be sufficient.The command you need to bind to your shortcut would be this:
pkexec bash -c 'echo VALUE > /sys/devices/platform/sony-laptop/kbd_backlight'
-
Write a short script to change the brightness and then configure
sudo
'sNOPASSWD
option so that you can run it as root without getting asked for a password.Relevant question: How do I run specific sudo commands without a password?
Here's what you need to do in your specific case:
-
Write a script file that contains all commands necessary to achieve what you want which will then be run as root:
#!/bin/bash if test "$(id -u)" -ne 0 ; then sudo "$0" "$1" exit $? fi if test "$1" -gt 0 ; then echo "$1" > /sys/devices/platform/sony-laptop/kbd_backlight else echo "Invalid argument $1" exit 1 fi
The script above takes an integer number greater than zero as command-line argument and writes it to the backlight control file after performing a basic sanity check. You could also modify the check to only allow the values
0
and2
if you want, but I'll leave that up to you.Additionally, it checks as which user it is running and tries to elevate its privileges to root by executing itself with
sudo
if necessary. That means you can even omit thesudo
when running it. Save this script as
/usr/local/bin/set-kbd-backlight
(you may pick a different file name, but the directory should stay the same ; note that writing to this location requiressudo
)-
Make sure your script file has correct ownership and permission settings. You're going to be able to run this script as root without password, so we must make sure it can be executed by everyone, but not be edited by non-root users!
sudo chown root:root /usr/local/bin/set-kbd-backlight sudo chmod 755 /usr/local/bin/set-kbd-backlight
-
Now you can configure
sudo
to allow running this script as root without password:Edit your
sudo
configuration file/etc/sudoers
by running the command below (not any other way!):sudo visudo
Now append the following line right before the line containing
includedir /etc/sudoers.d
near the end of the file, replacingUSERNAME
with the correct username which you will grant the passwordless execution as root:USERNAME ALL=(root) NOPASSWD: /usr/local/bin/set-kbd-backlight
Exit the
visudo
editor by pressing Ctrl+X, then Y (or whatever key represents "Yes" in your locale) and then Enter. -
Verify it is working by first running
sudo -k
to revoke your cached password, just in case you entered it within the last 15 minutes in the current shell session. This is not needed for the script to work, it is just to test whether you configuredNOPASSWD
correctly.Then run the commands below to turn the backlight on and off, without
sudo
. You should not be requested for your password.set-kbd-backlight 0 set-kbd-backlight 2
Bind the commands (
set-kbd-backlight 0
to switch the light off,set-kbd-backlight 2
to switch it on) to the respective keyboard shortcuts.
-
Related videos on Youtube
Jason
Updated on September 18, 2022Comments
-
Jason over 1 year
I am running Ubuntu 16.04 on a Sony Vaio laptop, which features a keyboard backlight. I can enter this command into the terminal, putting a "0" in place of
*value*
to turn the backlight off, and a "2" to turn it on.sudo su -c "echo *value* > /sys/devices/platform/sony-laptop/kbd_backlight"
When I went to the settings app to create a custom keyboard shortcut and bind it to a simple ctrl keybinding, it doesn't work.
-
TheWanderer over 7 yearsIt's most likely because it needs
sudo
. You could trygksudo
.
-
-
Byte Commander over 7 yearsStrange. What happens if you run the
gksudo
command from inside a terminal? Does it work there or do you get any error message? However, as the other option solved your problem, you might consider accepting this answer by clicking the check button on its left. Thanks. -
Jason over 7 yearsYes, that gksudo gives me an error saying that 'c' is an invalid option.
-
Byte Commander over 7 yearsAh, right. It believes that the
-c
afterbash
is meant as argument togksudo
. I fixed that. -
Jason over 7 yearsI have tried reading the link you provided and looked at other posts but I still can't figure out how to get the keyboard shortcut to work without a password. I get syntax errors when I try to add a tmp file to visudo, but I'm not sure I'm doing it right. I wondering how to do it for either the
gksudo
orpkexec
. They both pop up a GUI window prompting for password when I use the keyboard. How do I add exceptions for those commands? -
Byte Commander over 7 years@Jason I added a detailed explanation on how to make a script for your needs and configure it for NOPASSWD sudo.
-
Jason over 7 yearsFirst off, thank you for all of the details because I had been poking around for hours and couldn't do anything. So after giving the proper permissions and following the steps you laid out, the original
sudo
command I posted will not work as a keyboard shortcut. I used that command in the terminal and it worked as expected without needing a password. I am baffled by the fact that thesudo
command won't work as a shortcut. -
Byte Commander over 7 yearsI added instructions on how to use the script and what exactly to bind to the shortcut. Is still anything unclear?
-
Jason over 7 yearsThe instructions you provided for the password settings were straightforward and they work. The only thing that isn't working for me are the shortcuts. When I enter the command
sudo su -c "echo VALUE > /sys/devices/platform/sony-laptop/kbd_backlight"
into the shortcut and press the keybinding, nothing happens. I checked the file where that value is stored and nothing changed. When I run this command in the terminal, it works fine with no password. Both thegksudo -- bash -c "echo..."
andpkexec bash -c "echo..."
commands in the keyboard shortcut, they prompt for password and then work. -
Jason over 7 yearsI greatly appreciate your help and I think I will have to deal with typing a password for now until I can figure out what else could be causing the
sudo
command to not work with a keybinding, only in the terminal. -
Byte Commander over 7 yearsJust do as I instructed, save the little script I provided as
/usr/local/bin/set-kbd-backlight
and bind the commandsset-kbd-backlight 0
andset-kbd-backlight 2
to your shortcuts. Leave thesudo su
stuff away. -
Jason over 7 yearsThat's where I was going wrong. I was trying to vary
sudo
to try to get it to work. Thanks a lot I apologize for my incapablility but this helped me understand it a lot. Cheers. -
devklick over 3 yearsThanks for such a detailed answer, but I'm finding that I still dont have permission after editing the sudo config via
visudo
. My user is calleduser
, and I've addeduser ALL = (root) NOPASSWD: /usr/local/bin/kbd-bklt-toggle
(this is my script path) on the line before#includedir /etc/sudoers.d
but I stil get permission denied when running under theuser
account withoutsudo
. Any help would be appreciated. -
devklick over 3 yearsI found that that I had to include
sudo
in the command when binding to keyboard shortcut. Im guessing your example script does this (i dont understand it completely)). This was a great answer, thanks!