Course contents · Lesson 6 of 19

Linux File Permissions Explained Simply

The Command Line · Lesson 6 of 19 · 9 min Foundations

You type ./backup.sh to run a script, a text file of commands a colleague sent you, and the terminal replies "Permission denied", though the file is right there. This lesson explains Linux file permissions: who they apply to, how to read them, what read, write and execute mean, and how to change them safely.

Owner, group and others

Every file and folder records two names. Its owner is one user account, usually whoever created it. Its group is a named set of users who share access, such as a team; lesson 7 shows how both are made. Everyone else counts as others.

A permission is an on-off switch for one kind of access: read, write or execute. Each of the three audiences has its own three switches, so every file carries nine.

Reading the permission string in ls -l

In the long listing, ls -l (lesson 4), each line starts with a permission string. The diagram splits one line into seven labelled parts.

A terminal shows ls -l script.sh and the output line -rwxr-xr-- 1 anita team 120 Mar 3 10:15 script.sh. Below, the line is split. The permission string is part 1, the type, a dash meaning an ordinary file; part 2, rwx for the owner, read, write and run; part 3, r-x for the group, read and run; part 4, r-- for others, read only. Then come the link count 1, part 5 the owner anita, a user, part 6 the group team, a set of users, the size 120 bytes, the date last changed and part 7 the name script.sh. The owner name shares the owner set's colour, and the group name shares the group set's colour. Names, size and date are illustrative
The colours show whose set of three letters is whose.

The first character is the type: - for an ordinary file, d for a directory and l for a symbolic link (lesson 5). Then come three sets of three letters, for the owner, the group and others. Each slot shows its letter when on and a dash when off, always in the order r, w, x.

So anita, the owner, may read, change and run the script (rwx). Members of the group team may read and run it (r-x). Everyone else may only read it (r--).

The grey parts are the link count (for a file, usually 1), the size in bytes and the date of the last change. Some systems add a character after the nine letters, such as +, . or @ on a Mac, to flag extra settings; ignore it for now.

Read, write and execute

On a folder the letters shift meaning, and execute becomes the right to go in.

LetterOn a fileOn a folder
r readopen it and read its contentslist the names inside
w writechange its contentscreate, delete or rename files inside (needs x too)
x executerun it as a programenter it with cd and reach the files inside

Two surprises follow. Deleting a file changes its folder's list of names, so it needs the folder's w, not the file's. And with r but no x on a folder, you can list the names but not open the files.

Which set applies to you

When you use a file, the kernel, the core of the operating system (lesson 2), picks one set of letters and checks only that. In the flowchart, notice that the questions stop at the first yes.

Flowchart. You try to use a file, to read, write or run it. Are you the file's owner? If yes, use the owner's letters, rwx in the example. If no, are you in the file's group? If yes, use the group's letters, r-x. If no, use the others' letters, r--. All three lead to one check: does that set have the letter needed? Yes means allowed; no means Permission denied and nothing changes. A note says only the first match counts, so an owner never falls back to the group's letters
The kernel picks one set by who you are, then looks for the one letter it needs.

So on a file marked -r--rw-rw-, everyone may change it except its owner. The owner can fix that, since an owner may always change permissions. One account skips most of these checks: root, the administrator (lesson 7).

Changing permissions with chmod

chmod (change mode; the mode is the set of nine switches) changes permissions. Only the file's owner or root may use it on a file, in one of two styles.

Symbolic: who, plus or minus, which letters

Name who, then + to add, - to remove or = to set exactly, then the letters. Who is u (user, meaning the owner), g (group), o (others) or a (all three). So chmod u+x script.sh lets the owner run it, and chmod go-w notes.txt stops the group and others changing it.

Numeric: add up 4, 2 and 1

Each letter has a value: r is 4, w is 2 and x is 1. Add the values that are on in one set to get a digit from 0 to 7; three digits, for owner, group and others, set all nine switches. The table builds the common digits.

A table with columns r equals 4, w equals 2 and x equals 1. 7 is 4 plus 2 plus 1, rwx. 6 is 4 plus 2, rw-. 5 is 4 plus 1, r-x. 4 is read only, r--. 0 is nothing, three dashes. On the right, three-digit examples read owner, group, others: 644 is rw- r-- r--, for documents and settings; 755 is rwx r-x r-x, for programs and folders; 754 is rwx r-x r--, the script.sh example
One digit per set, owner first: three digits describe the file.

So chmod 644 notes.txt gives rw-r--r--: the owner reads and writes, everyone else only reads, the usual setting for documents. chmod 755 gives rwxr-xr-x, usual for programs and folders. Unlike the symbolic style, a number replaces all nine switches at once.

Changing the owner: chown and chgrp

chown (change owner) hands a file to another user and can set its group too: chown anita:team report.txt. Only root may change an owner, so you type sudo in front, which runs one command as root (lesson 7).

chgrp (change group) changes just the group, as in chgrp team report.txt. You may do that without root on a file you own, but only to a group you belong to. Both take -R for a whole folder, so check the path first.

Two common problems

"Permission denied" when you run a script

New files normally start without any x, so ./script.sh (run script.sh from this folder) is refused. Fix it with chmod u+x script.sh; lesson 16 comes back to this. bash script.sh also works, since bash then only needs to read the file.

Why chmod 777 is a bad idea

Forum answers sometimes suggest chmod 777, which is rwxrwxrwx: everything on for everyone. Then any account, including a program an attacker has taken over, may change the file; in a script, they could add commands that run as you.

Instead, grant only the missing letter, only to those who need it. This is called least privilege.

Check yourself

A file shows -rw-r----- with owner anita and group team. Ben is in team and Chen is not. What may each do?

Anita reads and changes it, Ben only reads it, and Chen can do nothing. Each gets exactly one set: the owner's rw-, the group's r-- or the others' ---.

You own plan.txt, shown as -r--rw-rw-. The group and others can edit it. Why can't you?

As the owner you get only the owner's set, r--; the kernel never falls back to the group's. As owner, you can fix it with chmod u+w plan.txt.

./deploy.sh says "Permission denied". Why fix it with chmod u+x rather than chmod 777?

It adds only the missing switch, execute for you. 777 would also let every account change the script, so anyone could add commands that then run as you.