5

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!

4 Answers 4

3

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.

1

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.

1

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

1
  • 6
    I got this error: setleds: Error reading current flags setting. Maybe you are not on the console?: ioctl KDGKBLED: Inappropriate ioctl for device
    – Felipe
    Jun 8, 2020 at 3:39
0

To build off RaphaelDavid's answer using setleds -D +num and setleds -D -num:

You might get an additional error like: setleds: Error reading current flags setting. Maybe you are not on the console?: ioctl KDGKBLED: Inappropriate ioctl for device

The solution to this error message is to run the setleds commands in a TTY (example: CTRL + ALT + F2)

To switch away from the TTY and back to your desktop, use CTRL + ALT + F1.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .