Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a simple command that I want to be able to perform as a user, but it requires root permissions. I suspect that this is a case for the "SUID"-bit, but I've never used it.

This is what I've tried:

aioobe@e6510:~/bin$ sudo -s
root@e6510:~/bin# cat -> spindown_baydrive
#!/bin/bash
/sbin/hdparm -Y /dev/sdb
root@e6510:~/bin# chmod +x spindown_baydrive 
root@e6510:~/bin# chmod ug+s spindown_baydrive 
root@e6510:~/bin# exit
aioobe@e6510:~/bin$ ./spindown_baydrive 
/dev/sdb: Permission denied
aioobe@e6510:~/bin$


aioobe@e6510:~/bin$ ls -la spindown_baydrive 
-rwsr-sr-x 1 root root 37 2011-01-31 09:59 spindown_baydrive

Any suggestions?

share|improve this question

3 Answers 3

up vote 7 down vote accepted

This is exactly the kind of thing that sudo was designed for. use visudo to edit the sudoers to allow the non privileged user to run your script as root.

visudo

add a line like this

aioobe ALL=NOPASSWD: /path/to/spindown_baydrive

and save the file

now you can run the file as root using the command

sudo /path/to/spindown_baydrive

If you want to require a password to be entered to run the script then change the sudoers line above to

aioobe ALL= /path/to/spindown_baydrive

share|improve this answer
    
excellent, thanks a lot. I don't understand your last remark thou... "If you want to require a password to be entered to run the script..." If I wanted a password, I wouldn't have to do anything, just sudo scriptname, right? –  aioobe Jan 31 '11 at 9:37
    
The last command would ask aioobe to enter their password before they could run the script. It helps to ensure that only aiobee can run the command as root. –  Iain Jan 31 '11 at 9:41
    
Ahaa, cool. Thanks again. –  aioobe Jan 31 '11 at 10:18
    
Exactly what I needed. Couldn't figure out why SUID wasn't working with my scripts. Made root owner of file, but allowed admin ability to use sudo on this file without entering a password. Works like a charm in my cronjobs. Thanks a lot. –  Safado Sep 22 '11 at 21:33
    
I take that back, it doesn't work in my cronjobs, but it works from the command line! –  Safado Sep 22 '11 at 22:17

You can let the user run the script as root via sudo by configuring it in /etc/sudoers, without forcing the user to enter his password (see the NOPASSWD option).

To suid bash scripts, read more here: (Ubuntu) setuid bash doesn't work

share|improve this answer

-rwsr-sr-x 1 root root 37 2011-01-31 09:59 spindown_baydrive

At 37 bytes, I'm guessing this is a shell script. When running as setuid, the shell starts new processes as the original uid. If you google for 'shell script setuid' you'll see lots of explanations why it doesn't work - and lots of ways to resolve the problem, obvious ones are using sudo or writing a wrapper program in C.

share|improve this answer

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.