Wednesday 21 March 2007

su vs sudo

Both su (substitute user) and sudo can be used to do some operations as the user other than the currently login user. Although any other user can be chosen, mostly the root user is picked, so that some administration tasks can be carried out.

To run a single command, you do with su -c or sudo . This is default to run the command as root, if you want to run as another user, specify the username at the command line, i.e., su -c or sudo -u .

To switch to another user and obtain a shell, you do su or sudo -s -u . Again if username is not specified, by default root is the target account. Additionally, su -l (or su - ) or sudo -H -s -u adherent the value of the HOME environmental variable of the target user.

As above described, the functionalities provided by su and sudo are very similar. So what is the difference?

The key difference is that if you use su, you have to know the password of the target user; not if you use sudo. For this mechanism, by default all normal users can use su (as long as (s)he knows the target user’s password). The use of sudo is controller by /etc/sudoers, which can be set to require a user to provide his/her own password so as to run sudo, or, it can grant a user to use sudo without any authentication. What’s more, sudo can limit a user to be able to run only a certain set of commands.


I think sudo is better because it doesn’t give out the root password and has more control of running commands. In fact, ubuntu by default ships with a disabled root account and allows the first normal user account to have sudo access.


No comments: