User Identities in Linux: How to use the sudo and su commands
published on Tue Mar 31 2020Often we want to gain superuser privileges to carry out some administrative task, but it is also possible to impersonate another regular user. We can do so using the sudo and su commands as explained below.
sudo command
The sudo command has some advantages over the su command. They are:
- The administrator can configure
sudoto allow an ordinary user to execute only specific commands as a superuser - Authenticating using
sudodoes not require the superuser’s password by the user’s own password - It doesn’t start a new shell or load another user’s environment
The syntax looks like this:
sudo <command>
su command
The su command is used to start a shell as another user. The command syntax looks like this:
su [-[l]] [user]
If the -l option is included, the resulting shell session is a login shell for the specified user which includes their environment and working directory. If the user is not specified, the superuser is assumed.
The -l may be abbreviated as -. To start a shell for the superuser, we would do this:
su -