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.

Possible Duplicate:
How to run an application using sudo without a password?

I've made a script that preforms various system tasks, that I run regularly, but I have to use sudo to run the script, despite the fact that I own it because it runs root privileged commands. How can I add make sudo let me run this one file without having to enter my password every time I run it?

I know that there are questions regarding running commands without a password, but I wasn't able to find one on running a file without a password.

I am aware of the security risks, but I'm at home on a computer that I trust.

I added john ALL = NOPASSWD: /home/john/.script/hibernate.sh to my /etc/sudoers but when running the command sudo sh /home/john/.script/hibernate.sh it still prompts me for my password...

share|improve this question

marked as duplicate by Mik, Thomas W., Tachyons, maythux, Mitch Oct 16 '12 at 5:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4 Answers 4

up vote 3 down vote accepted

Use "visudo" for a safe editing environment to edit the sudoers file. Then add a line like this:

username ALL= NOPASSWD: /path/to/your/script

The only part of this that bears elaboration is "ALL=" which specifies that the permission is granted when the user is logged in from any location, locally (console or terminal) or remotely (ssh, etc).

share|improve this answer
    
For whatever reason, this method doesn't work. My username is john, and the file resides in ".script" which is inside my home. So i entered "john ALL = NOPASSWD: /home/john/.script/hibernate.sh" where hibernate.sh is the name of the script. After running "sudo sh /home/john/.script/hibernate.sh" it still asks for my password... –  John Oct 15 '12 at 22:21
    
You need to make your .sh script executable and then just do "sudo /home/john/.script/hibernate.sh". The way you're trying to run it, john would need sudo permission to /bin/sh –  Sparr Oct 15 '12 at 22:34
    
the file is already executable, or at least is showed as such in nautilus > properties. –  John Oct 15 '12 at 22:37
    
I just used chmod +rwx, and it still won't work... –  John Oct 15 '12 at 22:42
    
the +x is step one. now change your command from "sh script" to "script" –  Sparr Oct 15 '12 at 23:25

You need to ass the full path to the program you're running to the sudoers file, not the script. I'm assuming it's pm-hibernate, so :

john ALL=NOPASSWD: /usr/sbin/pm-hibernate
share|improve this answer

For completeness sake, you can achieve a similar effect by setting setuid bit in the file's permissions.

A slightly tricky part is that for security reasons setuid bit on scripts is ignored by the kernel, so you'll need to compile a small wrapper program in C and use it to invoke your script. Save this as runscript.c:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
   setuid( 0 );
   system( "/path/to/script.sh" );

   return 0;
}

then compile it with

gcc runscript.c -o runscript

and set setuid bit on the executable:

sudo chown root:root runscript
sudo chmod 4755 runscript

It is also important to make sure your script is only writable by root, since everyone who can modify the script will be able to execute arbitrary programs:

sudo chown root:root /path/to/script.sh
sudo chmod 0711 /path/to/script.sh

Here's an article I've got the wrapper program code from: setuid on shell scripts.

Security-wise, both approaches - the one with sudo and the one with setuid - are pretty bad, but probably will be ok on a home machine. The difference is that every user in the system will be able to run a setuid command, even without being in the sudoers file. Also, obviously, you won't need to prefix the command with sudo.

share|improve this answer

You need the "NOPASSWD" tag.

Use visudo and set something like this at the end of the file:

username ALL=NOPASSWD: /bin/echo

Replace username with your username, and /bin/echo with the command you want to run.

share|improve this answer

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