TExcelTAD
Home › Linux & Bash › The Permissions Model

The Permissions Model

Linux is a multi-user system: many users, services and programs share one machine, and the permission model decides who may do what with each file. The chmod lesson showed the commands; this lesson explains what the permissions mean, how they behave on directories, and the special bits you'll meet on real systems.

Three classes, three permissions

Every file has an owner (a user) and a group. When a process accesses the file, the kernel checks exactly one class, in this order:

  1. If you are the owner, the owner (u) bits apply.
  2. Else, if you are in the file's group, the group (g) bits apply.
  3. Otherwise the others (o) bits apply.

The root user (UID 0) bypasses the read/write checks entirely.

bash · try it yourself
printf 'salary data\n' > payroll.txt
chmod 604 payroll.txt
stat -c '%A owner=%U group=%G' payroll.txt
cat payroll.txt
-rw----r-- owner=learner group=learner
salary data

604 gives the owner rw-, the group nothing, and others r--. Because we are the owner, only the owner bits count; the classes are not combined.

What r, w, x mean: files vs directories

Bit On a file On a directory
r (4) Read the content List the names inside (ls)
w (2) Change the content Create, delete and rename entries inside
x (1) Run it as a program Enter it (cd) and access files inside by name

The directory rules surprise people. Deleting a file is a change to the directory, so it depends on the directory's w bit, not on the file's own permissions:

bash · try it yourself
mkdir shared
echo "keep me?" > shared/readonly.txt
chmod 444 shared/readonly.txt
echo "draft" > shared/readonly.txt 2>/dev/null || echo "cannot write the read-only file"
rm -f shared/readonly.txt && echo "but deleting it worked: the directory is writable"
/w/main.sh: line 4: shared/readonly.txt: Permission denied
cannot write the read-only file
but deleting it worked: the directory is writable

And r without x (or x without r) on a directory gives strange half-access:

bash · try it yourself
mkdir -p vault
echo "the code is 1234" > vault/secret.txt
chmod 300 vault
ls vault 2>&1
cat vault/secret.txt
chmod 500 vault
ls vault
chmod 700 vault
ls: cannot open directory 'vault': Permission denied
the code is 1234
secret.txt

With -wx (3), you can't list vault, but if you know a file's name you can still open it. With r-x (5) listing works again. Real-world use: a web server's directory might grant x but not r, so visitors reach known files but can't browse the folder.

The umask

New files don't start with arbitrary permissions. Programs usually request 666 for files and 777 for directories, and the umask removes bits from that. The common umask 022 removes write for group and others:

bash · try it yourself
umask
touch default.txt
umask 077
touch private.txt
umask 002
touch team.txt
stat -c '%a %A %n' default.txt private.txt team.txt
0022
644 -rw-r--r-- default.txt
600 -rw------- private.txt
664 -rw-rw-r-- team.txt
umask New files New directories Typical for
022 644 755 Most systems
002 664 775 Shared group projects
077 600 700 Private, security-sensitive accounts

Execute permission is never added by the umask: a new file only becomes executable with chmod +x.

Special bits: setuid, setgid, sticky

A fourth octal digit in front holds three special bits:

Bit Value Shows as Effect
setuid 4000 s in owner's x spot A program runs with its owner's privileges
setgid 2000 s in group's x spot Program runs with the file's group; on a directory, new files inherit its group
sticky 1000 t in others' x spot In a shared directory, only a file's owner may delete it

All three exist on a normal system:

bash · try it yourself
stat -c '%a %A %U %n' /usr/bin/passwd /tmp /etc/shadow
echo "---"
cat /etc/shadow
4755 -rwsr-xr-x root /usr/bin/passwd
1777 drwxrwxrwt root /tmp
640 -rw-r----- root /etc/shadow
---
cat: /etc/shadow: Permission denied
  • /usr/bin/passwd is setuid root: a normal user runs it to change their password, and it may update /etc/shadow, which only root can write.
  • /tmp is 1777: everyone can create files, the sticky bit stops users from deleting each other's.
  • /etc/shadow holds the password hashes and is unreadable for normal users.

You may also have noticed that directories in this course's home folder show drwxr-sr-x: the home directory has the setgid bit, so new directories inherit it.

Warning: Setuid programs are a classic attack target. Never set the setuid bit on shell scripts or programs you didn't write, and search for unexpected ones with find / -perm -4000 -type f 2>/dev/null.

Beyond rwx

Two more layers you may meet on servers:

Feature Commands Use
ACLs getfacl, setfacl -m u:ada:r file Grant one extra user or group access without changing owner/group
Capabilities getcap, setcap cap_net_bind_service=+ep prog Give a program one root power (e.g. bind port 80) instead of full setuid
SELinux / AppArmor ls -Z, aa-status Mandatory access control on top of normal permissions

If permissions look right but access is still denied, one of these is often the reason.

Test yourself

You own a file with mode -r--rw-rw-. Can you write to it?

  1. Yes, the group and others have write
  2. No, only the owner bits apply to the owner
  3. Only with sudo
  4. Only if you are in the group

What does the w permission on a directory allow?

  1. Editing the files inside
  2. Creating, deleting and renaming entries in it
  3. Entering it with cd
  4. Listing its contents

Why is /tmp mode 1777?

  1. So only root can use it
  2. Everyone can create files, and the sticky bit stops users deleting each other's files
  3. It makes files executable
  4. It is a mistake