Linux Users, Groups and sudo Explained
A setup guide tells you to type sudo before a command, and the terminal asks for your password, even on your own laptop. Without it, the command is refused. This lesson explains users, groups and sudo: who owns what, how teams share files, where the lists live and how to borrow administrator rights safely.
Users: every file and process has an owner
A user is an account, a name the system knows, such as anita. Behind each name is a number, the user ID (UID); the kernel, the core of the operating system (lesson 2), works only with the number. A file stores its owner's UID, and ls -l (lesson 6) looks up the name.
Every process, a running program (lesson 3), also runs as a user. Your shell (lesson 4) runs as you, and so does every command you start from it, with your rights and no more.
One account is special: root, the administrator, with UID 0. The kernel skips nearly all permission checks for UID 0, so root can read any file and change any setting. On Linux, accounts for people usually start at UID 1000; most lower numbers are system accounts, which run background services, so a service that is broken into does not hand over root.
Groups: shared access without shared passwords
A group is a named set of users who share access, with its own number, the GID (group ID). A team might use a developers group for its project files; members of docker may use Docker, a tool for packaging and running apps, without sudo.
A user can be in several groups: one primary group, usually given to the files they create, plus any number of supplementary groups. On most Linux systems the primary group is a private one named after the user. Follow the arrows in the diagram.
ravi, in both groups, can edit app.py and read deploy.log. anita, only in developers, cannot open deploy.log, but notes.txt is anita's alone.
Administrators create accounts with useradd and groups with groupadd, and add someone to a group with sudo usermod -aG developers ravi. Without the -a (append), -G replaces ravi's supplementary groups, removing every one not named. The change applies at ravi's next login, because running programs keep the groups they started with.
Where Linux keeps the lists
Accounts live in plain text files in /etc, the configuration folder (lesson 5). /etc/passwd has one line per account with seven fields separated by colons: name, password field, UID, primary GID, a comment such as the full name, home folder and login shell. Every user can read it, because programs need it to turn UIDs into names.
An illustrative line: anita:x:1001:1001:Anita Rao:/home/anita:/bin/bash. The x means the password is kept elsewhere. /etc/group lists each group's name, a rarely used password field, its GID and its supplementary members, as in developers:x:1003:anita,ravi.
Passwords live in /etc/shadow, which regular users cannot read. Even there, the system stores only a hash, a scrambled fingerprint of the password that cannot be run backwards (Encryption vs Hashing). At login, the system hashes what you type and compares.
sudo: root's power for one command
Working as root all day is risky: every typo, and every program you start, would have full power. sudo (often read as "superuser do") instead runs one command as root, after you prove who you are with your own password, not root's.
The rules live in /etc/sudoers. On Ubuntu, %sudo ALL=(ALL:ALL) ALL reads: members of the group sudo (% marks a group), on any machine, as any user and group, may run any command. Fedora and Red Hat use the group wheel, macOS uses admin. Edit the file only with sudo visudo, which checks your change first, because a broken sudoers file can stop sudo working.
The flowchart follows sudo systemctl restart nginx, which restarts the nginx web server (systemctl comes in lesson 12). Notice where the failures end up.
After a correct password, sudo remembers you in that terminal for a few minutes (5 by default; systems can change this). While you type the password, classic sudo shows nothing; Ubuntu 25.10 and 26.04 use a newer sudo that shows a star per key.
su (switch user) instead opens a new shell as another user, root by default, and asks for that user's password. macOS disables root and Ubuntu locks root's password, so there su to root fails by design.
The table sums up the five commands.
| Command | What it shows or does |
|---|---|
whoami | your user name |
id | your UID, primary group and all groups, with numbers |
groups | your groups, names only |
su | a shell as another user, with their password |
sudo | one command as root, with your password, if sudoers allows |
Good habits with root and sudo
The guiding rule is least privilege (lesson 6): every user and program gets the smallest set of rights that does the job. Work as yourself, and add sudo only to the one command that needs it.
Avoid logging in as root; a later lesson shows how to switch off root login over SSH (Secure Shell, for logging in to remote machines). With named accounts and sudo, the log says who did what.
Read a command before putting sudo in front of it, especially one pasted from a website: rm on the wrong path then deletes system files instead of stopping at Permission denied. Powerful groups need care too: Docker's documentation warns that the docker group grants root-level privileges.
Check yourself
ravi was just added to the docker group, but groups in an already open terminal does not list docker. Why?
Groups are read at login, and a running shell keeps the groups it started with. After logging out and back in, the new shell includes docker.
The account anita is renamed anita2 but keeps UID 1001. Whom does ls -l now show as the owner of its old files?
anita2. Files store the owner's UID, not the name, and ls -l looks up whichever name goes with 1001 in /etc/passwd today.
A team could share root's password or give each person sudo. Why is sudo better?
Each person uses their own password, so the log names the person behind each command and rules can differ per person. Removing someone does not mean changing a shared password.