How to turn numlock on from command line

So it looks like numlockx is the standard way to have numlock automatically enabled. In my case I have my own scripts that run at startup anyway, and it just feels cumbersome to install an entire package to turn on numlock. My hope is that someone can tell me how to do this from the command line, so I can just slip it in an existing script.

Note that I am not interested in turning numlock on for the login screen, which seems to be the focus of many similar questions. I want to do what can easily be done with numlockx, but by just adding a line or two to an existing script that runs on login. Thanks in advance!


setleds -D +num to turn on numlock or setleds -D -num to turn it off.


You can use setleds. See man setleds or setleds --help for more information.

I believe that in the man page, there is an example of setting numlock on various terminals. You would need to specify /dev/tty7 for the terminal.


You can set the numlock state from within an SSH session by accessing /dev/console, which requires root permission (unless you change the permissions on /dev/console).

All of these commands assume that the user can execute sudo without a password. This is often the case when the user is in a group called "sudo" or "wheel" depending on distribution and local configuration.

# turn on numlock
sudo sh -c 'setleds +num < /dev/console'

# turn off numlock
sudo sh -c 'setleds -num < /dev/console'

It's also possible to write to the /sys entry for the device:

# Note that 'bash' is used to support the '?' glob.
# You could use 'sh' if you specified "input1."

# Turn off numlock LED (also turns off the numlock state)
sudo bash -c 'echo 0 > /sys/class/leds/input?::numlock/brightness'

# Turn on numlock LED
sudo bash -c 'echo 1 > /sys/class/leds/input?::numlock/brightness'

For the /sys approach, you don't need to spawn a sub-shell since tee can write to the brightness file without output redirection:

# both the ? glob and > redirection happen outside the sudo process by your local shell

# Turn on
echo 1 | sudo tee /sys/class/leds/input?::numlock/brightness > /dev/null

# Turn off
echo 0 | sudo tee /sys/class/leds/input?::numlock/brightness > /dev/null

Tested on a Raspberry Pi 4 Model B rev 1.4.