sudo and the sudoers file

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

Working as root all day is risky, because one wrong key can break the whole machine. The safer way is sudo, which runs one command as root and nothing more. It asks for your own password, not the shared root password, and it writes a log line each time. This article explains how sudo is used, where its rules are written in the sudoers file, why that file must be edited with visudo, and what the log shows an auditor.

What you will learn

Why sudo replaces a shared root password

In the old days, many administrators shared one root password. Nobody could tell who had done what, and changing the password meant telling everyone. sudo avoids that problem. It asks for your own password, runs only the command you typed, and writes a log line that says who ran it.

sudo remembers a recent password for a short time, so a few commands in a row do not ask for it each time.

Running one command with sudo

Restart the ssh service as root
sudo systemctl restart ssh

Note: sudo asks for your own password, then runs the command as root. Put sudo in front of any command that needs root.

List what your account may run with sudo
sudo -l

The list depends on the rules. If the password is refused again and again, your account probably lacks sudo rights, and an administrator must check the rules.

The sudoers file and why you edit it with visudo

The rules that decide who may run what live in one file, the sudoers file. The default line for the sudo group on Ubuntu is %sudo ALL=(ALL:ALL) ALL. The percent sign means a group, so every member of the sudo group may run any command as any user, on any host.

Rules can also name one user, such as alice, or one command, such as a single service restart. Narrow rules are safer than broad ones.

Open the sudoers file in an editor that checks the syntax
sudo visudo

Note: Never edit the sudoers file with a plain editor. One typo there can lock every administrator out, so use visudo, which checks the syntax before it saves.

What the log shows after a sudo use

Every sudo use writes a log entry. The log records who asked, which command ran, and which user it ran as. The entry also records when it happened. Those facts answer most audit questions, and they are what an auditor reads later.

Try it yourself

You need an account that is allowed to use sudo. Both steps only read the rules, so run them without changing anything.

  1. List what your account may run with sudo. The list depends on the rules on this machine.

    sudo -l

    What you should see: The list of commands your account may run with sudo, which depends on the rules.

  2. Open the sudoers file to read the rules. Quit without saving any change.

    sudo visudo

    What you should see: The file opens in an editor that checks the syntax before it saves.

Terms

sudoers file
The file that lists which accounts or groups may run which commands with sudo, and as which user.
visudo
The editor for the sudoers file, which checks the syntax before it saves the file.

Check yourself

Why is sudo safer than sharing the root password?

sudo asks for your own password and runs only the command you name. Each use is logged with who ran what.

Why should the sudoers file be edited with visudo?

visudo checks the syntax before it saves, so one typo does not lock every administrator out.

What does a sudo log entry show?

Who asked, which command ran, and which user it ran as, together with the time.

Previous: setuid, setgid and the sticky bit. Next: su or sudo: two ways to act as root.