23

I'm starting X as a user and need to set my keyboard brightness in /sys/class/leds/asus\:\:kbd_backlight/brightness. The /sys/ directory gets recreated after reboot, so the permissions will reset too. How do I set it up so I don't need to make the file writable by all users after every boot?

I'm using Archlinux with SDDM as the login manager and KDE as DE.

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
xoned
  • 333
  • 2
    You could put a command in a systemd boot service. – goldilocks Mar 23 '13 at 13:52
  • Are you meaning to create a own systemd service which set the permissions? Or do you talk from a specific file? – xoned Mar 23 '13 at 14:04
  • The former. "Service" is the systemd term, even though in this case it would just be a one-off command and not a daemon. You could make it a kind of "miscellaneous stuff" service like rc.local is under sysV. In this case the service just runs a shell script, and you put the command in there. – goldilocks Mar 23 '13 at 14:31

4 Answers4

18

No you can't, the permission of sysfs is defined in kernel space and can't be changed with userspace tools (unless with kernel side support).

But for your own problem, you could setup a sudo entry that allow everyone to write to that path, i.e ALL ALL = (ALL) NOPASSWD: /usr/bin/tee /sys/class/leds/asus\:\:kbd_backlight/brightness

And when you write to that directory, use a script like this, echo 1 | sudo /usr/bin/tee "/sys/class/leds/asus::kbd_backlight/brightness"

daisy
  • 54,555
  • I tried that, but it asks always for the password. I also tried to add a NOPASSWD: behind the (ALL). – xoned Mar 29 '13 at 16:19
  • @Timo can you try again? See my updates – daisy Mar 29 '13 at 22:55
  • It works now. The problem was that my entry was overwritten by another one without NOPASSWD. And i accepted your answer because it was easier as the other answers. Thanks all. – xoned Mar 30 '13 at 18:24
  • I am using linux Mint Debian in sony vaio and trying to create a file in /sys/devices/platform/sony-laptop/kbd_backlight to control the keyboard Backlight. I have changed the path in your code which leads to this error: bash: syntax error near unexpected token('The command I typed isALL ALL = (ALL) NOPASSWD: /usr/bin/tee /sys/devices/platform/sony-laptop/kbd_backlight` Please help – Indra Jun 14 '14 at 14:00
  • I couldn't get this working on an Ubuntu machine until I added a comma between /usr/bin/tee and /sys/class.... After a brief look at the grammar in man sudoers I suspect the comma is necessary. – Sage Mitchell May 02 '15 at 00:23
  • 1
    Rather than issue permissions to ALL, it would be better from a security point of view, to give the permission to a User Group, and then make the relevant User(s) a member of that Group. – MikeW Jul 04 '19 at 10:19
  • I do not understand, how to implement this answer? – Vitaly Zdanevich Dec 24 '22 at 19:30
8

additional way to archive permission changes is with an udev rule:

$ cat /etc/udev/rules.d/99-asus.rules

# allow keyboard brightness control for everyone
ACTION=="add", SUBSYSTEM=="leds", KERNEL=="asus::kbd_backlight", RUN+="/bin/chmod a+w /sys/class/leds/%k/brightness"

to test the rule try with
udevadm test /sys/class/leds/asus::kbd_backlight
in the report you should see the specified Run command.

and to trigger it use
sudo udevadm trigger --verbose --action=add /sys/class/leds/asus::kbd_backlight

based on this answer

6

I had a similar problem, I needed to set the permissions before running the nodered service. Following the comment of goldilocks I created this systemd script:

$ cat /etc/systemd/system/setledspermissions.service

[Unit]
Description=Set leds writable to everybody
Before=nodered.service

[Service]
Type=oneshot
User=root
ExecStart=/bin/bash -c "/bin/chmod a+w /sys/class/leds/led0/*"

[Install]
WantedBy=multi-user.target

After writing the service file I enabled it with

$ sudo systemctl enable setledspermissions.service
$ sudo systemctl start setledspermissions.service
$ sudo systemctl status setledspermissions.service
  • Is this generally the way to go when needing to grant write access to specific /sys/ paths? My use case is setting the screen backlight brightness on my ĺaptop. – Richrd Aug 09 '18 at 19:44
  • I guess it is because the sys path is recreated each boot. But i'm not expert enough to be sure about that. – hariseldon78 Aug 11 '18 at 00:09
3

The /sys directory in Linux is fake, it is a view into the kernel dressed up as files. So to change permissions in it permanently means hackig the kernel, and that would be ill-advised. As the comments say, perhaps a systemd unit setting this would be a solution (in general, set the change up as part of the boot process).

vonbrand
  • 18,253