How-to

The sudo Command and Its Rules

sudo lets named users run chosen commands with another account's rights, usually root, and records who did what. It is the backbone of shared server administration.

What does sudo do?

It runs a single command as another user, root by default, after checking a policy in /etc/sudoers and usually asking for your own password. Every call is logged. Compared with logging in as root, sudo gives you three things: a record of who ran each privileged command, rules that limit what each person may do, and a habit of thinking before each privileged action because you have to type the word.

Which sudo forms do I use daily?

CommandDoes
sudo apt upgradeRun one command as root
sudo -u postgres psqlRun as another user
sudo -iRoot login shell with root's environment
sudo -sRoot shell keeping much of your environment
sudo -lList what you are allowed to run
sudo -e /etc/hostsEdit a file safely as root with your own editor
sudo -kForget the cached password now
sudo !!Shell trick: repeat the last command with sudo

sudo -e, also called sudoedit, deserves more use. It copies the file to a temporary location, opens your editor as you, and writes it back as root. Your editor plugins and configuration never run with root rights.

Why does sudo not remember my password forever?

sudo caches a successful authentication for 15 minutes per terminal by default, then asks again. Change it with Defaults timestamp_timeout=5 in a sudoers drop-in; zero asks every time. Ubuntu and Debian keep the cache per terminal, so a second SSH session asks separately, which is a sensible default.

Who decides who may use sudo?

The sudoers policy. Distributions grant full rights to one admin group: sudo on Ubuntu and Debian, wheel on Rocky Linux, AlmaLinux and CentOS Stream. Adding a user to that group is the normal way to make an admin; see Create a sudo user on Debian and Sudo user on CentOS Stream, Rocky, Alma. For anything narrower, write a drop-in file in /etc/sudoers.d/ instead of editing the main file:

visudo -f /etc/sudoers.d/30-support
# contents:
Cmnd_Alias WEBOPS = /usr/bin/systemctl reload nginx, /usr/bin/systemctl restart php-fpm, /usr/bin/journalctl -u nginx *
%support ALL=(root) WEBOPS

Members of the support group may now reload nginx, restart PHP-FPM and read nginx logs as root, and nothing else. visudo checks syntax before saving, which protects you from locking everyone out of sudo with a typo.

What makes a sudoers rule unsafe?

  • Commands that open shells: allowing vim, less, find, awk or tar as root lets users escape to a root shell. Use sudoedit for editing.
  • Wildcards in arguments: /usr/bin/systemctl restart * also allows restarting sshd with odd options or any unit.
  • Scripts the user can edit: if a user may run /opt/tools/fix.sh as root and can also write that file, they have full root.
  • Broad NOPASSWD: ALL=(ALL) NOPASSWD: ALL on a human account turns any stolen session into instant root.

How do I see who ran what?

sudo writes every attempt to the system log, successful or denied:

journalctl _COMM=sudo --since today
grep 'COMMAND=' /var/log/auth.log | tail -20      # Debian, Ubuntu with rsyslog
grep 'COMMAND=' /var/log/secure | tail -20         # Rocky, AlmaLinux

Each line shows the user, the terminal, the working folder and the exact command. For full session recording, sudo's log_output option stores everything typed and shown, readable later with sudoreplay.

Is sudo itself kept up to date?

It should be. sudo runs with root rights on every call, so its own bugs matter; a notable one, nicknamed Baron Samedit, was fixed in early 2021. Security updates through apt or dnf cover it, and automatic security updates keep it current without effort. There is also a newer implementation, sudo-rs, written in Rust, which Ubuntu adopted as the default starting with release 25.10; the commands and sudoers format stay the same.

How is sudo different from su?

sudo uses your own password and grants per-command rights; su switches to another account using that account's password. The full comparison is in sudo vs su. On a server shared by more than one admin, sudo is the right default.

Keep Reading

Create a sudo user on Debian

Add an admin account on Debian 13 or 12, give it sudo rights, install your SSH key and retire daily root logins safely.

Open

sudo vs su

Two ways to act as root: su switches accounts with the target password, sudo grants logged, per-command rights with yours.

Open

SSH root login settings

What each PermitRootLogin value does, how to move daily work to a sudo user and when key-only root login is reasonable.

Open

Linux file permissions

Read ls -l output, understand owner, group and other bits, umask defaults, ACLs and why root and SELinux sit on top of it all.

Open

FAQ

Quick Answers

Run sudo -l. It lists the rules that apply to your account.

15 minutes per terminal by default. Change it with Defaults timestamp_timeout in a sudoers drop-in.

Use sudo -e or sudoedit, which edits a temporary copy as you and writes it back as root.

In the system journal, readable with journalctl _COMM=sudo, and in auth.log or secure when rsyslog is installed.

Create a drop-in with visudo -f in /etc/sudoers.d listing only the exact commands they need.