Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 37 ยท Users, Groups and Permissions

sudo and Privilege Management on Linux

Grant administrative power safely with sudo: groups, visudo, drop-in files in /etc/sudoers.d, command limits, NOPASSWD risks and reading the sudo log.

Running as root all day makes every typo dangerous and every action anonymous. sudo solves both: a normal user runs a specific command with elevated rights, after authenticating, and the action is logged with that user's name. This lesson shows how to configure it without locking yourself out.

Using sudo

Terminal
sudo systemctl restart nginx
sudo -l                       # what may I run?
sudo -u postgres psql         # run as another user
sudo -i                       # root login shell (use sparingly)
sudo -k                       # forget cached authentication

By default sudo asks for your own password and remembers it for a few minutes. Members of a certain group may use it: the group sudo on Debian-family systems, wheel on Red Hat-family ones.

Terminal
sudo usermod -aG sudo dana      # Debian family
sudo usermod -aG wheel dana     # Red Hat family

The sudoers file

The policy lives in /etc/sudoers and files in /etc/sudoers.d/. Always edit with visudo, which locks the file and refuses a syntax error. A broken sudoers can leave nobody able to use sudo.

Terminal
sudo visudo
sudo visudo -f /etc/sudoers.d/webadmins
sudo visudo -c          # check all files

Rule format:

TEXT
who  where = (as_whom)  what
dana ALL=(ALL:ALL) ALL
%developers ALL=(ALL) /usr/bin/systemctl restart myapp
  • who: a user, or a %group.
  • where: the host name (usually ALL).
  • (as_whom): which users and groups the command may run as.
  • what: commands, with full paths.

Least-privilege rules

Instead of ALL, allow only what the role needs:

TEXT
# /etc/sudoers.d/deploy
deploy ALL=(root) /usr/bin/systemctl restart myapp, /usr/bin/systemctl status myapp

Use aliases to keep large files readable:

TEXT
Cmnd_Alias SERVICES = /usr/bin/systemctl restart nginx, /usr/bin/systemctl reload nginx
%webadmins ALL=(root) SERVICES

Files in sudoers.d must have mode 0440 and not contain a . or end in ~ in their names, or they are ignored. Prefer drop-ins to editing the main file.

NOPASSWD removes the password check: deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp. Use it only for narrowly scoped commands, such as for automation. A rule like ALL=(ALL) NOPASSWD: ALL turns every compromised account into a root compromise.

Allowing editors, shells, less, or anything that can launch another program can let a user escape the limit, because those programs can run commands. Allow specific actions, not general-purpose tools.

Logging and auditing

Sudo logs each use through syslog or the journal and, on many systems, to a file.

Terminal
sudo journalctl _COMM=sudo
sudo grep sudo /var/log/auth.log        # Debian family
sudo grep sudo /var/log/secure          # Red Hat family

Failed attempts also appear there, so unexpected entries are worth investigating. Optional features include I/O logging (sudoreplay) for recording sessions.

su versus sudo

su switches user and needs the target user's password; with su - you get a full login environment. sudo needs your password and applies per-command rules. Locking the root password (sudo passwd -l root) and relying on sudo is common on Ubuntu, but keep a tested way to recover the system (console access, rescue mode).

flowchart LR
    A["dana runs sudo cmd"] --> B{"Rule in sudoers?"}
    B -->|No| C["Denied + logged"]
    B -->|Yes| D["Ask password if required"]
    D --> E["Run as target user + log"]

Every sudo call is matched against policy and logged.

A stand-in: a tiny policy checker

This model mimics sudo rule matching with exact commands and groups. It is a simplified stand-in, not the real parser.

Python Stand-in: may this user run it?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A stand-in: summarizing a sudo log

This model reads made-up log lines and counts commands and failures per user, the way you might triage an auth log.

Python Stand-in: triage a sudo log

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Editing /etc/sudoers with a normal editor and saving a syntax error.
  • Using wildcards in command arguments, which can be abused.
  • Giving blanket NOPASSWD: ALL.
  • Forgetting to log out of root shells opened with sudo -i.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which tool should you use to edit sudoers?

    visudo locks the file and validates syntax before saving.
  2. 2. Which password does sudo normally ask for?

    sudo authenticates the caller; su asks for the target's password.
  3. 3. Which group usually grants sudo rights on Red Hat-family systems?

    wheel is the default admin group there; Debian uses sudo.
  4. 4. Why is allowing sudo for vim or less risky?

    Programs that spawn shells hand out full root access.

Summary

  • Prefer sudo per command to a permanent root session.
  • Edit policy with visudo; put rules in /etc/sudoers.d/ with mode 0440.
  • Grant the narrowest commands; avoid blanket NOPASSWD.
  • Review sudo logs for unexpected use.

Next: Package Managers: apt and dnf