Sudo – How is Sudo Intended to Be Used?

rootsudo

I've tried configuring sudo before, but I haven't had too much luck with it. How is it different from su -l -c "x"? It seems that via the configuration file, one can make it so a user can only have access to certain commands and more. I always thought of sudo as a way of one-lining a command as another user or group. Since distros like Ubuntu and Mint make it easy by essentially giving the main user easy access to root via a password, I'm not really sure what its intended use is.

How do I add a user to the sudo file, giving them rights to only run certain commands at root? I also don't want to open up any security holes.

Best Answer

For basic operation — running commands as root — the most visible difference between sudo and su is that sudo requires the password of the calling user (i.e. your password) whereas su requires the password of the target user (i.e. the root password). The security implications have been discussed extensively in a previous question: Which is the safest way to get root privileges: sudo, su or login?.

Sudo has additional features beyond su's. In particular, once you have a user's password, you can run any command as that user. On the other hand, sudo can be configured so that the user invoking it can only run specific commands as some other user. This is possible because sudo doesn't require any authentication (other than perhaps confirming that you are you by typing your password — but that's subtly different from authenticating your user for a task).

You change the sudo configuration by running the visudo command as root (never edit the configuration directly). Make sure the environment variable EDITOR or VISUAL is set to your favorite editor or you may get an unfamiliar editor. The sudoers man page is a bit terse but has examples. To allow the user bob to run /bin/foo (with any number of arguments) and /bin/bar --safe (but not with any other argument) as root, use the following lines:

bob ALL = (root) /bin/foo
bob ALL = (root) /bin/bar --safe
Related Question