Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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]
    echo "Must be root"
    exit 1

at the begining of the script.

share|improve this answer
thanks that works perfectly! – user31831 Jan 15 '10 at 7:20
@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

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

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

Your Answer


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