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 am using Ubuntu 11.04. I want to run a script which checks for a running process and shuts down the system when that process ends. To do this, the user requires shutdown access so that the script may be executed without requiring a password.

I referred to this page and added the text in my sudoers file. However, it's still asking me the password whenever I execute sudo shutdown -h now. Also, I get an error saying root privileges are required, when I execute shutdown without sudo.

My sudoers file looks something like this.

# Cmnd alias specification
Cmnd_Alias      SHUTDOWN = /sbin/shutdown

# User privilege specification
root    ALL=(ALL:ALL) ALL
myusername    ALL = NOPASSWD: SHUTDOWN

How can I give permission to a user to execute the shutdown command without having to manually enter the password?

Thanks.

share|improve this question
1  
Could you post your real and whole sudoers file, please? –  enzotib Jun 19 '11 at 18:57
add comment

4 Answers 4

up vote 5 down vote accepted

You can shut down or restart using HAL or ConsoleKit which will not require you enter an username/password:

HAL (older Ubuntu versions):

dbus-send --system --print-reply --dest=org.freedesktop.Hal /org/freedesktop/Hal/devices/computer org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown

ConsoleKit (newer Ubuntu versions, tested on Ubuntu 11.04):

dbus-send --system --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Stop
share|improve this answer
add comment

I did it like in this answer, works flawlessly for me. Maybe your approach misses the visudo part?

Open a command line and type:

sudo visudo 

In the File that opens add the following line at the end:

yourusername ALL=NOPASSWD: /sbin/halt

Then exit the editor and safe it (CTRL+x).

Now you can shut down your computer from the command line, e.g. after you download something:

wget UrlOfTheFileYouWantToDownload ; sudo halt
share|improve this answer
add comment

Some time ago I did this by setting the suid (Set User ID). This makes the program execute as the owner of the file, not the user running it. Since shutdown is owned by root this should let you shutdown without sudo.

chmod u+s /sbin/shutdown

There are security implications though since anyone can then run shutdown. http://en.wikipedia.org/wiki/Setuid

share|improve this answer
    
In general, it is not advisable to modify files, or metadata of files, installed from packages, other than configuration files in /etc. –  enzotib Jun 20 '11 at 11:57
    
Hmm... true! But what is the official way then? Playing with polkit? Not so nice either... –  turbo Jun 20 '11 at 15:57
    
sudo is the right way, in my opinion, but unfortunately the user don't give the chance to see its sudoers file, and what's wrong with it. –  enzotib Jun 20 '11 at 18:05
add comment

It sounds like you'd want to run the script itself as root. If you want to run a script that waits for something to finish and then powers down your computer, then you'd just run the script with sudo. That will ask your password when you run the script, but then the script itself is free to shutdown the system without any further intervention.

share|improve this answer
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.