开发者

Write shell script that can only be run by a sudo user in Ubuntu

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 开发者_JAVA百科of doing this, thanks for helping


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜