Skip to content
Visual explainer 2h to learn 4 steps

sudo & privilege delegation

Grant scoped administrative rights with sudoers drop-in files edited via visudo.

By Invicly Tech, , in Linux Administration (RHCSA-style). About 10 minutes to read.

Topic 10 of 42

In short

sudo lets approved users run commands as root or another user, with every use logged. Rules live in /etc/sudoers and, preferably, small drop-in files in /etc/sudoers.d edited with visudo, which checks syntax before saving. On RHEL members of wheel get full sudo; on Ubuntu it is the sudo group.

Explained simply

Logging in as root for daily work is risky: one typo can damage the whole system, and nobody can tell who did what. sudo solves both. Normal users run single commands as root by putting sudo in front, and every use is written to the log with their name.

Who may use sudo, and for what, is written in rules. The main file is /etc/sudoers, and extra rules go in small files inside /etc/sudoers.d/. You edit them with visudo, which refuses to save a file with a mistake in it.

The simplest setup is a group with full rights: wheel on RHEL and sudo on Ubuntu. Add a person to that group and they can do anything with sudo. Better practice for teams and automation is to grant only the commands someone really needs, such as restarting one service.

sudo asks for the user's own password, not root's. After a correct password, it remembers you for a few minutes, so a series of commands does not ask again each time.

Press Play on a diagram to walk through it one step at a time.

SequenceWhat happens when you run sudo
User
sudo
sudoers policy
Command
  1. 1sudo systemctl restart httpd
    User to sudo: sudo systemctl restart httpd
  2. 2Is this user allowed this command on this host?
    sudo to sudoers policy: Is this user allowed this command on this host?
  3. 3Allowed; password required
    sudoers policy to sudo: Allowed; password required
  4. 4Prompt for the user's own password
    sudo to User: Prompt for the user's own password
  5. 5Run as root, log to the journal
    sudo to Command: Run as root, log to the journal
FlowHow sudo decides on one request
  1. 1
    Read the policy

    /etc/sudoers plus every file in sudoers.d

  2. 2
    Find matching rules

    User or group, host, run-as user, command

  3. 3
    Last match wins

    A later ! rule can deny an earlier allow

  4. 4
    Password check

    Skipped with NOPASSWD or a recent timestamp

  5. 5
    Run and log

    Clean environment, entry in the journal

In depth

Anatomy of a sudoers rule

A rule reads: who, on which hosts, as whom, with which options, may run what. %ops ALL=(root) NOPASSWD: /usr/bin/systemctl restart httpd means members of group ops (the % marks a group), on any host, may run that exact command as root without a password. The host field matters only when one sudoers file is shared across many machines, so it is usually ALL.

Commands must use full paths. If you list a command without arguments, any arguments are allowed. If you list it with arguments, only exactly those are allowed, and "" means no arguments at all. Several commands are separated by commas, and a command prefixed with ! is excluded.

When more than one rule matches, the last matching one wins. Put broad rules first and specific exceptions after them.

Flow%ops ALL=(root) NOPASSWD: /usr/bin/systemctl restart httpd
  1. 1
    %ops

    Who: any member of group ops

  2. 2
    ALL=

    Which hosts the rule applies on

  3. 3
    (root)

    Run the command as root

  4. 4
    NOPASSWD:

    Optional tag: no password prompt

  5. 5
    /usr/bin/systemctl restart httpd

    Exactly this command and arguments

Drop-in files and the rules visudo enforces

The main /etc/sudoers ends with an include line (written #includedir or @includedir) that reads every file in /etc/sudoers.d. Package updates may replace /etc/sudoers, but they leave your drop-ins alone, so put your rules there: one file per team or purpose.

A surprising rule: sudo skips files in that directory whose names contain a dot or end with ~. So /etc/sudoers.d/ops works, but /etc/sudoers.d/ops.conf is silently ignored. Files should be owned by root with mode 0440; visudo -f sets that for you.

visudo -c checks the main file and every include; visudo -cf /etc/sudoers.d/ops checks one file. If you do break sudo, you need another way in: the root password, a root SSH key or a rescue boot.

  • RHEL: %wheel ALL=(ALL) ALL
  • Ubuntu: %sudo ALL=(ALL:ALL) ALL
  • User_Alias and Cmnd_Alias group names and commands for long rules
  • sudo -l -U alice shows what alice may run

Rules that accidentally give full root

Some commands can start other programs, so allowing them is the same as allowing everything. Editors (vi, vim, nano), pagers (less, more), find with -exec, tar, awk, python and any shell can all spawn a root shell. A rule like alice ALL=(root) /usr/bin/vim /etc/hosts lets alice type :!bash inside vim and become root.

For editing files, grant sudoedit instead: alice ALL=(root) sudoedit /etc/hosts. sudo copies the file, opens the copy in alice's own editor as alice, and writes it back as root afterwards, so the editor never runs as root.

Also avoid wildcards in arguments and scripts that users can modify. A NOPASSWD rule for /home/deploy/run.sh is a root shell for anyone who can edit that file. Keep such scripts root-owned in /usr/local/bin.

  • sudoedit file is the same as sudo -e file
  • The NOEXEC tag stops many programs from starting others
  • Never allow sudo for a script the user can write to

Shells, cached passwords and logs

sudo -i starts a root login shell: it reads root's startup files and moves to /root. sudo -s starts a root shell but keeps much of your environment. su - switches user too, but asks for the target account's password, which is why teams prefer sudo.

After a correct password, sudo remembers you for 5 minutes by default, usually per terminal. sudo -v renews the timer, sudo -k forgets it, and Defaults timestamp_timeout=0 makes sudo ask every time. env_reset clears most environment variables and secure_path sets a fixed PATH, so a user cannot trick sudo with their own PATH.

Every sudo call is logged with the user, terminal, directory and command. Look in the journal with journalctl _COMM=sudo, or in /var/log/secure on RHEL and /var/log/auth.log on Ubuntu.

CompareBecoming root: sudo -i, sudo -s and su -
sudo -i
  • Your own password
  • Root login shell, starts in /root
  • Root's environment
  • Logged under your name
sudo -s
  • Your own password
  • Root shell in your current directory
  • Keeps much of your environment
  • Logged under your name
su -
  • Root's password
  • Root login shell
  • Needs the root password shared
  • Log shows only the switch

Learn it in 4 short steps

About 30m each. Track them with a timer in your plan.

  1. 1
    Add users to wheel or sudo group~30m
  2. 2
    Write drop-ins in /etc/sudoers.d~30m
  3. 3
    Validate syntax with visudo -c~30m
  4. 4
    Restrict commands in a sudo rule~30m

Key points

  • Always edit with visudo or visudo -f; a syntax error can lock out sudo entirely.
  • Rule format: who host=(runas) commands, for example %ops ALL=(root) /usr/bin/systemctl.
  • Prefer granting specific commands over ALL.
  • NOPASSWD is convenient for automation but weakens security.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
sudo visudo -f /etc/sudoers.d/ops
# contents:
%ops ALL=(root) /usr/bin/systemctl restart httpd, /usr/bin/journalctl
sudo visudo -c                # validate all sudoers files
sudo -l -U natasha            # list what natasha may run

A scoped rule for the ops group.

A helpdesk group that can reset passwords, but not root's

The helpdesk team needs to reset user passwords without having full root. Step 1: create a drop-in file with visudo -f, using a name without a dot. Step 2: allow passwd followed by a user name, and then deny any command line that mentions root; the deny comes last so it wins. Step 3: check the syntax. Step 4: list the rule as a helpdesk member and test both cases.

bash
sudo visudo -f /etc/sudoers.d/helpdesk
# contents:
# %helpdesk ALL=(root) /usr/bin/passwd [A-Za-z]*, !/usr/bin/passwd *root*
sudo visudo -c
sudo -l -U kim
sudo -u kim sudo passwd alice     # allowed, asks kim's password first
sudo -u kim sudo passwd root      # Sorry, user kim is not allowed

Let a deploy account run one script without a password

A CI job logs in as deploy and must run /usr/local/bin/deploy-app as root with no prompt. Step 1: make the script owned by root and not writable by anyone else, or the rule becomes a root shell. Step 2: allow only that path with no arguments, using "". Step 3: test non-interactively with sudo -n, which fails instead of prompting. Step 4: confirm that arguments are refused.

bash
sudo chown root:root /usr/local/bin/deploy-app
sudo chmod 755 /usr/local/bin/deploy-app
echo 'deploy ALL=(root) NOPASSWD: /usr/local/bin/deploy-app ""' \
  | sudo tee /etc/sudoers.d/deploy > /dev/null
sudo chmod 440 /etc/sudoers.d/deploy
sudo visudo -cf /etc/sudoers.d/deploy
sudo -u deploy sudo -n /usr/local/bin/deploy-app        # runs
sudo -u deploy sudo -n /usr/local/bin/deploy-app --x    # refused

Quick reference

  • visudo -f /etc/sudoers.d/NAME edits a drop-in; never use a dot in NAME
  • visudo -c checks every sudoers file before you log out
  • sudo -l shows your own rights; sudo -l -U user shows someone else's
  • In sudoers, the last matching rule wins, so put exceptions after general rules
  • cmd "" in a rule means no arguments allowed; no args listed means any args
  • Grant sudoedit /etc/file instead of an editor, which could spawn a root shell
  • sudo -k forgets your cached password; the default cache is 5 minutes
  • journalctl _COMM=sudo shows who ran what with sudo

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1You create /etc/sudoers.d/web.conf with a valid rule, but it has no effect. Why?

Question 2

2Which rule lets alice edit /etc/hosts as root without being able to start a root shell from her editor?

Question 3

3A rule allows /usr/bin/systemctl restart nginx. What happens with sudo systemctl stop nginx?

Question 4

4On Ubuntu, which group gives full sudo rights by default?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Give user natasha full sudo rights on a RHEL-family system and on Ubuntu, using the standard admin group on each. Verify it.

  2. Exercise 2medium

    Allow members of group webops to restart, reload and view the status of httpd only, without a password, and prove they cannot stop it.

  3. Exercise 3hard

    Make sudo ask for a password every time for user alice only, and log every sudo session's terminal input and output for her.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Does sudo ask for my password or root's?
Your own. That is the point: root's password does not have to be shared, and each action is logged under your name.
What if I break /etc/sudoers?
If you used visudo, it will not save a broken file. If it happened anyway, log in as root, use pkexec if polkit is available, or boot into rescue mode and fix the file.
Is NOPASSWD safe?
It is fine for narrow commands used by automation. For broad rules it means anyone who gets into that account becomes root without any extra hurdle.
Why does sudo say command not found for a program in my PATH?
sudo uses its own secure_path instead of your PATH. Use the full path to the program, or install it in a standard directory like /usr/local/bin.

Go deeper

ShareXLinkedInWhatsApp