Skip to content

File Permissions and ACLs | Linux

Every file and directory on a Linux system carries a set of permission bits that control which users Can read, write, or execute it. The kernel enforces these permissions during every file system Operation.

Each file has three categories of permissions, each with three bits:

CategoryRead (r)Write (w)Execute (x)
Owner (u)Read file contents / List directory entriesModify file / Create/delete directory entriesRun file / Enter directory
Group (g)Same as owner for group membersSameSame
Other (o)Same for everyone elseSameSame
OctalBinaryPermission
0000---
1001—x
2010-w-
3011-wx
4100r—
5101r-x
6110rw-
7111rwx

Common permission sets:

OctalMeaningUse Case
755rwxr-xr-xExecutables, directories
644rw-r—r—Regular files
700rwx------Private scripts
600rw-------SSH keys, config files
400r--------Read-only secrets
711rwx—x—xPublic directories (listable only by owner)
Terminal window
## Long listing shows permissions, owner, group
ls -la /etc/passwd
## -rw-r--r-- 1 root root 2847 Jan 15 10:30 /etc/passwd
# Numeric view
stat -c "%a %n' /etc/passwd
# 644 /etc/passwd
stat -c '%A %U:%G %n' /etc/passwd
# -rw-r--r-- root:root /etc/passwd
# Full stat output
stat /etc/passwd

Directory permissions have different semantics than file permissions:

PermissionFileDirectory
rRead file contentsList filenames (requires x)
wModify file contentsCreate/delete/rename files (requires x)
xExecute as a programEnter directory (cd), access inode information
To list directory contents: r + x
To create/delete files: w + x
To access a file inside: x on every parent directory
Without x on a directory:
- Cannot cd into it
- Cannot stat files inside it
- Cannot read files even if they are 644
Without r on a directory:
- Cannot list files (ls fails)
- But can access files if you know their names (cat dir/file works)
Terminal window
# Example: a directory where you can access files but not list them
mkdir secret && chmod 711 secret
echo "hidden content" > secret/data.txt
chmod 644 secret/data.txt
# Other users cannot list the directory
ls secret/ # Permission denied
# But can read the file if they know the name
cat secret/data.txt # works!
Terminal window
# Format: who + action + permission
# who: u (owner), g (group), o (other), a (all)
# action: + (add), - (remove), = (set exactly)
# permission: r, w, x, X, s, t, u, g, o
# Add execute for owner
chmod u+x script.sh
# Remove write for group and other
chmod go-w file.txt
# Set exact permissions
chmod u=rwx,g=rx,o= file.txt
# Recursively set directories to 755, files to 644
chmod -R a=rX,u+w . # X sets x only if already x for any category, or if directory
find . -type d -exec chmod 755 {} +
find . -type f -exec chmod 644 {} +
# Reference mode (copy permissions from another file)
chmod --reference=reference.txt target.txt
Terminal window
chmod 755 script.sh # rwxr-xr-x
chmod 600 id_rsa # rw-------
chmod 644 config.conf # rw-r--r--
chmod 1777 /tmp # rwxrwxrwt (sticky bit)
chmod 4755 /usr/bin/sudo # rwsr-xr-x (setuid)
Terminal window
# Change owner
chown user file.txt
# Change group
chgrp group file.txt
# Change both owner and group
chown user:group file.txt
# Change owner, keep group
chown user: file.txt
# Change group only (shortcut)
chown :group file.txt
# Recursive
chown -R user:group /var/www/html/
# Reference
chown --reference=ref.txt target.txt