2

I am trying to run a script that changes the backlight, but requires root priviliges using a keyboard shortcut. Since I dont want to type in my password everytime I change the brightness, I want to add an exception in my sudoers file for that script. However none of my attempts have been succesfull. Please note that I did not write this script myself, and thus dont completely understand it (for the most parts, I do though).

extra info:

-I created an alias in my .bashrc file for the script:

alias backlight='sudo ./.config/Scripts/backlight.sh'

-I removed the sudo commands from the script:

#!/bin/bash

# base dir for backlight class
basedir="/sys/class/backlight/"

# get the backlight handler
handler=$basedir$(ls $basedir)"/"

# get current brightness
old_brightness=$(cat $handler"brightness")

# get max brightness
max_brightness=$(cat $handler"max_brightness")

# get current brightness %
old_brightness_p=$(( 100 * $old_brightness / $max_brightness ))

# calculate new brightness % 
new_brightness_p=$(($old_brightness_p $1))

# calculate new brightness value
new_brightness=$(( $max_brightness * $new_brightness_p / 100 ))

# set the new brightness value
(sudo) chmod 666 $handler"brightness"
echo $new_brightness > $handler"brightness"

-These are the lines i was experimenting with by adding them to the end of the sudoers file:

ALL ALL=(ALL) NOPASSWD: /home/sepseel/.conf/Scripts/$
#ALL ALL=(ALL) NOPASSWD: /bin/chmod

-My system is currently running ubuntu 16.04, with te i3wm so I have this in my i3 config file for the shurtcut:

bindsym XF86MonBrightnessUp       exec "sudo ./home/sepseel/.config/Scripts/backlight.sh +10"
bindsym XF86MonBrightnessDown     exec "sudo ./home/sepseel/.config/Scripts/backlight.sh -10"

Thanks in advance.

  • I have not tried to do anything like this but if I did, I would try to debug it by first using a standard command in place of yours and see if it could do something only root could do. If that worked, then copy that command to your home directory and try your experiment by pointing to the copy in your home directory. Should tell you whether it is that your script or the location is the problem vs an issue with sudoers configuration file. Standard debugging way to narrow down the problem. – jpezz Nov 1 '17 at 22:23
  • hey jpezz, thanks for the reply, i did try that earlyer. I used the apt-get update command, and that seemed to work the way you would expect it to. I should have mentioned that in my original post. (: – seppe van besauw Nov 1 '17 at 22:48
  • First, document in your original posting what you have tried so someone reading it knows what you did. That includes what you see when you try to execute the command. BTW, another good test is to add to the front of each command in the file the word "echo" as in <echo basedir="/sys/class/backlight/" this will echo, not execute the commands and you will see if it executes at all and what it would do if it worked. At this point, I can't see what happens when you try to execute the command in the sudoers config file as a user. – jpezz Nov 3 '17 at 1:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.