Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I write a bash file thus:

#!/bin/bash         
echo 1000 |sudo tee /sys/class/backlight/intel_backlight/brightness

(that changes the screen's brightness)

I need it to run at startup, but because it requires the sudo pass-phrase I must enter the pass-phrase manually. How can I reform this code not to require the sudo pass-phrase at run time?

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

For this particular problem, you could probably just stick echo 1000 > /sys/class/backlight/intel_backlight/brightness in /etc/rc.local (before the exit-line). That would run it on boot, as root before the desktop was loaded.

But in general (how do I run a script as root?) things are a bit sticker...

You could just run sudo ./myscript (by editing sudoers) but just allowing ./myscript to be run without a password would mean you (or a program running as you) could edit that file and suddenly you can run anything as root without going through the proper procedures. Uber-poor security.

Instead I would suggest:

sudo mv myscript /sbin/myscript
sudo chown root:root /sbin/myscript
sudo chmod 700 /sbin/myscript

The ch* commands now mean only root can read/run the script. Sticking it in /sbin/ is just incidental - you don't have to do that.

Now you need to edit /etc/sudoers. The Wiki has a good example and you'll just need to substitute the shutdown commands with your script's path (use a full, absolute path). NOPASSWD is the key keyword here. It allows you to run something as sudo without the password. So then you can just add sudo /sbin/myscript to your startup jibberyboo and you're flying.

share|improve this answer
    
Oli what can you say about a solution like echo password | sudo -S ./myscript ? –  desgua Mar 8 '12 at 17:40
    
@desgua Yuck. That would have your sudo-capable password in plaintext somewhere in your local settings/xinit. Essentially anything you ran could read it. You might as well stick your password in ~/heres-my-password-guys in case somebody wants to root your computer :) The point of sudoers is to lock things down as far as you possibly can. My suggestion that limits that open access to a file that only somebody with legitimate root access could have written, without giving away your password or open root access. –  Oli Mar 8 '12 at 18:05
    
Thank you for the warning ;-) –  desgua Mar 8 '12 at 18:32
    
For the record here is another reference that may be useful for someone viewing this page: gratisoft.us/sudo/sudoers.man.html#nopasswd_and_passwd –  desgua Mar 10 '12 at 18:25
add comment

I'm just taking a shot in the dark, but try to change permissions on the file

chmod 777 [file path]

share|improve this answer
1  
That just means anybody and everybody can read/write/execute. It doesn't do anything for permissions. –  Oli Mar 8 '12 at 16:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.