Skip to content

Instantly share code, notes, and snippets.

@volodalexey
Created April 3, 2019 11:32
Show Gist options
  • Save volodalexey/bae7c3a8b40dad102f35f5b1b28b80b6 to your computer and use it in GitHub Desktop.
Save volodalexey/bae7c3a8b40dad102f35f5b1b28b80b6 to your computer and use it in GitHub Desktop.
Change screen/keyboard brightness (backlight) on Ubuntu on Terminal/script

To see your Laptop model you can do following:

sudo dmidecode | grep Version

If you have ASUS Laptop as me N501JW.210 than you have two brightness/backlight controls one for display/screen and second one for keyboard.

You can change brightness from command-line with following command:

echo 400 | sudo tee /sys/class/backlight/intel_backlight/brightness

And for keyboard:

echo 1 | /sys/devices/platform/asus-nb-wmi/leds/asus::kbd_backlight/brightness

Unfortunately it requires sudo privileges, because of potential risk.

Maximum brightness/backlight level can be obtain from corresponding files:

cat /sys/class/backlight/intel_backlight/max_brightness

And for keyboard:

cat /sys/devices/platform/asus-nb-wmi/leds/asus::kbd_backlight/max_brightness

To avoid all time to enter sudo password you can create script file screen_brightness:

#!/bin/bash
#sudo visudo
#sudo chown lex brightness
MIN_BRIGHTNESS="0"
MAX_BRIGHTNESS=$(cat /sys/class/backlight/intel_backlight/device/intel_backlight/max_brightness)
MIDDLE_BRIGHTNESS=$((MIN_BRIGHTNESS + (MAX_BRIGHTNESS - MIN_BRIGHTNESS) / 2))

set_brightness() {
  step=100
  cur_br=$(get_brightness)
  if ! [ "$1" -eq "$1" ] 2> /dev/null; then
    if [ "$1" = "max" ]; then
      new_br=$MAX_BRIGHTNESS
    elif [ "$1" = "min" ]; then
      new_br=$MIN_BRIGHTNESS
    elif [ "$1" = "toggle" ]; then
      if [ "$cur_br" -eq "$MAX_BRIGHTNESS" ]; then
        new_br=$MIN_BRIGHTNESS
      else
        new_br=$MAX_BRIGHTNESS
      fi
    elif [ "$1" = "+" ]; then
      new_br=$((cur_br + step))
      if [ "$new_br" -gt "$MAX_BRIGHTNESS" ]; then
        new_br=$MAX_BRIGHTNESS
      fi
    elif [ "$1" = "-" ]; then
      new_br=$((cur_br - step))
      if [ "$new_br" -lt "$MIN_BRIGHTNESS" ]; then
        new_br=$MIN_BRIGHTNESS
      fi
    else
      echo "Invalid brightness argument! $1" 1>&2
      exit 1
    fi
  else
    new_br=$1
  fi

  if [ "$new_br" -ge "$MIN_BRIGHTNESS" ]; then
    if [ "$new_br" -le "$MAX_BRIGHTNESS" ]; then
      #sudo sh -c 'echo $new_br > /sys/class/backlight/intel_backlight/brightness'
      echo $new_br >> /sys/class/backlight/intel_backlight/brightness
    else
      echo "Too high brightness value! Maximum: $MAX_BRIGHTNESS, passed: $new_br" 1>&2
    fi
  else
    echo "Too low brightness value! Minimum: $MIN_BRIGHTNESS, passed: $new_br" 1>&2
    exit 1
  fi
}

get_brightness() {
  cat /sys/class/backlight/intel_backlight/brightness
}

icon() {
  br=$(get_brightness)
  perc_br=$(echo "scale=2; $br / $MAX_BRIGHTNESS * 100" | bc)

  [ "$br" -le "$MIDDLE_BRIGHTNESS" ] && ret="💻🔅 $perc_br %"
  [ "$br" -gt "$MIDDLE_BRIGHTNESS" ] && ret="💻🔆 $perc_br %"

  echo "$ret"
}

case $BLOCK_BUTTON in
  1) sudo /path/to/screen_brightness toggle ;;
  4) sudo /path/to/screen_brightness + ;;
  5) sudo /path/to/screen_brightness - ;;
esac

if [ $# -eq 0 ]
then
  icon
else
  set_brightness $1
fi

Set this file as executable sudo chmod +x screen_brightness And finally add path to this script file to the sudo:

sudo visudo

Add following line at the end:

yourusername ALL=(ALL) NOPASSWD: /path/to/screen_brightness

Save and exit.

Now you can use this script file without password promt:

sudo /path/to/screen_brightness +
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment