su or sudo: two ways to act as root

Video 117 of 140 in Virtualization Foundations, in the unit “Users, groups, permissions and sudo”. 2:53, with English and Hindi audio.

Two commands can give you a root shell: su and sudo. They look alike, but they ask for different passwords, they grant different amounts of power, and they leave different traces in the log. You will meet both on every Linux machine you support, and the choice affects who can be traced when something changes. This article compares the two, shows how each one is used, and explains why sudo is the usual choice on Ubuntu servers.

What you will learn

What su and sudo do, side by side

The record matters most when something changes. With sudo, each use is logged with your name. With su, it is harder to tell who used the account.

Opening a root shell with sudo

Ask for your own password, then open a root shell
sudo -i

After sudo -i, everything typed in that shell runs with root's power. Close it as soon as the job is done, so the commands you type later run as yourself again. Running one command with sudo, instead of a root shell, avoids a root shell completely.

Switching to another account with su

Switch to bob, with his home folder and settings
su - bob

su with a name switches to that account and asks for its password. The dash makes it a login shell, so you get that account's home folder and settings.

Ask for the root password
su -

Note: With no name, su asks for the root password. On Ubuntu, the root account has no password set by default, so plain su fails for most people. That is one reason sudo is the usual choice.

Why most administrators choose sudo

su works only when the target account has a password that it can accept. On many Ubuntu servers, that is one more reason to prefer sudo. The rules for sudo are kept in one place, so access can be granted or removed there.

Try it yourself

You need a Linux machine where your account is allowed to use sudo. The root shell has full power, so leave it as soon as the step is done.

  1. Print the account your shell runs as. It should be your own account.

    whoami

    What you should see: The shell prints your own account name, such as alice.

  2. Open a root shell with sudo. It asks for your own password, not root's.

    sudo -i

    What you should see: The shell asks for your own password, then opens a root shell.

  3. Check the account again, now inside the root shell.

    whoami

    What you should see: The shell prints root. Type exit to return to your own account when the job is done.

Terms

Root
The administrator account on Linux, which has full power over the machine.
Login shell
A shell started as if the account had logged in, with that account's home folder and settings.

Check yourself

Which password does sudo ask for, and which does su ask for?

sudo asks for your own password. su asks for the password of the account you switch to, such as root's.

Why does sudo leave a clearer trace than su?

Each sudo run is written to the log under your name, so the record shows who acted.

Why does plain su usually fail on Ubuntu?

On Ubuntu the root account has no password set by default, so su cannot ask for one that works for most people.

Previous: sudo and the sudoers file. Next: System users: accounts that run services.