sudo and the sudoers file

Intermediate · 21 min

Why admins use sudo instead of logging in as root, how sudoers rules are written and checked with visudo, drop-ins in /etc/sudoers.d, and rules that leak root.

Before thisManaging accounts and passwords

Learn Linux TV · 23 min · from 2:44

sudo and the sudoers file

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Learn Linux TVReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • sudo lets named users run named commands as another user, usually root, after proving who they are with their own password. Every use is logged, which a shared root password can never give you.
  • Debian and Ubuntu grant full sudo to the sudo group; Fedora and RHEL to wheel. A rule reads who, on which host, as whom, which commands: alice ALL=(ALL) ALL.
  • Edit with visudo, never a plain editor. It locks the file and refuses to save a syntax error that would otherwise lock everyone out of sudo.
  • Put local rules in /etc/sudoers.d/ and edit them with visudo -f. Files whose names contain a dot or end in ~ are silently skipped, so alice.conf never loads.
  • Least privilege is hard in sudoers: allowing an editor, pager, or any command that takes a file or script argument often hands out a root shell. When several rules match, the last one wins.

Why not just log in as root?

A small team shares the root password. Something gets deleted at 2 a.m. The log says “root did it”. Which of five people? When someone leaves, you change the password and tell the other four. And every one of them spends the whole session with unlimited power, where a mistyped rm has no safety net.

sudo fixes each of those. Each person logs in as themselves, proves who they are with their own password, and runs one command with elevated rights. The rule that allowed it names them. The log names them. Removing their access is one line. And most of the time, they work unprivileged.

How sudo gets its power

sudo is a setuid-root program, which the next lesson explains in detail. In the Fedora 45 image I downloaded, the archive lists it as ---s--x--x root root /usr/bin/sudo: anyone may execute it, nobody may read it, and it starts with root’s identity. Its first act is to read its policy, /etc/sudoers, and decide whether to use that power for you. It authenticates you through PAM, the subject of the PAM lesson; Fedora’s /etc/pam.d/sudo includes system-auth like any login.

Reading a rule

A sudoers rule says who may run what, where, as whom:

alice   ALL = (ALL)       ALL
%wheel  ALL = (ALL)       ALL
deploy  web1 = (www-data) NOPASSWD: /usr/bin/systemctl restart app

The first field is a user or, with %, a group. The second is the host, so one file can be shared across machines. The parenthesised part is the run-as user. Last come the commands, optionally prefixed by tags such as NOPASSWD:.

These are the active rule lines from Fedora 45’s real /etc/sudoers, read from the official container image:

root	ALL=(ALL) 	ALL
%wheel	ALL=(ALL)	ALL

So on Fedora and RHEL, being in wheel means full sudo. Debian and Ubuntu use a group called sudo instead: the Debian image’s /etc/group has sudo:x:27:. That is why usermod -aG sudo alice or usermod -aG wheel alice is the usual way to make someone an admin.

The same file sets Defaults that matter more than they look: env_reset throws away most of the caller’s environment, and secure_path replaces PATH, so a user cannot trick a root command into running their own ls.

Editing safely: visudo

A syntax error in sudoers breaks sudo for everyone, and on a machine with no root password that can be a lockout. visudo(8) exists for this: it “locks the sudoers file against multiple simultaneous edits, performs basic validity checks, and checks for syntax errors before installing the edited file”. Always use it:

sudo visudo                            # edit /etc/sudoers
sudo visudo -f /etc/sudoers.d/deploy   # edit a drop-in
sudo visudo -c                         # check every file, change nothing

Drop-ins in /etc/sudoers.d

Package updates may replace /etc/sudoers, and config management tools prefer to own whole files. So put your rules in /etc/sudoers.d/, which the main file pulls in. Fedora’s file still uses the old spelling, #includedir /etc/sudoers.d, which looks like a comment but is not: sudoers(5) says #include and #includedir “are also accepted” for compatibility, alongside the newer @includedir.

Two rules from the man page trip people up. Files are read in “sorted lexical order”, so 1_whoops loads after 10_second. And sudo reads each file “skipping file names that end in ‘~’ or contain a ‘.’ character”. A file named deploy.conf is ignored without any warning in normal use. Name it deploy or 50-deploy.

Least privilege, carefully

Full ALL is honest: everyone knows it means root. Narrow rules can be worse, because they look safe and are not.

  • Last match wins. The man page: “Where there are multiple matches, the last match is used (which is not necessarily the most specific match).” A broad rule placed after a narrow one overrides it.
  • Shell escapes. Editors, pagers like less, and many tools can spawn a shell, which then runs as root. sudoers(5) describes the NOEXEC tag for this and warns it does not work everywhere. For editing files, grant sudoedit /etc/app.conf instead: it copies the file, runs your editor as you, and writes it back.
  • Arguments matter. /usr/bin/systemctl with no arguments allows every subcommand. Spell out the full command line you mean to allow.
  • Writable targets. Never allow a script that the user can edit. They will edit it.

Checking and logging

sudo -l lists “the privileges for the invoking user … on the current host”. Run it first on any unfamiliar machine. Every sudo invocation is logged with the user, terminal, working directory and command, through syslog or the journal; on a systemd system, journalctl _COMM=sudo shows them. The journald lesson later covers querying logs.

One change to know about: Ubuntu 25.10 made sudo-rs, a Rust reimplementation, the default sudo. It reads the same sudoers format but, according to Canonical’s announcement, does not implement every feature, such as I/O logging. Check sudo --version before relying on an obscure option.

About the video

The embed is Learn Linux TV’s “Linux Crash Course - sudo”, January 2022, from the “sudo overview” chapter to the end (about 23 minutes). Jay LaCroix covers the sudo group, listing privileges, the /etc/sudoers file, editing it with visudo, restricting a user to specific commands, NOPASSWD, and recovering from visudo errors. That matches this lesson closely. It predates sudo-rs, and it spends little time on /etc/sudoers.d and none on the dot-in-filename rule; his December 2025 video in deeper is about the drop-in directory. I judged it from the chapters and description, and English from the original English audio track.

What was run: reading /etc/sudoers, /etc/pam.d/sudo, /etc/group and the archive permissions from the downloaded Fedora and Debian images, on a Mac. Rule semantics are quoted from sudoers(5) (sudo 1.9.18), sudo(8) and visudo(8); the sudo-rs change is from Canonical’s announcement as reported in search results, checked 2026-11-28.

Check yourself

  1. 01You create /etc/sudoers.d/deploy.conf granting a deploy user rights, but sudo -l shows nothing for that user. Why?

    sudo skips files in an included directory whose names contain a dot (or end in ~), to avoid editor and package backup files. Rename it to /etc/sudoers.d/deploy.

  2. 02Why is giving a user sudo rights to /usr/bin/vi /etc/hosts effectively giving them root?

    vi can run shell commands with :!sh, and that shell runs as root. Use sudoedit, which edits a copy as the user, or the NOEXEC tag where it works.

  3. 03A user has both '%admins ALL=(ALL) ALL' and a later 'alice ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart app'. Which applies to that systemctl command?

    The later rule. When several entries match, sudoers uses the last match, not the most specific one, so order matters.

  4. 04What does sudo -l show and why is it the first thing to run on an unfamiliar box?

    The commands the current user is allowed to run with sudo on this host. It tells you exactly what access you have without guessing or triggering failed attempts.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark