Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 2 of 6 · Worked example · 20 min · revision 3

Who may read this file? Users, groups and mode bits

Deconstruct the Linux discretionary access control model: ownership, symbolic and octal mode bits, directory traversal semantics, and permission evaluation rules.

Contentsand lesson path

What you will learn

  • Read and change file ownership and permission bits, and explain why a given user can or cannot access a file.

01. Identities in Linux: Users, Groups, and UIDs/GIDs

Linux decides file access (discretionary access control, or DAC) with numbers, not names:

  • User Identifier (UID): The superuser root is consistently assigned UID 0. System service accounts (such as www-data, systemd-resolve, and postgres) receive low UIDs (typically between 1 and 999 on modern distributions), while interactive human login accounts begin at UID 1000 on most distributions (the UID_MIN setting in /etc/login.defs).
  • Group Identifier (GID): Primary and supplementary group memberships defined in /etc/group allow multiple users to share access to common directories, devices and services. For example, dialout members may open serial ports, docker members may use the Docker daemon's socket, and sudo (on Debian and Ubuntu; wheel on Red Hat) members may run commands as root through the sudoers rules.

The kernel stores and compares only the numbers. When you inspect a file with ls -l, it is ls (a user-space program) that looks up the numeric UID and GID in the account databases (/etc/passwd and /etc/group, or LDAP and similar sources through NSS) to print names. If no name exists for a number, ls prints the number itself, and ls -n always shows the raw numbers. In the listing below, the first column details file type and permissions, followed by links, owner, group, size, timestamp, and filename.

Bash · Inspecting file ownership and mode bits with ls
# Detailed listing of application configuration file
ls -l /opt/app/application.yml
# Sample output:
# -rw-r----- 1 deploy webapps 4096 Oct 27 10:15 /opt/app/application.yml

02. Dissecting Mode Bits: Symbolic Notation and Octal Calculation

The ten-character permission string decomposes into four distinct segments:

  1. File Type (Character 1): Dash - for a regular file, d for a directory, l for a symbolic link, s for a Unix domain socket, and p for a named pipe (FIFO).
  2. Owner / User (u, Characters 2–4): Permissions granted to the specific user account owning the file.
  3. Group (g, Characters 5–7): Permissions granted to accounts that belong to the owning group.
  4. Others / World (o, Characters 8–10): Permissions granted to every other process on the host that is neither the owner nor a member of the owning group.

Each triad consists of three fundamental bits, evaluated numerically in octal (base-8) representation:

  • Read (r): Value 4 (binary 100). Grants viewing of file contents.
  • Write (w): Value 2 (binary 010). Grants modifying or truncating file contents.
  • Execute (x): Value 1 (binary 001). Permits running the file as an executable program or script.

By summing the values within each triad, permissions map to a 3-digit octal number:

  • rwxr-xr-x = (4+2+1)(4+0+1)(4+0+1) = 755 (standard executable script or public directory).
  • rw-r--r-- = (4+2+0)(4+0+0)(4+0+0) = 644 (standard world-readable configuration or document).
  • rw-r----- = (4+2+0)(4+0+0)(0+0+0) = 640 (restricted configuration shared only with a group).
  • rw------- = (4+2+0)(0+0+0)(0+0+0) = 600 (private key or credential file accessible solely by the owner).
Bash · Managing ownership and permissions with chown and chmod
# Reassign file ownership to service user and app group
chown deploy:webapps /opt/app/production.env

# Set strict owner read-write and group read-only using octal mode
chmod 640 /opt/app/production.env

# Set execute bit for owner only using symbolic notation
chmod u+x /opt/app/bin/start-service.sh

# Strip all permissions from others on sensitive directory
chmod o-rwx /opt/app/secrets

03. Worked example: can this user read the file?

The kernel answers "may this process read the file?" with one fixed rule. It compares the process's user ID and groups with the file's owner and group, and picks exactly one class:

  1. If the process's UID is the file's owner, the owner bits apply.
  2. Otherwise, if the file's group is one of the process's groups (primary or supplementary), the group bits apply.
  3. Otherwise, the other bits apply.

Only the bits of that one class are checked. The classes do not add up, and the kernel never moves on to a later class that would allow more. Root (or a process with CAP_DAC_OVERRIDE) skips this check for reading and writing. POSIX ACLs, marked by a + after the mode in ls -l, add named-user entries right after step 1 and named-group entries to step 2.

Now work through one listing. Someone meant to type chmod 640 but typed chmod 040:

----r----- 1 ana ops 19 Nov 20 09:00 budget.csv

Three users want to read it. ana owns the file and is also a member of ops, bob is a member of ops, and carol is in neither group.

  • ana: her UID matches the owner, so step 1 picks the owner bits, ---. She is denied, even though she is in ops and ops may read. The kernel stopped at the first class that matched.
  • bob: he is not the owner, but ops is one of his groups, so step 2 picks the group bits, r--. He can read.
  • carol: neither owner nor in ops, so step 3 picks the other bits, ---. She is denied.

Being the owner still matters: only the owner (or root) may change the mode, so ana can repair the typo with chmod u+rw budget.csv without help. The block below replays the example. It needs root because it creates three users, so run it in a throwaway VM or container (for example docker run --rm -it debian:13 bash), never on a shared server. runuser -u NAME -- COMMAND runs a command as another user; it is part of util-linux and only root may use it (sudo -u NAME COMMAND does the same on a machine with sudo).

Bash · run as root in a throwaway VM or container: one file, three users
groupadd ops
useradd -m -G ops ana
useradd -m -G ops bob
useradd -m carol
mkdir -p /srv/demo && cd /srv/demo
echo 'q4,budget,approved' > budget.csv
chown ana:ops budget.csv
chmod 040 budget.csv          # a typo for 640: the owner bits are now ---
stat -c '%A %U %G %n' budget.csv
for u in ana bob carol; do echo "$u is in: $(id -nG "$u")"; done
for u in ana bob carol; do
  printf '%s: ' "$u"; runuser -u "$u" -- cat budget.csv 2>&1
done
runuser -u ana -- chmod u+rw budget.csv   # the owner may still change the mode
stat -c '%A %U %G %n' budget.csv
printf 'ana: '; runuser -u ana -- cat budget.csv
Expected output (fresh Debian 13 container)
----r----- ana ops budget.csv
ana is in: ana ops
bob is in: bob ops
carol is in: carol
ana: cat: budget.csv: Permission denied
bob: q4,budget,approved
carol: cat: budget.csv: Permission denied
-rw-r----- ana ops budget.csv
ana: q4,budget,approved

The owner ana was refused while bob, with the same group, could read: the owner class matched first, and its empty bits were final. After chmod u+rw the mode is 640 again and all three answers follow the usual pattern (ana reads and writes, bob reads, carol gets nothing). When a permission error makes no sense, ask the same three questions in order: is this process the owner, is it in the group, or is it other?

04. Directory Permissions: Why Execute Means Traversal

A common source of permission errors in DevOps stems from misinterpreting what read, write, and execute mean when applied to directories. In Unix, a directory is simply a special file containing a table mapping filenames to inode numbers:

  • Directory Read (r): Grants the ability to query the directory table entries. A user with read-only permissions can run ls /path/to/dir to see the names of files inside, but cannot read their metadata (file sizes, ownership, timestamps) without the execute bit.
  • Directory Write (w): Together with execute (x), grants the ability to alter the directory table—meaning creating new files, deleting existing files, or renaming files inside that directory. Write without execute is not enough: on a directory with mode 600, rm fails with Permission denied. Note that deleting a file modifies the parent directory, not the file itself. A user with w and x on a directory can delete a file inside it even if the file itself has permissions 000 and is owned by someone else, unless the sticky bit is set on the directory.
  • Directory Execute (x): Known as the Search / Traversal bit. It grants a process permission to traverse (pass through) the directory to access inodes within it or descend into deeper child subdirectories.

05. The File Creation Mask (umask) and Special Permission Bits

A program that creates a file asks for a mode, usually 666 for files and 777 for directories. The kernel then clears every bit that is set in the process's umask (user file creation mask), so the result is mode & ~umask:

  • A default umask of 022 clears (masks out) group write and other write (----w--w-). New files become 644 (rw-r--r--), and new directories become 755 (rwxr-xr-x).
  • A hardened umask of 027 clears all permissions for others and write for group. New files become 640 (rw-r-----), and directories become 750 (rwxr-x---).
  • A strict umask of 077 isolates created files entirely to the owner (600 for files, 700 for directories).

In addition to standard mode bits, Linux provides three Special Permission Bits (represented by a fourth leading octal digit):

  1. Setuid (suid, Octal 4000): When set on an executable binary, any user running the program executes it with the effective UID of the file's owner (e.g. /usr/bin/passwd runs as root to update /etc/shadow).
  2. Setgid (sgid, Octal 2000): When set on a directory, all newly created files and subdirectories automatically inherit the group ownership of that directory rather than the primary group of the creating user. Teams use this for shared directories.
  3. Sticky Bit (Octal 1000): When set on a world-writable directory (such as /tmp, displayed as drwxrwxrwt), it limits deleting or renaming a file inside to the file's owner, the directory's owner and root, preventing unprivileged users from deleting each other's temporary files.

You can check the umask and directory rules yourself as an ordinary user. The block below works in a fresh directory from mktemp -d. The umask change is wrapped in ( ), a subshell, so your own shell keeps its umask. Run it on a normal Linux filesystem: some mounted Windows or network drives fake the mode bits and show 755 for everything.

Bash · umask and directory bits in a scratch directory
cd "$(mktemp -d)"
(umask 027; touch app.log; mkdir reports; stat -c '%a %A %n' app.log reports)
mkdir box && touch box/a.txt
chmod 400 box        # read, but no execute, for the owner
ls -l box
chmod 600 box        # read and write, still no execute
rm box/a.txt
chmod 700 box        # restore, so the scratch directory can be removed
Expected output (run as an ordinary user, not root)
640 -rw-r----- app.log
750 drwxr-x--- reports
ls: cannot access 'box/a.txt': Permission denied
total 0
-????????? ? ? ? ?            ? a.txt
rm: cannot remove 'box/a.txt': Permission denied

The umask 027 turned the requested 666 and 777 into 640 and 750. With read but no execute, ls can list the name a.txt but cannot look up its details, so every field shows ?. With read and write but no execute, rm still fails: changing a directory needs w and x together. Root would succeed in both cases, because root bypasses these checks.

06. Check your understanding

Practice check · 3 questions

Ownership, permissions, and directory traversal verification

Three questions on directory bits, the sticky bit and the umask.

Q1. You have read but not execute permission on a directory (`r--`). What does `ls -l` on that directory show?
Q2. What does the sticky bit (`chmod +t`, as in mode `1777`) add to a world-writable directory such as `/tmp`?
Q3. A program creates `report.csv` and asks the kernel for mode `0640`. The process runs with `umask 0022`. What mode does the file get?
Practice only: not graded, not saved, not evidence.

07. Apply it

Implement these permission hardening guidelines in deployment automation:

  • Enforce Least Privilege for App Accounts: Run application containers and systemd services under dedicated unprivileged users (e.g. User=webapps) with umask 027 to prevent accidental exposure of log data or secrets to world users.
  • Set Shared Directory SGID: When setting up shared directories for build artifacts or team staging, execute chmod 2775 /var/shared to ensure all newly created files inherit the team group.
  • Never Use chmod 777: If an application reports permission errors writing to a socket or directory, adjust group ownership (chown :webapps) and grant group write permissions (chmod 775 or chmod 770) rather than granting world write access.

08. Review prompt

Describe the differences between symbolic permission modes and octal calculations. Explain why the execute bit on a directory represents traversal rather than program execution, and outline the security risks associated with applying chmod 777 to service directories. Detail how the sticky bit safeguards /tmp in multi-user environments.

References

Your progress

Reading state

Sign in to save that you finished reading this lesson.

Sign in to mark reading complete

Prove this skill

8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check