Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operation not permitted

I want to run some command in python script

import fcntl

KDSETLED = 0x4B32
SCR_LED  = 0x01

console_fd = os.open('/dev/console', os.O_NOCTTY)
fcntl.ioctl(console_fd, KDSETLED, SCR_LED)

I've set a+rw for /dev/console but when I run script from regular user:

fcntl.ioctl(console_fd, KDSETLED, SCR_LED) IOError: [Errno 1] Operation not permitted

What should I do if I need to run that script from regular user?

like image 991
Max Frai Avatar asked Jul 25 '26 19:07

Max Frai


1 Answers

I believe you need to get your script executed with CAP_SYS_TTY_CONFIG. Either that, or (if you're running on the console), using your controlling tty (e.g., /dev/tty1) instead of /dev/console might work.

The kernel code that enforces this appears to be drivers/tty/vt/vt_ioctl.c:

/*
 * To have permissions to do most of the vt ioctls, we either have
 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
 */
perm = 0;
if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
    perm = 1;
⋮
case KDSETLED:
    if (!perm)
        goto eperm;
    setledstate(kbd, arg);
    break;

So, definitely looks like those are your two options.

like image 141
derobert Avatar answered Jul 27 '26 08:07

derobert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!