DEV Community

Raunak Ramakrishnan
Raunak Ramakrishnan

Posted on

Setting display brightness on Linux from the command line

I auto-adjust the brightness on my display using a cron which runs at 7 pm everyday.

xrandr --output eDP-1 --brightness 0.9
Enter fullscreen mode Exit fullscreen mode

This sets the brightness to 90% of maximum brightness.

Working with Linux Display environments in a cron

The above command works when run from my terminal. But when run on a cron, it does not produce any effect. We need to explicitly mention the "display number" as an environment variable DISPLAY.

8 19 * * * DISPLAY=:0 xrandr --verbose --output eDP-1 --brightness 0.9
Enter fullscreen mode Exit fullscreen mode

How to get the display number of your monitor?

Running xrandr will give you a lot of output like

Screen 0: minimum 320 x 200, current 1920 x 1080, maximum 16384 x 16384
eDP-1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis)
Enter fullscreen mode Exit fullscreen mode

In my case, the display number is :0

Adjusting backlight brightness vs perceived brightness

If xrandr is unable to find backlight, it only changes "perceived" brightness i.e it does software color correction. If we want to save battery, we will need to reduce the backlight brightness.

In Linux, the backlights can be found in /sys/class/backlight. In my case, it was /sys/class/backlight/intel_backlight. In this folder, there are many files like

actual_brightness
max_brightness
brightness
Enter fullscreen mode Exit fullscreen mode

max_brightness shows highest possible level of brightness for the display. We can adjust the value in brightness file to reduce backlight brightness.
Here's what I do to adjust it: echo 1800 > /sys/class/backlight/intel_backlight/brightness (in root crontab)

Unlike the xrandr command, this does not require setting any DISPLAY variable in the cron file.

Other tools

  • brightnessctl is an easy to use tool which integrates well with systemd. It is also available in the package managers of various distributions like Debian, Ubuntu, Arch Linux.

Discussion (0)