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
On this page
- Identities in Linux: Users, Groups, and UIDs/GIDs
- Dissecting Mode Bits: Symbolic Notation and Octal Calculation
- Worked example: can this user read the file?
- Directory Permissions: Why Execute Means Traversal
- The File Creation Mask (umask) and Special Permission Bits
- Check your understanding
- Apply it
- Review prompt
Lessons of Linux CLI
- Finding your way around the filesystem: globs, nano, vim and sudoedit Concept · 20 min
- Who may read this file? Users, groups and mode bits Worked example · 20 min
- Answering questions from an access log with pipelines Lab · 25 min
- Processes, resource usage, signals and the environment a program sees Concept · 20 min
- Remote work over SSH: keys, tar archives, scp and rsync Lab · 25 min
- Check: diagnose a misbehaving host from the shell Knowledge check · 8 questions
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
rootis consistently assigned UID0. System service accounts (such aswww-data,systemd-resolve, andpostgres) receive low UIDs (typically between1and999on modern distributions), while interactive human login accounts begin at UID1000on most distributions (theUID_MINsetting in/etc/login.defs). - Group Identifier (GID): Primary and supplementary group memberships defined in
/etc/groupallow multiple users to share access to common directories, devices and services. For example,dialoutmembers may open serial ports,dockermembers may use the Docker daemon's socket, andsudo(on Debian and Ubuntu;wheelon 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.
# 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.yml02. Dissecting Mode Bits: Symbolic Notation and Octal Calculation
The ten-character permission string decomposes into four distinct segments:
- File Type (Character 1): Dash
-for a regular file,dfor a directory,lfor a symbolic link,sfor a Unix domain socket, andpfor a named pipe (FIFO). - Owner / User (
u, Characters 2–4): Permissions granted to the specific user account owning the file. - Group (
g, Characters 5–7): Permissions granted to accounts that belong to the owning group. - 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): Value4(binary100). Grants viewing of file contents. - Write (
w): Value2(binary010). Grants modifying or truncating file contents. - Execute (
x): Value1(binary001). 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).
# 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/secrets03. 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:
- If the process's UID is the file's owner, the owner bits apply.
- Otherwise, if the file's group is one of the process's groups (primary or supplementary), the group bits apply.
- 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 inopsandopsmay read. The kernel stopped at the first class that matched. - bob: he is not the owner, but
opsis 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).
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----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,approvedThe 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 runls /path/to/dirto 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 mode600,rmfails withPermission denied. Note that deleting a file modifies the parent directory, not the file itself. A user withwandxon a directory can delete a file inside it even if the file itself has permissions000and 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
022clears (masks out) group write and other write (----w--w-). New files become644(rw-r--r--), and new directories become755(rwxr-xr-x). - A hardened umask of
027clears all permissions for others and write for group. New files become640(rw-r-----), and directories become750(rwxr-x---). - A strict umask of
077isolates created files entirely to the owner (600for files,700for directories).
In addition to standard mode bits, Linux provides three Special Permission Bits (represented by a fourth leading octal digit):
- Setuid (
suid, Octal4000): 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/passwdruns as root to update/etc/shadow). - Setgid (
sgid, Octal2000): 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. - Sticky Bit (Octal
1000): When set on a world-writable directory (such as/tmp, displayed asdrwxrwxrwt), 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.
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 removed640 -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 deniedThe 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
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) withumask 027to 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/sharedto 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 775orchmod 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.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.