HOWTO Control LED Lights

From LinuxReviews
Jump to navigationJump to search

The Linux kernel has had a user-controllable LED subsystem since version 4.17. This subsystem makes LED available and controllable led lights show up in the special /sys/class/leds/ folder. Sending commands to files in that folder can make led lights indicate everything from disk activity to battery status.

Kernel Configuration[edit]

The kernels shipped with common Linux distributions are configured with all the common LED drivers. The drivers can be selected in Device Drivers ▸ LED Support and the triggers for LED lights are in that pages LED Trigger support sub-menu.

You do not need to do anything special to be able to control kernel-supported LED lights unless you compile the kernel yourself OR you want to control LED lights that are not supported by the stock kernel (in which case you need to compile modules yourself).

Inspecting Your LED Lights[edit]

A simple ls /sys/class/leds/ will show you what LED controls are available. That command may show something like

input3::capslock input3::numlock input3::scrolllock mmc0:: phy0-led

Those will be symbolic links to folders with a file called trigger which can be abused by sending a command to it. LED triggers can be any of

none usb-gadget usb-host kbd-scrolllock kbd-numlock kbd-capslock kbd-kanalock kbd-shiftlock kbd-altgrlock kbd-ctrllock kbd-altlock kbd-shiftllock kbd-shiftrlock kbd-ctrlllock kbd-ctrlrlock AC0-online BAT0-charging-or-full BAT0-charging BAT0-full BAT0-charging-blink-full-solid disk-activity disk-read disk-write ide-disk mtd nand-disk panic mmc0 mmc1 mmc2 rc-feedback rfkill-any rfkill-none bluetooth-power rfkill0 rfkill1 audio-mute audio-micmute rfkill3 phy0rx phy0tx phy0assoc [phy0radio] hci0-power rfkill5

You can change a LED lights "trigger" by issuing a valid directive to a LED lights trigger file. For example,

echo 'disk-activity' > /sys/class/leds/phy0-led/trigger

will make the phy0-led (typically a WLAN light on laptops) show the disk activity.

As good as all setups will have inputX::capslock, inputX::numlock and inputX::scrolllock available (they are typically present even if the keyboard don't actually have any LED lights at all).

The scroll lock light is a good choice if you want to use a LED light for something since most keyboards have it and that key is rarely used. The keyboards input number will vary and you may have more than one so the following command may be handy:

for led in /sys/class/leds/input?::scrolllock/trigger ;do echo 'disk-activity' > $led ;done

The above one-liner will make the scroll-lock light on all attached keyboards show disk activity. You can send kbd-scrolllock to the scroll locks trigger file to revert back to default behavior.

You can, similarly, use any supported LED light to show anything else with a valid trigger such as audio-mute, BAT0-charging and so on. What triggers you have will depend on what hardware is available, you will not have a BAT0-charging trigger unless the machine has a battery.

RGB Lights On Motherboards, RAM and Graphics cards[edit]

You can, with some effort, control RGB LEDs on some motherboards, graphics cards and even RAM sticks from GNU/Linux.

One major obstacle to controlling the vast majority of RGB LED lights is that the Linux kernel lacks support for most of them.

You can get RGB led lights using the OpenRGB software and a kernel patch provided by that project. You will have to compile your own kernel using their patch to use it and it may not work on your hardware even if it is listed as supported. There is also a fairly good chance OpenRGB will work if your hardware is listed as supported.

See OpenRGB for details. It really is your best bet if you want to use/control RGB lights on Linux - but it is, as mentioned, not all that easy to get it working since you do need to be a wizard capable of compiling a kernel with the special patch (until/unless that patch lands upstream).


avatar

Anonymous (3f4c27da56)

30 months ago
Score 0
The one liner gives me syntax error near unexpected 'do' token
avatar

Anonymous (3f4c27da56)

30 months ago
Score 0

tried it again and this time it worked, why is linux at times as weird as windows?

thanks author!
avatar

Anonymous (af49490145)

22 months ago
Score 0
Thanks for this. I wanted to turn on/off my keyboard lights using ioctl system call but didn't know where they were located😅
avatar

Anonymous (a79fd44f22)

one hour 36 minutes ago
Score 0
Hey author, can you explain all the triggers pleae?
Add your comment
LinuxReviews welcomes all comments. If you do not want to be anonymous, register or log in. It is free.