$_ CLI In Depth
Learning progress 0%
lesson permissions users sudo

sudo, su & Working as Root Safely

5 min read Module 5: Users, Permissions & sudo Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:21

Root is UID 0, not a special login

Root is an account with UID 0. It is not a separate kind of system, and it has no special login path that bypasses the kernel. Its power comes from the kernel rule that UID 0 bypasses the discretionary permission checks described in this module. A process running as UID 0 may read any file, write any file, and change ownership of any file, regardless of the mode bits.

Because the power comes from the UID, the danger comes from the UID as well. A shell that runs as root will carry out any command you type, including a mistyped one. A single wrong path in a recursive removal can delete a system. That is why most administrators avoid working as root, and why the tools in this unit exist.

su: become another user

The su command starts a shell as another user. Without arguments it asks for the password of root and starts a root shell. With a name, such as su alice, it asks for alice password. The option - starts a login shell with that account environment. The new shell has the full power of the target account until you exit it.

Because su asks for the target password, it is useful when no other method is available, such as on a minimal system where sudo is not installed. The drawback is that the shell lasts as long as you let it run, and everything you do in it runs with that identity. Typing exit returns you to your own account.

sudo: run one command as root

The sudo command runs a single command as another user, and by default that user is root. It asks for your own password, not the password of the target account. It checks a policy file that says whether your account may run that command, and it records the command in the system log. When the command finishes, you are back in your own account.

BASH2 lines
sudo systemctl restart ssh
sudo -l

The first line is an example of a single privileged action. The second, sudo -l, lists the commands that the policy allows your account to run, without running them. Checking that list before you need it saves time and avoids a surprise. Neither line is runnable in this lab, because sudo is not installed here. The point is the pattern, not the particular service.

Tip

Prefer sudo over su on a system that has it. Each sudo call is logged and limited to one command, so the audit trail shows exactly what was run with elevated rights.

sudoers and visudo

The sudo policy lives in a file called sudoers, usually at /etc/sudoers, plus optional files in a directory alongside it. Entries say which users or groups may run which commands, and whether a password is needed. The file is read by root only. Editing it with an ordinary text editor is risky, because a syntax error can lock every administrator out of sudo.

The visudo command edits the policy file safely. It locks the file during the edit and checks the syntax before saving. If the check fails, it warns you and lets you fix the mistake. Always use visudo for sudoers changes, and never write the file directly.

Working safely

A few habits prevent most damage. Run a single command with sudo instead of opening a root shell. Read the full command before you press Enter, and check every path in it, because a wrong path with sudo runs as root and cannot be undone by the permission system. Avoid giving broad access to make an error go away, such as recursive chmod 777, because that replaces one problem with a larger one.

Ask whether the task needs root at all. Many tasks that seem to require root, such as editing a file in your home directory, only need the correct owner or group. The earlier units give you the tools to check that. Root is for the few tasks that truly need it, and those are the tasks that deserve the extra care.

Setuid programs reach root in a controlled way. Programs such as passwd and sudo are setuid root, so they can perform a narrow job as root and then return control to you. The narrowness is the important part. A program that does one thing and checks its input is safer than a shell that does everything.

The sudo policy can also limit which environment variables a command receives and which directory it runs in. Those options matter for scripts that run as root, because a variable that a normal user controls can change how a tool behaves. The safest policy names exact commands and exact arguments, and it avoids granting a shell or an editor, since those can run any other command on request.

Some administrators put sudo rights on a group rather than on single accounts. Members of the group may run the allowed commands after entering their own password, and other users cannot. The grant lives in the policy file, so removing a user from the group takes the right away the next time that user runs sudo. Keep the membership list short, and review it when people change roles.

Hands-on: check where you stand

Run id -u to confirm that your process is not UID 0. Then check whether sudo is installed with command -v sudo. The lab for this unit asks you to record both results in files in your workspace. The checks read the actual state of the shell, so the answers match this environment, which has no sudo and no root access.

Terminal lab · 2 steps · real Linux in your browser

sudo, su & Working as Root Safely

Check whether your shell runs as root, and whether sudo is installed, and save both answers in files.

01
Confirm you are not root
Test your effective UID with id -u. Root has UID 0. Save the word root or not-root in /workspace/who.txt, depending on what the test shows.
Show hint for step 1
Use an if statement around the id -u comparison, and write the result with > to the file.
02
Check whether sudo is installed
Use command -v sudo to test whether the sudo program is on your PATH. Discard its output. Save the word present or absent in /workspace/sudo.txt, depending on the result.
Show hint for step 2
command -v prints nothing and returns a non-zero status when the name is not found.
Check yourself Question 1 of 4
Concept Check The root UID recall

Which UID does the root account have?

Recap & Key Takeaways

Root is UID 0, and the kernel lets it bypass the normal permission checks, so every root command carries full risk.
su asks for the target account password and gives a shell that lasts until you exit, while sudo asks for your own password and runs one command.
Edit sudoers only with visudo, because it checks the syntax before saving and protects against a lockout.
Prefer single commands under sudo, read every path first, and never fix a permission error with mode 777.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue