Operating systems / Linux file permissions

Linux file permissions

What rw-r----- means and where 640 comes from: one file, three kinds of people, nine switches, and the two commands that change them.

Beginner7 steps~6 min
Owner — the one user the file belongs to
Group — a named set of users
Others — everyone else on the system
Switch — one permission, on or off
// THE SHORT VERSION

In 30 seconds

Every Linux file has an owner and a group; everyone else counts as others. For each of the three, the file stores three switches: read, write and execute.

ls -l shows them as a string such as rw-r-----. Counting r as 4, w as 2 and x as 1 turns each triplet into a digit, here 640.

chmod changes the switches and chown changes the owner. On a directory the same letters mean list, change the entries, and enter.

Linux file permissions, step by step

STEPS01 / 07

Left out: root bypasses these checks, and there are extra bits (setuid, setgid, sticky) and access control lists on top of the nine switches.

STEP 01 / 07 Three kinds of people
THE MATH
owner: alice group: staff others: everyone else
  1. Step 1: Three kinds of people. Every file on Linux records one owner, a user, and one group, a named set of users. That splits everybody into three classes: the owner, the members of the group, and all others. Here report.txt is owned by alice and belongs to the group staff.
  2. Step 2: Three things to do. There are three things someone can be allowed to do with a file. Read: look at its contents. Write: change its contents. Execute: run it as a program. Each is written as one letter, r, w or x. Three classes of people times three actions gives nine yes-or-no switches per file.
  3. Step 3: Reading ls -l. The command ls -l prints the nine switches as a string. After one character for the type (a dash means an ordinary file) come three letters for the owner, three for the group and three for others, always in the order r, w, x. A dash means that switch is off.
  4. Step 4: Which triplet applies?. When someone opens the file, the system picks one triplet. Are you the owner? Use the first. If not, are you in the group? Use the second. Otherwise use the third. So alice can write, bob can read but not write, and dave is refused even a look.
  5. Step 5: The same thing in numbers. Each triplet can be written as one digit. Count r as 4, w as 2 and x as 1, and add up the ones that are on: rw- is 6, r-- is 4, --- is 0. The whole string becomes 640. The digits only reach 7 (rwx), which is why this is called octal notation.
  6. Step 6: chmod and chown. chmod (change mode) sets the switches: chmod 660 turns on write for the group, and chmod g+w says the same in letters. Only the owner or the administrator, root, may do it. chown (change owner) hands the file to another user, and only root may do that, hence sudo.
  7. Step 7: On a directory, new meanings. A directory is a list of names, so its letters mean something different. r lets you list the names, x lets you enter it and reach what is inside, and w (together with x) lets you add, delete or rename entries. So deleting a file depends on the directory’s w, not on the file’s own permissions.
VALUES SO FAR
owneraliceuser
groupstaffgroup
actions····rule
string····ls -l
mode····octal
chmod····new mode
chown····new owner
dir····folder

Spotted a mistake on this page?