How do I add sudo commands to a keyboard shortcut?

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.


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.