Overview
File permissions control who can read, write, and execute files on a Linux system. Misconfigured permissions are a leading cause of security incidents. This tutorial explains the permission model and shows how to use chmod, chown, and chgrp correctly.
The Permission Model
Every file and directory has three permission sets:
| Set | Symbol | Applies to |
|---|---|---|
| User | u | The file owner |
| Group | g | Members of the file's group |
| Others | o | Everyone else |
Each set has three permission bits: read (r), write (w), and execute (x).
Reading Permission Strings
ls -l script.sh
-rwxr-xr-- 1 alice devs 1024 Jan 1 12:00 script.sh
| Position | Meaning |
|---|---|
- | File type (- regular file, d directory, l symlink) |
rwx | Owner: read, write, execute |
r-x | Group: read, execute (no write) |
r-- | Others: read only |
Numeric (Octal) Mode
Each permission maps to a number: read = 4, write = 2, execute = 1. Sum the values for each set.
| Octal | Symbolic | Meaning |
|---|---|---|
7 | rwx | Full access |
6 | rw- | Read and write |
5 | r-x | Read and execute |
4 | r-- | Read only |
0 | --- | No permissions |
Common Octal Modes
| Mode | Symbolic | Typical use |
|---|---|---|
755 | rwxr-xr-x | Executable scripts, web directories |
644 | rw-r--r-- | Regular files, HTML, CSS |
600 | rw------- | Private keys, sensitive config |
700 | rwx------ | Private directories |
Symbolic Mode
Symbolic mode uses letters and operators instead of numbers.
# Add execute for owner
chmod u+x script.sh
# Remove write for group and others
chmod go-w file.txt
# Set exact permissions for all
chmod u=rwx,g=rx,o= file.sh
Changing Ownership
# Change owner
sudo chown alice file.txt
# Change owner and group
sudo chown alice:developers file.txt
# Change group only
sudo chgrp developers file.txt
# Recursively change ownership
sudo chown -R alice:developers /var/www/site
Special Permissions
| Bit | Octal | Effect |
|---|---|---|
| Setuid | 4xxx | Runs as file owner, not the user who executed it |
| Setgid | 2xxx | On directories, new files inherit the group |
| Sticky bit | 1xxx | Only the file owner can delete files in the directory (used on /tmp) |
Security Best Practices
- Never use
chmod 777on production files or directories. It gives everyone full access. - Private keys should be
600. SSH refuses to use keys with looser permissions. - Directories need execute permission to be traversed. A directory with mode
644cannot be entered even if it is readable. - Use groups instead of world permissions to share access among a team.
- Audit regularly with
find /var/www -perm -o+wto locate world-writable files.
