I have ubuntu 8.04 and I want to write a bash script that runs as root
which every user can run.
I myself can do sudo.
How do I do that?
CLARIFICATION: I don't want to do it with sudo, because then users will have to type their password. I just want them to run the script as root, perhaps something setuid, dunno.
If this was a normal binary, you could setuid by running
Unfortunately, scripts can't be setuid. (Well you can, but it's ignored). The reason for this is that the first line of the script tells the OS what interpreter to run the script under. For example if you had a script with:
You'd actually end up running
Obviously, you'd need the interpreter to be setuid, which would then mean all scripts would be setuid. This would be bad.
You can do this with sudo by putting the following in your /etc/sudoers file by running visudo.
And now any user can run
This allows them to run the script without typing in their password.
There is an alternative that doesn't require sudo in the command, which requires creating a small setuided binary that execs your script, but every additional setuid binary adds another potential security problem.
I needed to insert that line AT THE END of /etc/sudoers :
ALL ALL = NOPASSWD: <filename>
Apparently, a later%admin ALL=(ALL) ALL
override required a password for admin users.There is no security problem allowing a script to be run as root as long as the script does a well determined, harmless, allowed action and, if values for any parameters cannot cause the script to misbehave.
But there is a gotcha...
Always use full paths in command and file names. If you write something like
echo Hello world!
inmyrootscript
, someone might write a~/bin/echo script
andmyrootscript
would execute as root whatever is in it./bin/echo "Hoping this will keep you safe"
:-)By default, members of the
wheel
group are permitted tosudo
any command asroot
. This is probably how you are usingsudo
to date.To permit another user you will need to create a
sudoers
rule. For example:Will allow the user
mickey.mouse
to run the command/usr/local/bin/test.sh
asroot
without requiring an additional password prompt.You should read this document for more information.
Usechmod +s <filename>
to se the suid bit. This means that when the file is executed, it runs with the permissions of the owner of the file (so chown it to root to make it run as that).However, this can be VERY dangerous with something like a bash scripts, because a user finds a way to change it, they can easily gain a root shell. Make sure that it can't be written to by anyone except root.Linux doesn't allow you to setuid on scripts, to do this you'd have to compile it as a program. Instead, you can use the sudoers file (/etc/sudoers), and add a line like this.
<username> ALL = NOPASSWD: /path/to/script
Another alternative: you can do a small setuid C wrapper around that script that
Kind of like your own subset of the multiple capablities of
sudo
.