Find a course

Module 4: Permissions, Users and ProcessesLesson 9 of 1312 min

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.

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

Read the first ten characters like this:

  • Character 1 is the type: - regular file, d directory, l symbolic 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.

Reading -rwxr-xr--Parts diagram: -rwxr-xr--, - type, rwx owner, r-x group, r-- others.-rwxr-xr--- typerwx ownerr-x groupr-- others
-rwxr-xr--
- type

- regular file, d directory, l symbolic 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 fileOn a directory
r readview the contentslist the names inside (ls)
w writechange the contentscreate, delete or rename entries inside
x executerun it as a programenter 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 r but no x lets you see file names but not open the files. To be useful, directories almost always need r and x together.

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.

Which triplet applies to you?Process diagram: Root? → Owner? Use owner triplet → In the group? Use group triplet → Otherwise: others triplet.Root?Owner? UseownertripletIn the group?Use grouptripletOtherwise:others triplet

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:

OctalSymbolicTypical use
755rwxr-xr-xscripts and programs; directories everyone can enter
644rw-r--r--normal files: owner edits, others read
700rwx------private directories, such as ~/.ssh
600rw-------private files: SSH keys, files with passwords
775 / 664group can write tooshared 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.sh adds execute for the owner. This is the fix for Aisha's script.
  • chmod go-w shared.txt removes write from group and others.
  • chmod a+r index.html makes the file readable by everyone.
  • chmod -R g+w /srv/team applies a change recursively to a whole tree.

chown (change owner) changes who owns a file, and usually requires root:

  • sudo chown aisha report.txt makes aisha the owner.
  • sudo chown aisha:devs report.txt sets owner and group together.
  • sudo chown -R www-data:www-data /var/www/site hands 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?