"Set it to 777 and everything will work" is one of the most harmful tips on the internet. Linux permissions are simple, and after ten minutes with them you will set 755 and 644 knowingly rather than by guessing. Here is what the numbers and letters mean, which permissions a website, SSH keys and scripts need, and what umask is.

Who can do what

Every file has an owner and a group. Permissions are set for three categories:

Each category has three rights: r — read, w — write, x — execute. For directories the meaning differs a little: r — list the files, w — create and delete files inside, x — enter the directory and access the files in it.

ls -l shows permissions as a string: -rwxr-xr-x. The first character is the type (- file, d directory, l link), followed by three characters each for the owner, the group and others.

How to read the numbers

Each right is a number: r = 4, w = 2, x = 1. A category's permissions are their sum:

DigitRightsMeaning
7rwxEverything: read, change, run
6rw-Read and change
5r-xRead and run
4r--Read only
0---Nothing

Three digits are the owner, the group and others. 755 = rwxr-xr-x: the owner can do everything, others can read and run. 644 = rw-r--r--: the owner reads and writes, others only read.

Which permissions to set

WhatPermissionsWhy
Website files644The web server reads them but does not change them
Website directories755Without x you cannot enter a directory
Scripts755 or 700They need the execute right
Configs with passwords (.env, wp-config.php)600 or 640No outsider should read the passwords
~/.ssh700SSH refuses to work if others can access the directory
Private key ~/.ssh/id_ed25519600Only the owner reads the key
~/.ssh/authorized_keys600Otherwise sshd may ignore the keys

It is convenient to fix directories and files separately:

find /var/www/site -type d -exec chmod 755 {} +
find /var/www/site -type f -exec chmod 644 {} +

Why not 777

777 lets any user on the system and any process change the file. On a website, that means a vulnerability in one plugin, or someone else's site on the same server, can replace your files and inject malicious code. If "it does not work without 777", the problem is almost always the owner: the files belong to a different user than the one the web server or PHP runs as. The fix is chown, not permissions for everyone.

Symbolic notation

You can change permissions precisely without recalculating digits:

chmod u+x script.sh       # give the owner the right to run
chmod g-w file.txt        # remove write from the group
chmod o= secret.txt       # nothing for others
chmod -R a+X dir/         # x only for directories and already executable files

The capital X is a useful detail: in a recursive change it gives the right to enter directories but does not make ordinary files executable.

Special bits

To find all setuid files on the system: find / -perm -4000 -type f 2>/dev/null.

umask: default permissions

umask defines which rights are removed from new files. With the usual umask 022, new files get 644 and directories 755. With umask 027, other users cannot see new files at all: 640 and 750.

Permissions calculator

The chmod calculator converts permissions between digits, letters and checkboxes, applies expressions like u+x,g-w the same way GNU chmod does, handles special bits and works out default permissions for a umask. And if access is needed to commands rather than files, there are sudoers rules.