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
- su becomes another account, and it asks for that account's password.
- sudo runs commands as root, asks for your own password, and logs each use under your name.
- sudo can run one command, or open a root shell with sudo -i.
- On Ubuntu, the root account has no password set by default, so su with no name usually fails.
- Most administrators choose sudo, because the record shows who acted and the rules limit what can run.
What su and sudo do, side by side
- su stands for substitute user. It switches your shell to another account, and it asks for that account's password, such as root's. You get a full shell as that account.
- sudo asks for your own password, and runs only the commands its rules allow. Each run is written to the log under your name.
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
sudo -iAfter 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
su - bobsu 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.
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.
Print the account your shell runs as. It should be your own account.
whoamiWhat you should see: The shell prints your own account name, such as alice.
Open a root shell with sudo. It asks for your own password, not root's.
sudo -iWhat you should see: The shell asks for your own password, then opens a root shell.
Check the account again, now inside the root shell.
whoamiWhat 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.