Controlling screen brightness - sudo vs sudo su

I’m writing a little shell utility to manually control my screen brightness by reading and writing values to /sys/class/backlight/intel_backlight/brightness but I’ve noticed something weird. If I run

$ sudo echo 64000 > /sys/class/backlight/intel_backlight/brightness

I get permission denied on that file. However if I run

$ sudo su
# echo 64000 > /sys/class/backlight/intel_backlight/brightness

It works and the screen brightness changes.

The brightness file is owned by root so I would expect to have to elevate to access it, but why do these two methods of elevation behave differently?

In this case, sudo is only being applied to the echo. The redirection is outside of that.

You could do that like this:

sudo sh -c "echo 64000 > /sys/class/backlight/intel_backlight/brightness"

Alternatively, you could do this:

echo 64000 | sudo tee /sys/class/backlight/intel_backlight/brightness
4 Likes

TIL more about pipes and redirection! Thanks for the explanation and the solutions. I’ll decide which one I prefer.

Going back to screen brightness, I assume changing the permissions on that file so I can read/write it without elevation would be a bad idea?

What DE are you using ? If you are using plasma you can do that without root like that:

qdbus org.kde.Solid.PowerManagement /org/kde/Solid/PowerManagement/Actions/BrightnessControl setBrightness 100

100 is the brightness in %

I’m using i3 on a Framework 13.

If i was you, i would:

  • Create a script with the previously working script (or as many you need, to avoiding having to provide the value).
#!/usr/bin/env bash
echo 64000 | sudo tee /sys/class/backlight/intel_backlight/brightness
  • making it executable and moving it to the correct folder:

chmod +x my_script && sudo cp my_script /usr/bin/my_script

  • creating a new rule for sudoers:

sudo nano /etc/sudoers.d/my_rules

your_username ALL=(ALL) NOPASSWD: /usr/bin/my_script

And its done, open a new shell and you will be able to run this script without sudo :slight_smile:

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.