
setleds Command in Linux
The setleds command in Linux is a utility used to control the state of the keyboard LEDs. These LEDs typically represent the status of the Num Lock, Caps Lock, and Scroll Lock keys. The setleds command allows you to manipulate these LEDs manually, either by setting them to a specific state or toggling them.
Table of Contents
Here is a comprehensive guide to the options available with the setleds command â
- Understanding setleds Command
- How to Use setleds Command in Linux?
- setleds Command Options and Parameters
- Viewing Current LED Settings
- Turning On Keyboard LEDs
- Turning Off Keyboard LEDs
- Examples of setleds Command in Linux
Understanding setleds Command
The setleds command in Linux is used to control the state of keyboard LED indicators such as Num Lock, Caps Lock, and Scroll Lock on virtual consoles. This command allows users to manually turn these LEDs on or off, which can be useful for scripts, accessibility needs, or troubleshooting keyboard-related issues.
The command works only on virtual consoles (tty terminals) and does not affect graphical terminal emulators like GNOME Terminal or Konsole. The basic syntax includes setleds +num (to enable Num Lock), setleds -caps (to disable Caps Lock), and setleds scroll (to toggle Scroll Lock). Running setleds without arguments displays the current LED status.
How to Use setleds Command in Linux?
The setleds command is used to set the keyboard LEDs. This command can be useful for various purposes, such as indicating the status of certain operations or providing visual feedback in custom scripts.
Basic Syntax
The basic syntax of the setleds command is as follows −
setleds [options]
setleds Command Options and Parameters
- -L − Show current settings.
- -num − Set or clear the Num Lock LED.
- -caps − Set or clear the Caps Lock LED.
- -scroll − Set or clear the Scroll Lock LED.
- +num − Turn on the Num Lock LED.
- +caps − Turn on the Caps Lock LED.
- +scroll − Turn on the Scroll Lock LED.
- -D − Show all possible options and their current state.
Viewing Current LED Settings
You can view the current state of the keyboard LEDs using the -L option.
setleds -L

This command displays the current settings for the Num Lock, Caps Lock, and Scroll Lock LEDs.
Turning On Keyboard LEDs
You can use the +num, +caps, and +scroll options to turn on the Num Lock, Caps Lock, and Scroll Lock LEDs, respectively.
Example: Turning On the Num Lock LED
setleds +num

This command turns on the Num Lock LED.
Example: Turning On the Caps Lock LED
setleds +caps

This command turns on the Caps Lock LED.
Example: Turning On the Scroll Lock LED
setleds +scroll

This command turns on the Scroll Lock LED.
Turning Off Keyboard LEDs
You can use the -num, -caps, and -scroll options to turn off the Num Lock, Caps Lock, and Scroll Lock LEDs, respectively.
Example: Turning Off the Num Lock LED
setleds -num

This command turns off the Num Lock LED.
Example: Turning Off the Caps Lock LED
setleds -caps

This command turns off the Caps Lock LED.
Example: Turning Off the Scroll Lock LED
setleds -scroll

This command turns off the Scroll Lock LED.
Toggling Keyboard LEDs
You can toggle the state of the keyboard LEDs by combining the + and - options.
Example: Toggling the Num Lock LED
setleds +num -num

This command toggles the state of the Num Lock LED.
Using setleds in Scripts
The setleds command can be used in shell scripts to provide visual feedback or indicate the status of certain operations.
Example: Using setleds in a Script
Here's an example of a shell script that uses setleds to indicate the start and end of a process.
#!/bin/ # Turn on Num Lock LED to indicate the start of the process setleds +num # Perform some operations echo "Starting the process..." sleep 5 # Turn off Num Lock LED to indicate the end of the process setleds -num echo "Process completed."
Persistent LED Settings
To make the LED settings persistent across reboots, you can add the setleds commands to a startup script, such as /etc/rc.local.
Example: Persistent LED Settings in /etc/rc.local
Edit the /etc/rc.local file and add the setleds commands.
#!/bin/sh -e # Turn on Num Lock LED at startup s exit 0etleds +num
Troubleshooting LED Settings
If the LED settings do not work as expected, you can check the following −
- Ensure that the correct options are being used.
- Verify that the keyboard supports the LED indicators.
Common Use Cases for setleds
Here are some common use cases for the setleds command:
- Status Indicators − Use the LEDs to indicate the status of certain operations or processes.
- Custom Scripts − Incorporate setleds into shell scripts for visual feedback.
- Accessibility − Provide visual indicators for users who may have difficulty seeing the on-screen status indicators.
Examples of setleds Command in Linux
Here are some detailed examples of setleds usage.
Example 1: Indicating Disk Activity
You can use setleds to indicate disk activity by turning on an LED when the disk is being accessed.
#!/bin/ # Turn on Num Lock LED to indicate disk activity setleds +num # Perform disk operations dd if=/dev/zero of=/tmp/testfile bs=1M count=100 # Turn off Num Lock LED to indicate the end of disk activity setleds -num echo "Disk activity completed."
Example 2: Indicating Network Activity
You can use setleds to indicate network activity by turning on an LED when data is being transmitted.
#!/bin/ # Turn on Caps Lock LED to indicate network activity setleds +caps # Perform network operations ping -c 5 google.com # Turn off Caps Lock LED to indicate the end of network activity setleds -caps echo "Network activity completed."
Conclusion
The setleds command is a versatile utility for controlling the state of the keyboard LEDs in Linux. By using setleds, you can customize the behavior of your keyboard LEDs to provide visual feedback, indicate the status of certain operations, and enhance accessibility.