views:

242

answers:

3

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, thanks for helping

+2  A: 

make sure you are in the sudoers list then:

sudo chmod 700 /path/to/scriptname

[answering comment]
sorry - I don't use subversion: my guess is that you will need to run another script as a sudoer that will create the script with the correct permissions.

No, you do not have to change ownership to root, but it will keep things clean if you do.

slashmais
is there a way to detect if a user is in sudo list within the shell script? the shell script will be submitted to subversion control system, so I'm worried that the file permission may be lost as it's being checked out and deployed on someone else's system
You should also `chown root file` as in Teddy's answer.
Dennis Williamson
chown to root is the simplest way. If You're worried about that, Dennis's answer will allow you to make a second manual check in your script. I would suggest that you do both (++ to both).
FalseVinylShrub
+8  A: 

Change the execute permissions on the script to only be executable by the user, and change the ownership of the script to root. That should do it.

Teddy
+2  A: 

You can check the values of $UID and $EUID in your script. They would be zero for being equivalent to root. Or, if not Bash, you can use id -u.

Dennis Williamson
If using `id -u`, be sure to use the full pathname or set your own PATH so that the proper binary is used (otherwise the user could write their own `id` program/script and arrange for it to be in the path before `/usr/bin` or where ever).
Chris Johnsen