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'm writing a shell script that is supposed to be run by users only in sudo user list, what's the appropriate way of doing this?

what I'm thinking is in the shell script, try to create a dummy file in system dir such as /var/run/ and remove it, so users not in sudo list will receive a permission error, but I believe there gotta be a more appropriate way of doing this, preferably detect if a user is in sudo list within the shell script. thanks for helping

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

It's much simpler to do, just put the below at the beginning of your script

if [ `id -u` -ne 0]
then
    echo "Must be root"
    exit 1
fi

at the begining of the script.

share|improve this answer
    
thanks that works perfectly! –  user31831 Jan 15 '10 at 7:20
2  
@unknown (google) - Don't forget to check the permissions on the script file. If someone can edit the script, they can undo your work. –  pcapademic Jan 15 '10 at 8:19
add comment

Put the script in a directory that is only readable by root. That way users can't call it accidentally, or deliberately, or even read it to look for holes.

share|improve this answer
add comment

when you run the script with sudo, sudo can check the group for you:

%yourgroup ALL=/your/command

when you put this in /etc/sudoers, sudo will check wether a user is in group yourgroup when the user executes sudo /your/command. this way you don't have to add a new user manually to the script.

share|improve this answer
    
That doesn't prevent anyone else from launching the script though, it just doesn't give them specific rights. –  ℝaphink Jan 15 '10 at 12:14
add comment

Your Answer

 
discard

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