How do I add sudo commands to a keyboard shortcut?

7,542

You have three options. Pick the one of them which you like best:

  • Install the gksu package if not done already and use gksudo to get a GUI password entry dialog pop-up instead of being asked on the terminal console like sudo 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 of sudo. It works similar to gksudo 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's NOPASSWD 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:

    1. 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 and 2 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 the sudo when running it.

    2. 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 requires sudo)

    3. 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
      
    4. 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, replacing USERNAME 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.

    5. 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 configured NOPASSWD 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
      
    6. 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.

Share:
7,542

Related videos on Youtube

Jason
Author by

Jason

Updated on September 18, 2022

Comments

  • Jason
    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
      TheWanderer over 7 years
      It's most likely because it needs sudo. You could try gksudo.
  • Byte Commander
    Byte Commander over 7 years
    Strange. 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
    Jason over 7 years
    Yes, that gksudo gives me an error saying that 'c' is an invalid option.
  • Byte Commander
    Byte Commander over 7 years
    Ah, right. It believes that the -c after bash is meant as argument to gksudo. I fixed that.
  • Jason
    Jason over 7 years
    I 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 or pkexec. 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
    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
    Jason over 7 years
    First 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 the sudo command won't work as a shortcut.
  • Byte Commander
    Byte Commander over 7 years
    I added instructions on how to use the script and what exactly to bind to the shortcut. Is still anything unclear?
  • Jason
    Jason over 7 years
    The 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 the gksudo -- bash -c "echo..." and pkexec bash -c "echo..." commands in the keyboard shortcut, they prompt for password and then work.
  • Jason
    Jason over 7 years
    I 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
    Byte Commander over 7 years
    Just do as I instructed, save the little script I provided as /usr/local/bin/set-kbd-backlight and bind the commands set-kbd-backlight 0 and set-kbd-backlight 2 to your shortcuts. Leave the sudo su stuff away.
  • Jason
    Jason over 7 years
    That'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
    devklick over 3 years
    Thanks 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 called user, and I've added user 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 the user account without sudo. Any help would be appreciated.
  • devklick
    devklick over 3 years
    I 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!