User Identities in Linux: How to use the sudo and su commands

published on Tue Mar 31 2020

Often 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 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 -