Linux › Section B · Basic commands and the directory hierarchy › Topic B.14
Running commands as the superuser
Administration needs root's power, but working as root all the time is risky and leaves no trail. sudo lets chosen users run individual commands as root, with their own password and a log of every use, and its rules live in one carefully edited file.
Why not just log in as root?
The superuser, root (user ID 0), bypasses ordinary permission checks, so one mistyped command as root can delete or overwrite anything. Working in a root shell all day makes such mistakes more likely and has other drawbacks. There is no record of which commands changed the system, and on a shared machine no record of who ran them. You lose your own shell settings, and everyone who administers the machine must share the root password. The safer practice, an example of least privilege, is to work as yourself and raise your privileges for single commands only when needed. Many distributions, Ubuntu among them, lock the root password entirely for this reason.
sudo in everyday use
Put sudo in front of a command to run it as root: sudo apt update on Debian or Ubuntu, sudo dnf upgrade on Fedora. sudo asks for your own password, not root's, then remembers that you authenticated for a few minutes, five by default in classic sudo and 15 in sudo-rs, so a run of commands needs only one prompt. sudo -k forgets the cached authentication at once. sudo applies to one command; redirections on the same line are still performed by your own shell, which is why a command such as sudo echo text > /etc/motd fails, as Shell input and output: redirection and pipes explained. Ubuntu 25.10 replaced the classic sudo with sudo-rs, a Rust rewrite that keeps the same commands and reads the same sudoers files, though it supports only the commonly used subset of options.
Useful sudo options
sudo -u ana command runs a command as the user ana instead of root, which is handy for testing another account's access. sudo -l lists what you are allowed to run. sudo -i starts a login shell as root, and sudo -s starts a shell using your SHELL variable; use these sparingly, for a burst of related work, and type exit as soon as you are done. sudoedit file, also written sudo -e file, is the safe way to edit a protected file: sudo copies it, opens the copy in your normal editor running as you, and writes it back as root only if you changed it.
Who may use sudo
sudo reads its rules from /etc/sudoers. Distributions ship a rule that gives full access to one administrators' group: the group is called sudo on Debian and Ubuntu, and wheel on Fedora, RHEL and their relatives. The installer usually puts the first user in that group. To make another user an administrator, add them to the group, for example sudo usermod -aG sudo ana on Debian or sudo usermod -aG wheel ana on Fedora, as User management files explains in detail. Group changes take effect at the next login. A user who is not authorised sees a message saying they are not in the sudoers file, and the attempt is logged.
Reading a sudoers rule
Each rule says who may run what, where and as whom: who host=(runas user:runas group) commands. The default Debian rule is %sudo ALL=(ALL:ALL) ALL. The % means the rule applies to a group rather than a user; the first ALL means on any host, which matters only when one file is shared by many machines; (ALL:ALL) means the command may run as any user and any group; the final ALL means any command. Rules can be narrow instead: backup ALL=(root) /usr/bin/rsync allows just one program, given by its full path. The NOPASSWD: tag before a command list skips the password prompt, which is convenient for automation but should be used rarely.
Edit with visudo
Never open /etc/sudoers in an ordinary editor. A single syntax error can stop sudo working for everyone, and if root has no password you may then have no way to fix it. Run sudo visudo instead. It locks the file against simultaneous edits, opens a temporary copy in an editor, checks the syntax when you save, and refuses to install a broken file, offering to let you edit again. sudo visudo -c checks the current files without changing anything. Most systems also read every file in /etc/sudoers.d, through an include line at the end of /etc/sudoers. Files there whose names contain a dot or end in ~ are skipped.
Worked example: one command for one user
A user called deploy must restart the web service but should have no other root access. 1. Create a separate rules file, so package updates to /etc/sudoers cannot clash with your change: sudo visudo -f /etc/sudoers.d/deploy. 2. Add one line: deploy ALL=(root) /usr/bin/systemctl restart nginx. 3. Save; visudo checks the syntax before installing the file. 4. Note the file name contains no dot, so sudo will read it; a name such as deploy.conf would be silently ignored. 5. Test as that user: sudo -l should list exactly that command. 6. Check that sudo systemctl stop nginx is refused, since the rule allows only the exact command given.
su: switching users
su, short for substitute user, starts a shell as another user, root by default. Unlike sudo, it asks for the target user's password, so su to root needs the root password, and it gives you a whole shell rather than one command. su - (or su -l) starts a login shell, with the target user's environment and home directory, which avoids confusing leftovers from your own environment. On systems where root has no password, su to root fails, and sudo -i is the way to get a root shell. Because su needs a shared password and records less, sudo is preferred for administration.
Worked example: reading the sudo log
A configuration file changed overnight and you want to know who used sudo. 1. On a systemd system, ask the journal for sudo's messages: journalctl SYSLOG_IDENTIFIER=sudo. 2. Narrow the time window with --since, as in journalctl SYSLOG_IDENTIFIER=sudo --since yesterday. 3. Each entry records the user, the terminal, the working directory, the target user and the full command; an illustrative line reads ana : TTY=pts/0 ; PWD=/home/ana ; USER=root ; COMMAND=/usr/bin/vim /etc/hosts. 4. On systems that also keep text logs, look in /var/log/auth.log on Debian and Ubuntu or /var/log/secure on Fedora and RHEL. 5. Failed attempts by unauthorised users appear in the same places.
Common mistakes to avoid
First, editing /etc/sudoers directly instead of with visudo. Second, naming a file in /etc/sudoers.d with a dot, such as admins.conf, so it is ignored without warning. Third, granting ALL with NOPASSWD to make a script work, when one specific command would do. Fourth, staying in a sudo -i shell longer than necessary. Fifth, expecting sudo to apply to a redirection on the same line. Sixth, adding a user to the sudo or wheel group and testing in the same session before they have logged in again.
Key takeaways
Work as yourself and use sudo for individual commands; it asks for your own password, caches it for a few minutes and logs every use. sudo -u runs as another user, -l lists your rights, -i and -s start shells, -k forgets authentication and sudoedit edits files safely. Full access usually comes from membership of the sudo group (Debian, Ubuntu) or wheel (Fedora, RHEL). Rules read who host=(runas) commands, with % for groups. Always edit with visudo, and put local rules in /etc/sudoers.d, in file names without dots. su needs the target's password. journalctl SYSLOG_IDENTIFIER=sudo shows the log.
Flashcards
Flashcard
The superuser, user ID 0, which bypasses permission checks
Test your understanding
Question 1 of 5
Whose password does sudo ask for by default?
Question 2 of 5
You save a new rule in /etc/sudoers.d/admins.conf with visudo, but sudo ignores it. Why?
Question 3 of 5
What does the rule backup ALL=(root) /usr/bin/rsync allow?
Question 4 of 5
Why should /etc/sudoers be edited with visudo rather than an ordinary editor?
Question 5 of 5
Ana was just added to the wheel group on a Fedora server, but sudo still refuses her in her open session. What is the most likely fix?
