File Permissions and Ownership
Read the permission string in ls -l, convert rwx to octal numbers like 755 and 644, and change access safely with chmod and chown.
Welcome back!
Step 1 of 12Learn
Permission denied
Aisha writes a small script called deploy.sh, types ./deploy.sh and gets: bash: ./deploy.sh: Permission denied. The file is hers, she just created it, and yet Linux won't run it. The reason is the most important security mechanism on any Linux system: file permissions.
Linux is a multi-user system. Dozens of people and services may share one machine, so every file and directory records who owns it and who may do what with it. Run ls -l and the first column shows this:
-rwxr-xr-- 1 aisha devs 1820 Mar 14 09:12 deploy.shRead the first ten characters like this:
- Character 1 is the type:
-regular file,ddirectory,lsymbolic link. - Characters 2–4 are the permissions of the owner (user): here
rwx. - Characters 5–7 are the permissions of the group: here
r-x. - Characters 8–10 are the permissions of others, everyone else: here
r--.
After the link count come the owner (aisha) and the group (devs). Each user belongs to one or more groups, which is how a team can share files without opening them to everyone.
Within each triplet, r means read, w means write and x means execute; a dash means that permission is missing. Aisha's new file was created as -rw-r--r--, with no x anywhere, so the shell refused to execute it.
Step 2 of 12Practice · Diagram
Reading -rwxr-xr--
Ten characters: one for the type, then three triplets. Tap each part.
Which part decides what members of the devs group can do?
Tap the shape that answers the question, then press Check.
- -rwxr-xr--
- - type
-regular file,ddirectory,lsymbolic link.- rwx owner
aisha can read, write and run the file.
- r-x group
Members of the devs group can read and run it, not change it.
- r-- others
Everyone else can only read it.
Step 3 of 12Learn
What r, w and x really mean
The three letters mean slightly different things for files and for directories, and the directory meanings cause most confusion.
| On a file | On a directory | |
|---|---|---|
r read | view the contents | list the names inside (ls) |
w write | change the contents | create, delete or rename entries inside |
x execute | run it as a program | enter it (cd) and reach files inside |
Two consequences surprise people:
- Deleting a file depends on write permission on the directory, not on the file. If you can write to a directory, you can remove a read-only file inside it.
- A directory with
rbut noxlets you see file names but not open the files. To be useful, directories almost always needrandxtogether.
Linux checks permissions in order: if you are the owner, the owner triplet applies and the others are ignored; otherwise, if you are in the file's group, the group triplet applies; otherwise, the "others" triplet applies. The root user bypasses these checks, which is why working as root is risky.
Two other commands help: id shows your user and the groups you belong to, and ls -ld dirname shows a directory's own permissions instead of listing its contents.
Step 4 of 12Diagram
Which triplet applies to you?
Linux stops at the first rule that fits. Only one triplet is ever used.
Tap a shape with to see more.
- Root?
Root skips the checks entirely.
- Owner? Use owner triplet
Even if the group or others triplet would allow more.
- In the group? Use group triplet
- Otherwise: others triplet
Step 5 of 12Practice · Sort into groups
For the file -rw-r----- 1 maria finance report.xlsx, sort each action by whether it is allowed or denied (assume nobody is root).
Tap an item, then tap its group (or drag it there).
Step 6 of 12Learn
Octal numbers: 755, 644 and friends
Admins often write permissions as three digits, such as chmod 755 deploy.sh. Each digit is one triplet (owner, group, others), and each permission has a value:
- r = 4
- w = 2
- x = 1
Add the values for each triplet. rwx is 4 + 2 + 1 = 7. r-x is 4 + 0 + 1 = 5. r-- is 4. rw- is 6. So rwxr-xr-x is 755 and rw-r--r-- is 644.
The values that matter in daily work:
| Octal | Symbolic | Typical use |
|---|---|---|
| 755 | rwxr-xr-x | scripts and programs; directories everyone can enter |
| 644 | rw-r--r-- | normal files: owner edits, others read |
| 700 | rwx------ | private directories, such as ~/.ssh |
| 600 | rw------- | private files: SSH keys, files with passwords |
| 775 / 664 | group can write too | shared team directories and files |
You may see advice online to "just chmod 777" when something fails. 777 gives everyone full write access, so any user or compromised service on the machine can change or replace the file. It hides the real problem instead of fixing it. Find out which user needs access and grant exactly that.
Step 7 of 12Flashcards
Octal modes you will meet daily
Tap a card to flip it.
Step 8 of 12Try the calculator
Permission digit calculator
Enter 1 if a permission is granted and 0 if not, for each of the three classes. The calculator adds r = 4, w = 2 and x = 1 for each digit.
- Owner digit
- 7
- Group digit
- 5
- Others digit
- 5
- Full mode for chmod
- 755
Step 9 of 12Practice · Work it out
What octal number corresponds to rwxr-x---?
Step 10 of 12Learn
Changing permissions and owners
chmod (change mode) sets permissions in two ways.
Numeric mode sets all three triplets at once: chmod 644 notes.txt, chmod 700 ~/private.
Symbolic mode adjusts specific bits without touching the rest. Pick who (u user/owner, g group, o others, a all), an operator (+ add, - remove, = set exactly) and the permissions:
chmod u+x deploy.shadds execute for the owner. This is the fix for Aisha's script.chmod go-w shared.txtremoves write from group and others.chmod a+r index.htmlmakes the file readable by everyone.chmod -R g+w /srv/teamapplies a change recursively to a whole tree.
chown (change owner) changes who owns a file, and usually requires root:
sudo chown aisha report.txtmakes aisha the owner.sudo chown aisha:devs report.txtsets owner and group together.sudo chown -R www-data:www-data /var/www/sitehands a website's files to the web server's account.
chgrp devs report.txt changes only the group. New files get default permissions from your umask, typically giving 644 for files and 755 for directories.
Step 11 of 12Practice · Select all that apply
Which commands would let the owner run backup.sh without giving anyone else write access? Select all that apply.
Select all that apply.
Step 12 of 12Practice · What would you do?
What would you do?
A web app can't write uploaded files to /var/www/app/uploads, which is owned by root:root with mode 755. The app runs as the user www-data. A forum post says to run chmod -R 777 /var/www. What should you do?
Lesson complete!