Read the Permission String
ls -l deploy.sh
-rwxr-x--- 1 deploy www-data 812 Nov 30 10:15 deploy.sh
The first character is the type: - for a file, d for a directory, l for a symbolic link. Then come three groups of three: the owner (rwx), the group (r-x), and everyone else (---). A dash means the permission is not set.
| Letter | On a file | On a directory |
|---|---|---|
r | Read it | List names |
w | Change it | Add, delete, rename |
x | Run it | Enter it |
Numeric Modes
Each permission has a value: read 4, write 2, execute 1. Add them per set, and write the three sums in the order owner, group, others:
| Digit | Means |
|---|---|
7 | rwx |
6 | rw- |
5 | r-x |
4 | r-- |
0 | --- |
chmod 640 config.ini
This gives the owner read and write, the group read, and others nothing.
Common Values
| Mode | Typical use |
|---|---|
644 | Normal files, web pages |
755 | Directories, scripts |
600 | Private keys, secrets |
700 | Private directories |
640 | Config read by a group |
Symbolic Modes
Letters name who is affected (u owner, g group, o others, a all), an operator (+ adds, - removes, = sets exactly), and the permissions:
chmod u+x deploy.sh # owner may run it
chmod g-w report.txt # group loses write
chmod o= secret.txt # others get nothing
chmod a+r index.html # everyone may read
chmod u=rw,go=r notes.txt # same as 644
A symbolic mode changes only the bits it names; a numeric mode sets all of them at once. The most common case, chmod +x, is covered in how to make a file executable in Linux.
Directories, -R, and Capital X
A warning first: -R changes a directory and everything below it, and there is no undo. Never run it on / or on system directories such as /etc, /usr, or /var, where wrong modes can break sudo, SSH, and services. Check the path with ls -ld, and use the narrowest directory that does the job.
A capital X sets execute only on directories, and on files that already have execute for someone, which makes it the safe way to fix a web root in one command:
ls -ld /var/www/example.com
chmod -R u=rwX,go=rX /var/www/example.com
Directories end up as 755 and files as 644; a file that already had an execute bit, such as a script, ends up as 755. Other methods, with find, are in chmod recursive.
Special Bits
A fourth, leading digit sets setuid (4), setgid (2), and the sticky bit (1): chmod 2775 shared/ makes new files inherit the directory's group, and the 1777 mode of /tmp lets users delete only their own files. See sticky bit for the details.
Copy Permissions from Another File
chmod --reference=good.conf new.conf
The second file gets the same mode as the first. To see a mode as a number before you copy it, run stat -c '%a' good.conf.
Why Not 777
Mode 777 lets every user and every process on the server change or replace the file, including a compromised web application. When something "only works with 777", the real fix is almost always ownership: give the file to the right user with the chown command. The risks are explained in chmod 777.
Where Default Permissions Come From
New files start from 666 and new directories from 777, minus the bits in your umask. With a umask of 022, files are created as 644 and directories as 755. How to read and change it is in umask.