File Permissions
How Linux decides who may read, change or run a file: the 10-character permission string, owner/group/others, what r, w and x mean on files versus directories, numeric modes like 644 and 755, symbolic and numeric chmod, recursive changes with capital X, and how to debug 'Permission denied'.
Why permissions exist
In the last lesson you saw that every file belongs to a user and a group. Permissions are the rules attached to the file that say what that owner, members of that group, and everybody else may do with it.
This is how one server safely holds:
- a database password the application must read, but other accounts must not,
- a website every visitor's request reads, but only deployers may change,
- a script the CI system runs, but nobody else may edit,
- each person's home folder, private to them.
Most "Permission denied" errors you'll meet as a DevOps engineer, from SSH refusing your key to nginx returning 403, come down to the rules in this lesson.
Who is asking, and what may they do?
db.conf with mode 640: the app's owner edits it, the app's group reads it, nobody else can even look.
Reading the permission string
Run ls -l and the first column is a 10-character string.
ls -l /etc/passwd /etc/shadow /usr/bin/ls-rw-r--r-- 1 root root 995 Nov 24 15:51 /etc/passwd
-rw-r----- 1 root shadow 549 Nov 24 15:51 /etc/shadow
-rwxr-xr-x 1 root root 142312 Apr 5 2024 /usr/bin/ls| File | String | What it says |
|---|---|---|
/etc/passwd | -rw-r--r-- | root edits it; everyone can read it (programs need to look up user names) |
/etc/shadow | -rw-r----- | root edits it; group shadow reads it; others get nothing (it holds password hashes) |
/usr/bin/ls | -rwxr-xr-x | everyone can run it; only root can change it |
Break the string into four parts:
- rw- r-- r--
│ │ │ │
│ │ │ └── others: everyone else
│ │ └───────── group: members of the file's group
│ └──────────────── user: the file's owner
└────────────────────── type: - file, d directory, l symlinkWithin each group of three, the positions are always r w x in that order, and a - means "not allowed".
Linux checks only one of the three groups for any request. If you're the owner, it uses the owner bits, and stops. Otherwise, if you're in the file's group, it uses the group bits. Otherwise it uses the others bits. (root skips these checks for reading and writing.)
That has a surprising consequence: with mode ---rw-rw-, the owner is the one person who can't read the file, even though everyone else can.
How the kernel decides
What r, w and x mean
The same three letters mean different things on files and on directories. This is the part that confuses people the most, so read the table twice.
| Letter | On a file | On a directory |
|---|---|---|
r read | read the contents (cat, less, cp from it) | list the names inside (ls) |
w write | change the contents (edit, append, truncate) | create, delete and rename entries inside (needs x too) |
x execute | run it as a program or script | enter it (cd) and reach the files inside by path |
Three consequences that matter every day:
- Deleting a file is a directory operation. You need
wandxon the folder, not any permission on the file itself. A read-only file in a folder you can write to can still be deleted. - To open
/a/b/c/file.txtyou needxon/,/a,/a/band/a/b/c, and thenron the file. One missingxanywhere on the path means "Permission denied". - Directories need
randxtogether for normal use. That's why directories are755or750, and files are644or640.
ls -ld /root /home/dev /tmp
su - dev -c 'ls /root'-d shows the directory itself instead of its contents.
drwxr-x--- 2 dev dev 4096 Nov 24 15:51 /home/dev
drwx------ 1 root root 4096 Nov 24 09:14 /root
drwxrwxrwt 1 root root 4096 Nov 24 15:51 /tmp
ls: cannot open directory '/root': Permission denied/rootisrwx------: only root can list or enter it, sodevis refused./home/devisrwxr-x---: dev has full control, thedevgroup can look around, others can't get in./tmpisrwxrwxrwt: everyone can create files there. Thetat the end is the sticky bit, which stops people deleting each other's files. You'll meet it in the SUID, SGID and the Sticky Bit lesson.
Permissions as numbers
Each letter has a value, and you add them up for each group:
| Letter | Value |
|---|---|
r | 4 |
w | 2 |
x | 1 |
- | 0 |
So every group of three becomes one digit from 0 to 7, and the whole mode becomes three digits.
rwx r-x r--
─── ─── ───
4+2+1 4+0+1 4+0+0
7 5 4 → 754| Digit | Letters | Digit | Letters | |
|---|---|---|---|---|
7 | rwx | 3 | -wx | |
6 | rw- | 2 | -w- | |
5 | r-x | 1 | --x | |
4 | r-- | 0 | --- |
You'll use only a handful of modes in practice. Learn these by heart:
| Mode | Letters | Use it for |
|---|---|---|
644 | rw-r--r-- | normal files: configs, HTML, documents |
640 | rw-r----- | configs a service's group must read |
600 | rw------- | secrets: SSH private keys, .env files, tokens |
755 | rwxr-xr-x | scripts, programs and ordinary directories |
750 | rwxr-x--- | directories only a team should enter |
700 | rwx------ | private directories like ~/.ssh |
stat shows both forms side by side:
stat /etc/passwd File: /etc/passwd
Size: 995 Blocks: 8 IO Block: 4096 regular file
Device: 0,41 Inode: 1475144 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2026-11-24 15:51:17.350752975 +0530
Modify: 2026-11-24 15:51:17.346092540 +0530
Change: 2026-11-24 15:51:17.346092540 +0530
Birth: 2026-11-24 15:51:17.345574527 +0530The Access: (0644/-rw-r--r--) line is the one to read: the mode as a number, then as letters, followed by the owner and group with their numeric IDs. The leading 0 is the special-bits digit, covered in a later lesson. For scripts, stat -c '%a %A %n' file prints just the parts you want.
Read permissions
Find every file in /srv/shop that anyone on the system can write to.
The first column of ls -l holds 10 characters: the file type, then three groups of three permission letters for the owner, the group and others. A - means "not allowed". "Others" means every account on the system, so a w in the last group means anyone can change the file.
1. List the application folder
ls -l /srv/shop
ls -l /srv/shop/config /srv/shop/public-rw-r--r-- 1 root root 22 Nov 24 15:48 app.py
drwxr-xr-x 2 root root 4096 Nov 24 15:48 config
-rw-r--r-- 1 root root 37 Nov 24 15:48 deploy.sh
drwxr-xr-x 2 root root 4096 Nov 24 15:48 public
drwx------ 2 root root 4096 Nov 24 15:48 reports
-rw-r--r-- 1 root root 29 Nov 24 15:48 secrets.env
/srv/shop/config:
-rw-rw-rw- 1 root root 33 Nov 24 15:48 db.conf
/srv/shop/public:
-rw-r--r-- 1 root root 14 Nov 24 15:48 index.html
-rwxrwxrwx 1 root root 22 Nov 24 15:48 style.cssDecode a few:
| Entry | Permissions | Meaning |
|---|---|---|
secrets.env | -rw-r--r-- | owner reads and writes; group and everyone can read the API key |
db.conf | -rw-rw-rw- | everyone can read and change the database password |
style.css | -rwxrwxrwx | everyone can do everything (the classic "chmod 777" mistake) |
deploy.sh | -rw-r--r-- | nobody has x, so it can't be run as a program |
reports | drwx------ | a directory only its owner can use |
2. The numeric view
stat -c '%A %a %U:%G %n' /srv/shop/secrets.env /srv/shop/config/db.conf| Format code | Prints |
|---|---|
%A | permissions as letters |
%a | permissions as a number |
%U:%G | owner and group |
%n | file name |
-rw-r--r-- 644 root:root /srv/shop/secrets.env
-rw-rw-rw- 666 root:root /srv/shop/config/db.confrw- = 4+2 = 6, r-- = 4: so rw-r--r-- is 644 and rw-rw-rw- is 666.
3. Find world-writable files
find /srv/shop -type f -perm -o+w| Part | Meaning |
|---|---|
-type f | regular files only |
-perm -o+w | files where others have write permission (the - before means "at least these bits") |
/srv/shop/config/db.conf
/srv/shop/public/style.cssSecurity audits run exactly this check, usually on the whole system: find / -xdev -type f -perm -o+w 2>/dev/null.
Save the full paths of every file under /srv/shop that others can write to, one per line, to /root/world-writable.txt.
Expected result: the file lists db.conf and style.css with their full paths.
Hint: the commands, explained
find /srv/shop -type f -perm -o+w > /root/world-writable.txtWhat each part does
find /srv/shop -type f: every regular file under the folder.-perm -o+w: only files where others have write permission (the-means "at least these bits are set").>saves the list of those risky files.
Changing permissions: chmod
chmod (change mode) has two styles. Only the file's owner or root can run it.
Symbolic mode: change specific bits
Write who, then how, then what:
| Who | How | What |
|---|---|---|
u user/owner | + add | r |
g group | - remove | w |
o others | = set exactly | x |
a all (default) | X (capital, see below) |
chmod u+x deploy.sh # owner may run it
chmod o-w config/db.conf # others may no longer write
chmod go-rwx secrets.env # group and others lose everything
chmod u=rw,g=r,o= app.py # set each group exactly: rw-r-----
chmod +x script.sh # add x for everyoneCommas separate several changes. o= with nothing after it means "others get nothing".
-rw-r----- 1 root root 22 Nov 24 15:51 /srv/shop/app.pyThat's app.py after chmod u=rw,g=r,o=. Symbolic mode is best when you want to change one thing without caring what the other bits currently are.
Symbolic chmod
Make the deploy script runnable, and take write access away from everyone else.
Symbolic chmod changes specific permissions and leaves the rest alone. It reads like a sentence: who, add/remove/set, what.
| Who | Operator | What | ||
|---|---|---|---|---|
u user (owner) | + add | r read | ||
g group | - remove | w write | ||
o others | = set exactly | x execute | ||
a all three |
So u+x = "give the owner execute", o-w = "take write away from others", go-rwx = "remove everything from group and others".
1. Why the script won't run
/srv/shop/deploy.shbash: /srv/shop/deploy.sh: Permission deniedEven root gets "Permission denied": the file has no x bit at all, so the kernel refuses to run it as a program. (bash deploy.sh would work, because then bash is the program and the script is just a file it reads.)
2. Make it executable
chmod u+x /srv/shop/deploy.sh
ls -l /srv/shop/deploy.sh
/srv/shop/deploy.sh-rwxr--r-- 1 root root 37 Nov 24 15:48 /srv/shop/deploy.sh
Deploying shop...The owner now has rwx, and the script runs. chmod +x file (no "who") adds x for everyone, the common shortcut for scripts.
3. Take write away from others
chmod o-w /srv/shop/config/db.conf
ls -l /srv/shop/config/db.conf-rw-rw-r-- 1 root root 33 Nov 24 15:48 /srv/shop/config/db.confOnly the w in the "others" group disappeared; everything else stayed as it was. Several changes at once are separated by commas: chmod u=rw,g=r,o= db.conf sets exactly rw-r-----.
- Make
/srv/shop/deploy.shexecutable by its owner. - Remove write permission for others from
/srv/shop/config/db.confand/srv/shop/public/style.css.
Expected result: deploy.sh runs, and find /srv/shop -type f -perm -o+w prints nothing.
Hint: the commands, explained
chmod u+x /srv/shop/deploy.sh
chmod o-w /srv/shop/config/db.conf /srv/shop/public/style.cssWhat each part does
chmod u+x deploy.sh: add (+) execute for the owner (user), so it can be run as./deploy.sh.chmod o-w FILE FILE: remove (-) write for others on both files. Other bits are left as they were.
Numeric mode: set everything at once
chmod 600 secrets.env
chmod 640 config/db.conf
chmod 755 deploy.shNumeric mode replaces all nine bits, so the result is the same whatever the file had before. That makes it the right choice in scripts, Dockerfiles and Ansible playbooks, where you want a known end state.
640 -rw-r----- /srv/shop/config/db.conf
755 -rwxr-xr-x /srv/shop/deploy.sh
644 -rw-r--r-- /srv/shop/public/style.cssNumeric chmod
Set exact permissions on the secret files and the script with octal modes.
Numeric (octal) chmod sets all permissions at once with three digits: owner, group, others. Each digit is the sum of r = 4, w = 2, x = 1.
| Digit | Letters | Typical use |
|---|---|---|
7 | rwx | owner of a script or directory |
6 | rw- | owner of a normal file |
5 | r-x | others on a program or directory |
4 | r-- | read only |
0 | --- | nothing |
The modes you'll use again and again:
| Mode | Letters | For |
|---|---|---|
644 | rw-r--r-- | normal files: owner edits, everyone reads |
600 | rw------- | secrets: private keys, .env files, passwords |
640 | rw-r----- | configs the app's group needs to read |
755 | rwxr-xr-x | scripts, programs and directories |
700 | rwx------ | private directories (~/.ssh) |
1. Lock down a secret
chmod 600 /srv/shop/secrets.env
ls -l /srv/shop/secrets.env
su - dev -c 'cat /srv/shop/secrets.env'| Part | Meaning |
|---|---|
6 | owner: rw- |
0 | group: nothing |
0 | others: nothing |
-rw------- 1 root root 29 Nov 24 15:48 /srv/shop/secrets.env
cat: /srv/shop/secrets.env: Permission deniedOnly the owner can read it now, and the dev user is refused.
2. A config the group may read, and a script everyone may run
chmod 640 /srv/shop/config/db.conf
chmod 755 /srv/shop/deploy.sh
chmod 644 /srv/shop/public/style.css
stat -c '%a %A %n' /srv/shop/config/db.conf /srv/shop/deploy.sh /srv/shop/public/style.css640 -rw-r----- /srv/shop/config/db.conf
755 -rwxr-xr-x /srv/shop/deploy.sh
644 -rw-r--r-- /srv/shop/public/style.cssNever use chmod 777 to "fix" a permission problem. It gives every account on the server full control, including any attacker who gets in through a web app. Find out who needs access and give exactly that, usually through the group (next lesson).
Set these exact permissions:
| File | Mode |
|---|---|
/srv/shop/secrets.env | 600 |
/srv/shop/config/db.conf | 640 |
/srv/shop/deploy.sh | 755 |
/srv/shop/public/style.css | 644 |
Expected result: stat -c '%a %n' on each file shows the mode from the table.
Hint: the commands, explained
chmod 600 /srv/shop/secrets.env
chmod 640 /srv/shop/config/db.conf
chmod 755 /srv/shop/deploy.sh
chmod 644 /srv/shop/public/style.cssWhat each part does
- Numeric mode: each digit is owner, group, others. 4 = read, 2 = write, 1 = execute; add them up.
600=rw-------(only the owner, for secrets) ·640=rw-r-----(group can read) ·755=rwxr-xr-x(a script anyone can run) ·644=rw-r--r--(a public file).
Recursive changes and capital X
chmod -R changes a whole tree. The trap: chmod -R 755 docs/ marks every text file as executable, and chmod -R 644 docs/ takes x away from every directory, locking everyone out of the subfolders.
Capital X solves this. It adds execute only to directories, and to files that are already executable for someone:
chmod -R u+rwX,go+rX /srv/shop/reports
ls -ld /srv/shop/reports
ls -l /srv/shop/reportsdrwxr-xr-x 2 root root 4096 Nov 24 15:51 /srv/shop/reports
total 8
-rw-r--r-- 1 root root 19 Nov 24 15:51 q3.txt
-rw-r--r-- 1 root root 20 Nov 24 15:51 q4.txtThe directory became 755 and the files 644, from one command. The alternative is two find commands, one per type:
find /srv/shop/reports -type d -exec chmod 755 {} +
find /srv/shop/reports -type f -exec chmod 644 {} +Directory permissions
Let the dev user list and read the reports, without being able to change them.
On a directory, the three letters mean different things, and this trips up even experienced engineers:
| Letter | On a directory it allows |
|---|---|
r | listing the names inside (ls) |
x | entering it and reaching the files inside (cd, opening a file by its path) |
w | creating, deleting and renaming files inside (together with x) |
To read a file, you need x on every directory on the way to it, and r on the file itself.
1. The dev user is locked out
ls -ld /srv/shop/reports
su - dev -c 'ls /srv/shop/reports'
su - dev -c 'cat /srv/shop/reports/q3.txt'drwx------ 2 root root 4096 Nov 24 15:48 /srv/shop/reports
ls: cannot open directory '/srv/shop/reports': Permission denied
cat: /srv/shop/reports/q3.txt: Permission denieddrwx------: others have no r (can't list) and no x (can't get in).
2. r without x: you see names, but can't open anything
chmod 744 /srv/shop/reports
su - dev -c 'ls /srv/shop/reports; cat /srv/shop/reports/q3.txt'q3.txt
q4.txt
cat: /srv/shop/reports/q3.txt: Permission deniedWith r but no x on the folder, dev can list the names but not reach the files. You need both r and x on a directory for normal use, which is why directories are usually 755 or 750, never 644.
3. x on the folder, but not r on the file
chmod 755 /srv/shop/reports
su - dev -c 'ls /srv/shop/reports; cat /srv/shop/reports/q3.txt'q3.txt
q4.txt
cat: /srv/shop/reports/q3.txt: Permission deniedThe folder is fine now, but the files themselves are 600. Every level has to allow access: directory and file.
4. Fixing a whole tree: capital X
For many files and folders, chmod -R with a capital X is the trick: it adds execute only to directories (and files that are already executable), so files don't wrongly become programs:
chmod -R u+rwX,go+rX /srv/shop/reportsOr set folders and files separately with find:
find /srv/shop/reports -type d -exec chmod 755 {} +
find /srv/shop/reports -type f -exec chmod 644 {} +Let the dev user list /srv/shop/reports and read the files in it, but not create or change anything there.
Expected result: su - dev -c 'cat /srv/shop/reports/q3.txt' prints the report, and su - dev -c 'touch /srv/shop/reports/x.txt' is refused.
Hint: the commands, explained
chmod 755 /srv/shop/reports
chmod 644 /srv/shop/reports/*.txtWhat each part does
chmod 755 reports: on a folder,rlets you list it andxlets you enter it, so everyone can now open the folder.chmod 644 reports/*.txt:*.txtmatches every text file inside, and each becomes readable by everyone, writable only by the owner.
Finding risky permissions
Security audits look for files that anyone can change. find -perm matches permission bits:
find /srv/shop -type f -perm -o+w| Form | Matches |
|---|---|
-perm 644 | mode is exactly 644 |
-perm -o+w or -perm -002 | at least these bits are set (others can write) |
-perm /u+s | any of these bits is set |
/srv/shop/config/db.conf
/srv/shop/public/style.cssOn a real server you'd scan the whole system and hide the errors from folders you can't read: find / -xdev -type f -perm -o+w 2>/dev/null.
Debugging "Permission denied"
When a user or service is refused, work through it in order:
- Who is asking?
id appusershows the UID and every group. For a service, check theUser=in its unit file orps -o user= -p PID. - Check every directory on the path.
namei -l /srv/shop/reports/q3.txtprints the permissions of each component, so a missingxjumps out. - Check the file itself:
ls -lorstat. - Test as that user:
sudo -u appuser cat /path/fileorsu - appuser -c '...'. - Fix the narrowest thing: add the user to the file's group, or give the group the bit it needs. Don't open it to everyone.
cat: /srv/shop/secrets.env: Permission deniedThis is what the dev user sees reading a 600 file owned by root. The message never says which rule failed, which is why the path-by-path check matters.
chmod 777 to make an error go away. It works, and it's one of the most common security findings on real servers. Every account, including a compromised web app, can now replace your script or config. Find out which user needs access and grant exactly that, usually through the group.
chmod -R 644 on a folder. Every subdirectory loses x, and nobody can get into them any more. Use capital X or separate find commands for files and directories.
Forgetting the directories. The file is 644 but a parent directory is 700, so the service still can't read it. namei -l shows the whole path.
Fixing a secret with 644. "Readable" means readable by every account on the box. Keys and tokens are 600, and SSH refuses private keys that are more open than that.
"Explain 755 and 644." Three digits for owner, group and others, each the sum of r=4, w=2, x=1. 755 is rwxr-xr-x (scripts and directories), 644 is rw-r--r-- (normal files).
"What does execute mean on a directory?" Permission to enter it and reach the files inside. r only lets you list the names.
"How do you find world-writable files?" find / -xdev -type f -perm -o+w 2>/dev/null.
"Can you delete a file you have no permissions on?" Yes, if you have write and execute on its directory. Deleting changes the directory, not the file. (The sticky bit, as on /tmp, prevents this for other people's files.)
"nginx returns 403 for a file you just deployed. What do you check?" The nginx worker's user (www-data), namei -l on the file's path for a missing x, and r on the file for that user.
Summary
ls -lshows type plus three groups (owner, group, others) ofr,w,x. The kernel checks exactly one group: owner, else group, else others.- On files: read, change, run. On directories: list, create/delete inside, enter. You need
xon every directory along the path. - r=4, w=2, x=1. Know
644,640,600,755,750,700. chmod u+x,o-w,go-rwx,u=rw,g=r,o=change specific bits;chmod 640sets them all.chmod -Rwith capitalXfor trees.find -perm -o+wfor audits.namei -landsu - user -cfor debugging. Neverchmod 777.
Quick quiz
You've practised everything in this lesson
Every task was checked on your own machine. You can end the lab, or keep experimenting in the terminal.