10

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.

1
  • It's most likely because it needs sudo. You could try gksudo. – TheWanderer Sep 7 '16 at 2:20
9

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.

14
  • Turns out the gksu package was already installed, but when I bound that command to the key it didn't pull up any window at all. I tried the 'pkexec' command and that pulled up a GUI window prompting for a password. With that command the keys work fine, but it is interesting that it is backwards like that. Thanks for the help – Jason Sep 7 '16 at 16:26
  • 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. – Byte Commander Sep 7 '16 at 19:41
  • Yes, that gksudo gives me an error saying that 'c' is an invalid option. – Jason Sep 7 '16 at 22:33
  • 1
    @Jason I added a detailed explanation on how to make a script for your needs and configure it for NOPASSWD sudo. – Byte Commander Sep 8 '16 at 12:47
  • 1
    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! – Klicker Aug 26 '20 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.