Linux setleds Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the setleds command in Linux to control the state of the keyboard LED indicators, such as the Caps Lock, Num Lock, and Scroll Lock LEDs. You will explore how to query the current LED state, set the LED state, and save the LED state as the default for future sessions. Additionally, you will learn how to automate keyboard LED state changes using shell scripts, which can be useful for various system configuration and settings tasks.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") subgraph Lab Skills linux/cat -.-> lab-422911{{"`Linux setleds Command with Practical Examples`"}} linux/echo -.-> lab-422911{{"`Linux setleds Command with Practical Examples`"}} linux/chmod -.-> lab-422911{{"`Linux setleds Command with Practical Examples`"}} end

Understand the Purpose of setleds Command

In this step, we will explore the purpose and usage of the setleds command in Linux. The setleds command is used to control the state of the keyboard LED indicators, such as the Caps Lock, Num Lock, and Scroll Lock LEDs.

The setleds command allows you to:

  • Query the current state of the keyboard LEDs
  • Set the state of the keyboard LEDs
  • Save the LED state as the default for future sessions

This command is useful for automating tasks that require specific LED states, such as setting the Num Lock LED to be on by default when a terminal session starts.

Let's start by checking the current state of the keyboard LEDs:

setleds -v

Example output:

Current led state:   00000000

The output shows the current state of the keyboard LEDs in binary format, where each bit represents the state of a specific LED (Scroll Lock, Num Lock, Caps Lock).

Now, let's turn on the Caps Lock LED:

setleds +caps

Example output:

Led state set to 00000001

The +caps option sets the Caps Lock LED to the "on" state.

To turn off the Caps Lock LED, you can use the -caps option:

setleds -caps

Example output:

Led state set to 00000000

The setleds command can also be used to set the default LED state for future sessions. For example, to make the Num Lock LED turn on by default, you can use the following command:

setleds +num

This will save the Num Lock LED state as the default, so the next time you log in, the Num Lock LED will be turned on automatically.

Modify Keyboard LED States Using setleds

In this step, we will learn how to use the setleds command to modify the state of the keyboard LEDs.

First, let's check the current state of the keyboard LEDs:

setleds -v

Example output:

Current led state:   00000000

As we can see, all the LEDs are currently in the "off" state.

Now, let's turn on the Num Lock LED:

setleds +num

Example output:

Led state set to 00000002

The output shows that the Num Lock LED (bit 1) is now in the "on" state.

To turn off the Num Lock LED, we can use the -num option:

setleds -num

Example output:

Led state set to 00000000

The Num Lock LED is now turned off, and the output shows that all LEDs are in the "off" state.

We can also toggle multiple LEDs at once. For example, to turn on the Caps Lock and Scroll Lock LEDs, we can use the following command:

setleds +caps +scroll

Example output:

Led state set to 00000005

The output shows that the Caps Lock (bit 0) and Scroll Lock (bit 2) LEDs are now in the "on" state.

To turn off the Caps Lock and Scroll Lock LEDs, we can use the -caps -scroll options:

setleds -caps -scroll

Example output:

Led state set to 00000000

All the LEDs are now in the "off" state.

Automate Keyboard LED State Changes with Shell Scripts

In this step, we will learn how to automate the process of changing the keyboard LED states using shell scripts.

First, let's create a simple shell script to toggle the Num Lock LED:

nano toggle_num_lock.sh

Add the following content to the file:

#!/bin/bash

## Get the current Num Lock LED state
current_state=$(setleds -v | grep -o -E '[01]{8}' | awk '{print substr($1,7,1)}')

## Toggle the Num Lock LED state
if [ "$current_state" == "0" ]; then
  setleds +num
else
  setleds -num
fi

Save and close the file.

Make the script executable:

chmod +x toggle_num_lock.sh

Now, you can run the script to toggle the Num Lock LED state:

./toggle_num_lock.sh

Example output:

Led state set to 00000002

The script checks the current Num Lock LED state and toggles it accordingly.

You can also create a script to set multiple LED states at once. For example, let's create a script to set the Caps Lock and Scroll Lock LEDs:

nano set_caps_scroll.sh

Add the following content to the file:

#!/bin/bash

setleds +caps +scroll

Save and close the file.

Make the script executable:

chmod +x set_caps_scroll.sh

Now, you can run the script to set the Caps Lock and Scroll Lock LEDs to the "on" state:

./set_caps_scroll.sh

Example output:

Led state set to 00000005

These scripts can be easily integrated into your system configuration or startup scripts to automatically set the desired LED states when the system boots up or when a user logs in.

Summary

In this lab, we learned about the purpose and usage of the setleds command in Linux. The setleds command allows us to query, set, and save the state of the keyboard LED indicators, such as the Caps Lock, Num Lock, and Scroll Lock LEDs. We explored how to check the current LED state, turn individual LEDs on and off, and set the default LED state for future sessions. This command is useful for automating tasks that require specific LED states, such as ensuring the Num Lock LED is on by default when a terminal session starts.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like