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

Users, Groups, UIDs & GIDs

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

Users are numbers with names attached

Every process on Linux carries a user ID, usually written UID, and every file records the UID of its owner. When a process asks to read or write a file, the kernel compares the process UID with the owner recorded on the file. The kernel never compares names. The name you see in your prompt is a label that libraries and tools look up from the number, and that lookup happens in user space, outside the kernel.

UID 0 belongs to the superuser, which is called root. The kernel gives root a special status: root bypasses most of the permission checks that this module describes. Every other UID is an ordinary account. System services usually run under their own UIDs with no login shell, so a flaw in one service cannot quietly change the files that belong to another account. Keeping accounts separate is the basic reason the permission system exists.

Groups follow the same pattern. Each group has a numeric group ID, or GID. A file has one owner and one group. A running process has an effective GID, plus a list of supplementary group IDs. When the kernel checks the group bits of a file, it compares them against all of those values, so membership in a group can grant access even when the group is not your primary group.

The password and group files

On a traditional Linux system, the mapping between names and numbers lives in two plain text files. The file /etc/passwd has one line per account, with seven colon-separated fields: the login name, a placeholder for the password, the UID, the primary GID, a comment, the home directory, and the login shell. The password hash itself is kept in a separate file that only root can read, which is why the second field usually contains only an x.

The file /etc/group has one line per group, with four fields: the group name, a placeholder, the GID, and a comma-separated list of member names. Membership in that list is what makes a group supplementary. The primary group is the one named in the passwd file, and it is the group that new files usually receive. Reading the two files is harmless, and it is the quickest way to see how a name turns into a number.

BASH1 lines
grep "^root:" /etc/passwd

On most systems the line for root begins with root:x:0:0. That shows the name root maps to UID 0 and GID 0. The two zeros are the reason root is special: the kernel only needs the number to decide what the process may do.

Asking the shell who you are

The id command prints your identity on one line. Each number is followed by its name in parentheses. The format is always the same, which makes the output easy to scan in a script or in a bug report. The fields are uid for your real and effective user, gid for your effective primary group, and groups for every group the process carries.

BASH4 lines
id
id -u
id -un
id -G

The first command prints the full line. The next three print only parts of it. The -u option prints the numeric UID, -un prints the user name, and -G prints every group number on one line. Scripts usually use the numeric form, because a name can be ambiguous when two directories disagree, while a number is what the kernel actually checks.

Note

The lab has no man pages installed. Use id --help to see the options of this command, and the online pages at man7.org for the system calls that the kernel uses to report identity.

Groups and shared access

Groups let several accounts share files without giving everyone full access. Suppose a team keeps a directory of documents. The owner can be a team lead, the group can be called docs, and the group bits can allow read and write. Each member who is listed in the group file receives those bits without any change to the files themselves. Adding a person to the team becomes one line in /etc/group instead of an edit to every file.

Membership is read when a login session starts. A shell that was already open before a membership change keeps its old group list, and the new group does not appear in that shell until you log in again. This surprises many people, and it is the first thing to check when a new group does not seem to work. The new list is visible in a fresh session with id -Gn.

Only root can change memberships, because only root can edit /etc/group. As an ordinary user you can list your groups and check which group a file belongs to, but you cannot grant yourself a new one. That limit is deliberate, because group membership grants access to files.

What a new file receives

When a process creates a file, the kernel sets the owner of the new file to the effective UID of the process. The group is set to the effective GID, unless the parent directory has the setgid bit set, a case covered in a later unit of this module. For now the rule is simple: your new files belong to you and to your primary group.

BASH2 lines
touch demo.txt
stat -c "%U %G %u %g" demo.txt

The format string prints the owner name, the group name, the owner UID, and the group GID. Read the four values in order, and check that the names match the ones id printed earlier. If the names match but the numbers look unfamiliar, that is normal, because the numbers are assigned when the account is created.

Why the numbers matter

Because the kernel only uses numbers, a name in a file is never proof of anything by itself. Scripts that compare owner names are fine for reports, but security decisions should compare numeric IDs. The stat command with the %u and %g formats gives you those numbers directly.

Root is the one account the rest of the system treats as trusted with everything. A later unit explains how an ordinary account reaches root through su and sudo. The safe habit, which that unit describes, is to run one command as root when it is needed, rather than to work in a root shell all day. Knowing that the kernel sees only numbers is what makes that habit make sense.

Hands-on: read your own identity

Run id, then run stat on a file you create. Compare the owner and group values with the output of id. Then find the line for root in /etc/passwd and confirm that its UID is 0. The labs at the end of this unit ask you to save these values in files in your workspace, so the checks can confirm each step.

How the kernel decides who may access a file

Visualizer

The kernel picks one class (owner, group or other) and uses only that class’s bits. Directories read r, w and x differently, and the sticky bit limits who can delete.

1 / 4
-type
-owner
-
-
rgroup
w
x
-other
-
-
process
uid 1000, gid 1000
file
owner 1000, group 1000
mode
070

The kernel knows the process’s uid and gid, and the file’s owner and group. It picks exactly one class for the check and tries them in order: owner, group, other.

cd "$(mktemp -d /tmp/perm.XXXX)" && touch own.txt && chmod 070 own.txt && cat own.txt; echo "status $?"; stat -c "%a %A" own.txt
Terminal lab · 3 steps · real Linux in your browser

Users, Groups, UIDs & GIDs

Read your own identity from the shell and save each value in a file in /workspace, so every step can be checked.

01
Record your numeric UID and name
Run id -u to print your numeric user ID, and id -un to print your user name. Save both values in /workspace/identity.txt. The number goes on line 1 and the name goes on line 2.
Show hint for step 1
Write the first line with > and append the second line with >>.
02
List your group IDs
Run id -G to print every group number that your process carries. Write each number on its own line in /workspace/groups.txt.
Show hint for step 2
id -G separates the numbers with spaces. Use tr to turn each space into a newline.
03
Check the owner of a file you create
Create an empty file named /workspace/owned.txt with touch. Write its owner name to /workspace/owner.txt using stat -c %U owned.txt.
Show hint for step 3
A new file belongs to your effective UID, so the name should match id -un.
Check yourself Question 1 of 4
Concept Check What the kernel compares recall

When a process asks to access a file, what does the kernel compare to decide the outcome?

Recap & Key Takeaways

The kernel checks numeric UIDs and GIDs, and names are only labels that tools look up from /etc/passwd and /etc/group.
UID 0 is root, and the kernel lets root bypass most permission checks, so ordinary work should run under a normal account.
A new file belongs to your effective UID and effective GID, and group membership is read when a login session starts.
Use id, id -u, id -un, id -G, and stat -c with %U, %G, %u, and %g to read identity values directly.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue