Learn / Linux / Users & Permissions
Lesson 18 of 74

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'.

25 min read Report a problem Hands-on lab included 7-question quiz

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?

Owner (u)the user the file belongs to
rw-read + write
Group (g)members of the file's group
r--read only
Others (o)every other account
---nothing

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.

bash
ls -l /etc/passwd /etc/shadow /usr/bin/ls
text
-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
FileStringWhat 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-xeveryone can run it; only root can change it

Break the string into four parts:

text
 -    rw-    r--    r--
 │     │      │      │
 │     │      │      └── others: everyone else
 │     │      └───────── group: members of the file's group
 │     └──────────────── user: the file's owner
 └────────────────────── type: - file, d directory, l symlink

Within 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

A process asks to open a filerunning as some UID with some groups
Is the process's UID the file's owner?yes → check the owner bits only, done
no
Is the file's group one of the process's groups?yes → check the group bits only, done
no
Check the others bitsallowed, or "Permission denied"

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.

LetterOn a fileOn a directory
r readread the contents (cat, less, cp from it)list the names inside (ls)
w writechange the contents (edit, append, truncate)create, delete and rename entries inside (needs x too)
x executerun it as a program or scriptenter it (cd) and reach the files inside by path

Three consequences that matter every day:

  1. Deleting a file is a directory operation. You need w and x on the folder, not any permission on the file itself. A read-only file in a folder you can write to can still be deleted.
  2. To open /a/b/c/file.txt you need x on /, /a, /a/b and /a/b/c, and then r on the file. One missing x anywhere on the path means "Permission denied".
  3. Directories need r and x together for normal use. That's why directories are 755 or 750, and files are 644 or 640.
bash
ls -ld /root /home/dev /tmp
su - dev -c 'ls /root'

-d shows the directory itself instead of its contents.

text
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
  • /root is rwx------: only root can list or enter it, so dev is refused.
  • /home/dev is rwxr-x---: dev has full control, the dev group can look around, others can't get in.
  • /tmp is rwxrwxrwt: everyone can create files there. The t at 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:

LetterValue
r4
w2
x1
-0

So every group of three becomes one digit from 0 to 7, and the whole mode becomes three digits.

text
  rwx  r-x  r--
  ───  ───  ───
  4+2+1 4+0+1 4+0+0
   7     5     4      →  754
DigitLettersDigitLetters
7rwx3-wx
6rw-2-w-
5r-x1--x
4r--0---

You'll use only a handful of modes in practice. Learn these by heart:

ModeLettersUse it for
644rw-r--r--normal files: configs, HTML, documents
640rw-r-----configs a service's group must read
600rw-------secrets: SSH private keys, .env files, tokens
755rwxr-xr-xscripts, programs and ordinary directories
750rwxr-x---directories only a team should enter
700rwx------private directories like ~/.ssh

stat shows both forms side by side:

bash
stat /etc/passwd
text
  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 +0530

The 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.

Try it · Task 1 of 4

Read permissions

Find every file in /srv/shop that anyone on the system can write to.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

bash
ls -l /srv/shop
ls -l /srv/shop/config /srv/shop/public
text
-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.css

Decode a few:

EntryPermissionsMeaning
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-rwxrwxrwxeveryone 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
reportsdrwx------a directory only its owner can use

2. The numeric view

bash
stat -c '%A %a %U:%G %n' /srv/shop/secrets.env /srv/shop/config/db.conf
Format codePrints
%Apermissions as letters
%apermissions as a number
%U:%Gowner and group
%nfile name
text
-rw-r--r-- 644 root:root /srv/shop/secrets.env
-rw-rw-rw- 666 root:root /srv/shop/config/db.conf

rw- = 4+2 = 6, r-- = 4: so rw-r--r-- is 644 and rw-rw-rw- is 666.

3. Find world-writable files

bash
find /srv/shop -type f -perm -o+w
PartMeaning
-type fregular files only
-perm -o+wfiles where others have write permission (the - before means "at least these bits")
text
/srv/shop/config/db.conf
/srv/shop/public/style.css

Security audits run exactly this check, usually on the whole system: find / -xdev -type f -perm -o+w 2>/dev/null.

2 · Your task

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
text
find /srv/shop -type f -perm -o+w > /root/world-writable.txt

What 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.
First start the lab machine in the terminal panel on the right.

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:

WhoHowWhat
u user/owner+ addr
g group- removew
o others= set exactlyx
a all (default)X (capital, see below)
bash
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 everyone

Commas separate several changes. o= with nothing after it means "others get nothing".

text
-rw-r----- 1 root root 22 Nov 24 15:51 /srv/shop/app.py

That'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.

Try it · Task 2 of 4

Symbolic chmod

Make the deploy script runnable, and take write access away from everyone else.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

Symbolic chmod changes specific permissions and leaves the rest alone. It reads like a sentence: who, add/remove/set, what.

WhoOperatorWhat
u user (owner)+ addr read
g group- removew write
o others= set exactlyx 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

bash
/srv/shop/deploy.sh
text
bash: /srv/shop/deploy.sh: Permission denied

Even 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

bash
chmod u+x /srv/shop/deploy.sh
ls -l /srv/shop/deploy.sh
/srv/shop/deploy.sh
text
-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

bash
chmod o-w /srv/shop/config/db.conf
ls -l /srv/shop/config/db.conf
text
-rw-rw-r-- 1 root root 33 Nov 24 15:48 /srv/shop/config/db.conf

Only 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-----.

2 · Your task
  1. Make /srv/shop/deploy.sh executable by its owner.
  2. Remove write permission for others from /srv/shop/config/db.conf and /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
text
chmod u+x /srv/shop/deploy.sh
chmod o-w /srv/shop/config/db.conf /srv/shop/public/style.css

What 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.
First start the lab machine in the terminal panel on the right.

Numeric mode: set everything at once

bash
chmod 600 secrets.env
chmod 640 config/db.conf
chmod 755 deploy.sh

Numeric 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.

text
640 -rw-r----- /srv/shop/config/db.conf
755 -rwxr-xr-x /srv/shop/deploy.sh
644 -rw-r--r-- /srv/shop/public/style.css
Try it · Task 3 of 4

Numeric chmod

Set exact permissions on the secret files and the script with octal modes.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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.

DigitLettersTypical use
7rwxowner of a script or directory
6rw-owner of a normal file
5r-xothers on a program or directory
4r--read only
0---nothing

The modes you'll use again and again:

ModeLettersFor
644rw-r--r--normal files: owner edits, everyone reads
600rw-------secrets: private keys, .env files, passwords
640rw-r-----configs the app's group needs to read
755rwxr-xr-xscripts, programs and directories
700rwx------private directories (~/.ssh)

1. Lock down a secret

bash
chmod 600 /srv/shop/secrets.env
ls -l /srv/shop/secrets.env
su - dev -c 'cat /srv/shop/secrets.env'
PartMeaning
6owner: rw-
0group: nothing
0others: nothing
text
-rw------- 1 root root 29 Nov 24 15:48 /srv/shop/secrets.env
cat: /srv/shop/secrets.env: Permission denied

Only 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

bash
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.css
text
640 -rw-r----- /srv/shop/config/db.conf
755 -rwxr-xr-x /srv/shop/deploy.sh
644 -rw-r--r-- /srv/shop/public/style.css

Never 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).

2 · Your task

Set these exact permissions:

FileMode
/srv/shop/secrets.env600
/srv/shop/config/db.conf640
/srv/shop/deploy.sh755
/srv/shop/public/style.css644

Expected result: stat -c '%a %n' on each file shows the mode from the table.

Hint: the commands, explained
text
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.css

What 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).
First start the lab machine in the terminal panel on the right.

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:

bash
chmod -R u+rwX,go+rX /srv/shop/reports
ls -ld /srv/shop/reports
ls -l /srv/shop/reports
text
drwxr-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.txt

The directory became 755 and the files 644, from one command. The alternative is two find commands, one per type:

text
find /srv/shop/reports -type d -exec chmod 755 {} +
find /srv/shop/reports -type f -exec chmod 644 {} +
Try it · Task 4 of 4

Directory permissions

Let the dev user list and read the reports, without being able to change them.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

On a directory, the three letters mean different things, and this trips up even experienced engineers:

LetterOn a directory it allows
rlisting the names inside (ls)
xentering it and reaching the files inside (cd, opening a file by its path)
wcreating, 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

bash
ls -ld /srv/shop/reports
su - dev -c 'ls /srv/shop/reports'
su - dev -c 'cat /srv/shop/reports/q3.txt'
text
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 denied

drwx------: 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

bash
chmod 744 /srv/shop/reports
su - dev -c 'ls /srv/shop/reports; cat /srv/shop/reports/q3.txt'
text
q3.txt
q4.txt
cat: /srv/shop/reports/q3.txt: Permission denied

With 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

bash
chmod 755 /srv/shop/reports
su - dev -c 'ls /srv/shop/reports; cat /srv/shop/reports/q3.txt'
text
q3.txt
q4.txt
cat: /srv/shop/reports/q3.txt: Permission denied

The 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:

text
chmod -R u+rwX,go+rX /srv/shop/reports

Or set folders and files separately with find:

text
find /srv/shop/reports -type d -exec chmod 755 {} +
find /srv/shop/reports -type f -exec chmod 644 {} +
2 · Your task

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
text
chmod 755 /srv/shop/reports
chmod 644 /srv/shop/reports/*.txt

What each part does

  • chmod 755 reports: on a folder, r lets you list it and x lets you enter it, so everyone can now open the folder.
  • chmod 644 reports/*.txt: *.txt matches every text file inside, and each becomes readable by everyone, writable only by the owner.
First start the lab machine in the terminal panel on the right.

Finding risky permissions

Security audits look for files that anyone can change. find -perm matches permission bits:

bash
find /srv/shop -type f -perm -o+w
FormMatches
-perm 644mode is exactly 644
-perm -o+w or -perm -002at least these bits are set (others can write)
-perm /u+sany of these bits is set
text
/srv/shop/config/db.conf
/srv/shop/public/style.css

On 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:

  1. Who is asking? id appuser shows the UID and every group. For a service, check the User= in its unit file or ps -o user= -p PID.
  2. Check every directory on the path. namei -l /srv/shop/reports/q3.txt prints the permissions of each component, so a missing x jumps out.
  3. Check the file itself: ls -l or stat.
  4. Test as that user: sudo -u appuser cat /path/file or su - appuser -c '...'.
  5. 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.
text
cat: /srv/shop/secrets.env: Permission denied

This 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 -l shows type plus three groups (owner, group, others) of r, 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 x on 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 640 sets them all.
  • chmod -R with capital X for trees. find -perm -o+w for audits. namei -l and su - user -c for debugging. Never chmod 777.
Check your understanding

Quick quiz

Feedback