
Recently I have purchased a new MacBook Pro 15″ and the first thing I do was install Ubuntu Lucid Lynx on it. After some readying I have all the hardware working properly but the keyboard backlight wasn’t integrated into system and unfortunately after searching on the web for something that could do this, I decide to write a simple script with Bash and use Ubuntu-tweak to bind the keyboard keys to invoke the script. So let’s go…
Preparing our system
Well, before try to install my script on your computer you must have installed the applesmc module on your computer. Don’t worry, if you go to help.ubuntu.com you’ll find specific instructions to install it.
Installing my script at your system
Just right-click on this link, save to your computer and rename it to keyboard-backlight. You can drop it at /usr/bin to execute from wherever you are. Here are the contents of the script If you are curious:
BACKLIGHT=$(cat /sys/class/leds/smc::kbd_backlight/brightness)
INCREMENT=15
if [ $UID -ne 0 ]; then
echo "Please run this program as superuser"
exit 1
fi
SET_VALUE=0
case $1 in
up)
TOTAL=`expr $BACKLIGHT + $INCREMENT`
if [ $TOTAL -gt "255" ]; then
exit 1
fi
SET_VALUE=1
;;
down)
TOTAL=`expr $BACKLIGHT - $INCREMENT`
if [ $TOTAL -lt "0" ]; then
exit 1
fi
SET_VALUE=1
;;
total)
TEMP_VALUE=$BACKLIGHT
while [ $TEMP_VALUE -lt "255" ]; do
TEMP_VALUE=`expr $TEMP_VALUE + 1`
if [ $TEMP_VALUE -gt "255" ]; then TEMP_VALUE=255; fi
echo $TEMP_VALUE > /sys/class/leds/smc::kbd_backlight/brightness
done
;;
off)
TEMP_VALUE=$BACKLIGHT
while [ $TEMP_VALUE -gt "0" ]; do
TEMP_VALUE=`expr $TEMP_VALUE - 1`
if [ $TEMP_VALUE -lt "0" ]; then TEMP_VALUE=0; fi
echo $TEMP_VALUE > /sys/class/leds/smc::kbd_backlight/brightness
done
;;
*)
echo "Use: keyboard-light up|down|total|off"
;;
esac
if [ $SET_VALUE -eq "1" ]; then
echo $TOTAL > /sys/class/leds/smc::kbd_backlight/brightness
fiUsing keyboard-backlight from command-line
This script could have 4 different uses:
- Increase backlight keyboard: sudo keyboard-backlight up
- Decrease backlight keyboard: sudo keyboard-backlight down
- Increase to total value of keyboard backlight: sudo keyboard-backlight total
- Turn off backlight keyboard: sudo keyboard-backlight off
You can customize the amount of steps by changing the INCREMENT variable inside keyboard-backlight file.
Using it without superuser password
As some advanced user could see, for get this to work we have to run it as superuser (cause we are writing configurations in /sys/).
This impedes us to execute it without being prompted for the superuser password so I have made another workaround. You can allow to admin group’s users to run it via sudo without password prompt. To do this you must add the next lines to the sudoers file:
Cmnd_Alias CMDS = /path/to/your/script/keyboard-backlight
%admin ALL = (ALL) NOPASSWD: CMDS
Binding keyboard keys to this script
Now that the script works from command line without password prompting we could bind keyboard keys to this script. I’ll use Ubuntu-Tweak but you can use “compiz-manager” or gconf directly.
In ubuntu-tweak we can tune our keyboard special keys under Personal > Shortcuts Commands, there just set up shortcuts as you can see in the image below.
I hope you enjoy it! And as I say always “Comments, questions, heckles, attacks, praises, and, (most especially) patches and contributions are welcome!”

Hi,
I just found your script via google and I really like it. I was searching how I could get the special charakters on my MBP’s keyboard like “\” to work in ubuntu. Haven’t found a solution yet, but I have a great idea to improve your script: Add a GUI via zenity and make a scale to adjust the backlight.
Write on September 11th, 2010 at 7:00 pm
Your wellcome!
To do exactly what yo want you can use the GUI script available from https://help.ubuntu.com/community/MacBookPro6-2/Lucid#Keyboard backlight without pommed
Regards
Write on September 19th, 2010 at 7:34 pm
Thanks for sharing this link, but unfortunately it seems to be offline… Does anybody have a mirror or another source? Please reply to my post if you do!
I would appreciate if a staff member here at http://www.mabishu.com could post it.
Thanks,
John
Write on September 20th, 2010 at 5:32 pm
Thanks for sharing the link, but unfortunately it seems to be offline… Does anybody have a mirror or another source? Please answer to my post if you do!
I would appreciate if a staff member here at http://www.mabishu.com could post it.
Thanks,
William
Write on September 28th, 2010 at 6:55 pm
the script works on my custom gentoo installation.
Write on October 17th, 2010 at 12:12 pm
Thanks for the script, really nice!
I’ve added a ‘value’ function, to set the brightness to a specified value. I’m using it in my rc.local to get a fixed brightness when starting the machine (MacBookPro 7.1 with Ubuntu 10.10).
#!/bin/bash
# Francisco Diéguez Souto (frandieguez@ubuntu.com)
# This script is licensed under MIT License.
# http://www.mabishu.com/blog/2010/06/24/macbook-pro-keyboard-backlight-keys-on-ubuntu-gnulinux/
#
# 2010-10-21 tomas@comsolvia.se added ‘value’ function and $USAGE variable
#
# This program just modifies the value of backlight keyboard for Apple Laptops
# You must run it as root user or via sudo.
# As a shortcut you could allow to admin users to run via sudo without password
# prompt. To do this you must add sudoers file the next contents:
#
# Cmnd_Alias CMDS = /usr/local/bin/keyboard-backlight
# %admin ALL = (ALL) NOPASSWD: CMDS
#
# After this you can use this script as follows:
#
# Increase backlight keyboard:
# $ sudo keyboard-backlight up
# Decrease backlight keyboard:
# $ sudo keyboard-backlight down
# Increase to total value backlight keyboard:
# $ sudo keyboard-backlight total
# Turn off backlight keyboard:
# $ sudo keyboard-backlight off
# Set backlight level to given value (0-255)
# $ sudo keyboard-backlight value 100
#
# You can customize the amount of backlight by step by changing the INCREMENT
# variable as you want it.
BACKLIGHT=$(cat /sys/class/leds/smc::kbd_backlight/brightness)
INCREMENT=15
USAGE=”Use: keyboard-light up|down|total|off|value x (0-255)”
if [ $UID -ne 0 ]; then
echo “Please run this program as superuser”
exit 1
fi
SET_VALUE=0
case $1 in
up)
TOTAL=`expr $BACKLIGHT + $INCREMENT`
if [ $TOTAL -gt "255" ]; then
exit 1
fi
SET_VALUE=1
;;
down)
TOTAL=`expr $BACKLIGHT – $INCREMENT`
if [ $TOTAL -lt "0" ]; then
exit 1
fi
SET_VALUE=1
;;
total)
TEMP_VALUE=$BACKLIGHT
while [ $TEMP_VALUE -lt "255" ]; do
TEMP_VALUE=`expr $TEMP_VALUE + 1`
if [ $TEMP_VALUE -gt "255" ]; then TEMP_VALUE=255; fi
echo $TEMP_VALUE > /sys/class/leds/smc::kbd_backlight/brightness
done
;;
off)
TEMP_VALUE=$BACKLIGHT
while [ $TEMP_VALUE -gt "0" ]; do
TEMP_VALUE=`expr $TEMP_VALUE – 1`
if [ $TEMP_VALUE -lt "0" ]; then TEMP_VALUE=0; fi
echo $TEMP_VALUE > /sys/class/leds/smc::kbd_backlight/brightness
done
;;
value)
if [ ! -n "$2" ]; then # no value given
echo $USAGE
exit 1
fi
if [ -z "${2//[0-9]}” ]; then # Only accept numeric values
echo $2 > /sys/class/leds/smc::kbd_backlight/brightness
else
echo $USAGE
exit 1
fi
;;
*)
echo $USAGE
;;
esac
if [ $SET_VALUE -eq "1" ]; then
echo $TOTAL > /sys/class/leds/smc::kbd_backlight/brightness
fi
Write on October 21st, 2010 at 7:42 am
Thanks for the improvements! I’ll share this version in the post
Write on October 24th, 2010 at 11:33 pm
How do I execute the script?
I put the keyboard-backlight.sh file in /usr/bin
and then when I try to run a command to modify the backlight state, it says that the command does not exist.
Write on November 20th, 2010 at 9:50 am
Hey, thanks a lot for the script. I’m using some of the info in it to come up with my own custom version. Very helpful, thanks so much!
Write on January 4th, 2011 at 8:06 pm
@gramasaurous sorry for the delay, I didn’t get realized that you post a comment here.
Well, you must give executable rights to the script
chmod a+x /usr/bin/keyboard-backlight.sh
should fix it.
Cheers
Write on January 9th, 2011 at 10:30 pm
[...] to work turns out to be a relatively simple. Initially, I’d just followed the instructions here to get the keyboard LED brightness working, and that did a decent job…. but not perfect. It [...]
Write on March 16th, 2011 at 12:21 am
anyone knows how to do this for windows 7?
i have broken my brightness key and now i cant see a thing…
please help me!!!!!!!!!!!!!!!!!!!!!!!!!!!
Write on June 1st, 2011 at 8:55 am
intresting .. thanks for the work on the script.. i am trying to figure out how to stop the keyboard backlight from timing out. it can stay on all the time in OS X – but times out after about a minute under Kubuntu Lucid. HHhhmmm…
Write on December 4th, 2011 at 3:24 am