Permissions

INPUT · Slides

Change permissions with numbers (`chmod 755`)

01 / 08

This is what you see in the field

You can use chmod u+x now. But look at a set of instructions someone wrote and it usually says this.

chmod 755 run.shchmod 644 index.htmlchmod 600 ~/.ssh/id_rsa

Numbers. You meet these more often than the symbols.

Without knowing what they mean they look like an incantation. And then you type the chmod 777 you found in a search.

Today puts an end to that. The number is the answer to a sum. Once the mechanism makes sense, there are only three things left to memorise.

02 / 08

r is 4, w is 2, x is 1

These three are all you memorise.

PermissionNumber
r (can read)4
w (can write)2
x (can execute)1

Then add up what you have.

PermissionSumNumber
rwx4+2+17
rw-4+26
r-x4+15
r--44
-wx2+13
-w-22
--x11
---00

Only eight possibilities, 0 to 7. There are eight combinations of three characters, which is exactly what one digit holds.

03 / 08

Why 4, 2 and 1

They look like odd numbers to choose, but there is a reason: no two sums come out the same.

Add 1, 2 and 4 in any combination and every answer differs.

4      5      6      7r--    r-x    rw-    rwx

See a 5 and you know it is 4 and 1, so r-x. Working backwards gives exactly one answer. Had the numbers been 1, 2 and 3, a 3 could be "3" or "1+2" and you could not tell.

This is binary thinking. Look at the three characters of rwx as three digits of "present = 1, absent = 0" and r-x is 101. In binary, 101 is 5. If you did base conversion in the IT fundamentals course, this is where it joins up.

04 / 08

Three digits for three people

One digit holds three characters, so three digits hold all nine.

chmod 6 4 4  memo.txt      ^ ^ ^   owner group other

The order is the same as with the symbols: owner, then group, then everyone else.

Let us read 644.

DigitNumberPermissions
owner6rw- (4+2)
group4r--
other4r--

Joined up that is -rw-r--r--, the same shape as the first one you read last lesson.

644 and rw-r--r-- say the same thing. There are simply two names for it.

05 / 08

Learn only the four you use

There are 512 possibilities, but in practice it is nearly always these four.

NumberSymbolsWhere you use it
644-rw-r--r--an ordinary file (the default)
755-rwxr-xr-xthings you run, and directories
600-rw-------secrets (keys, passwords)
700-rwx------a box for you alone

Compare them and a rule appears: the last two digits are usually the same, because the group and everyone else are usually treated alike.

  • 755 — everything for you, read and run for everyone
  • 644 — read and write for you, read for everyone
  • 600 — you alone, nothing for anyone else

Those four cover nine days out of ten. For the rest, look at the table.

06 / 08

Why 777 is too strong

chmod 777 turns up in searches all the time. It means "anyone in the world may read, write and execute this".

777 = rwx rwx rwx

What is the trouble with that?

1. Anyone can rewrite it — the contents can be swapped out
2. Anyone can run it — whatever was planted in it runs
3. It is far too strong for the problem — even when it works, you do not know why

Type 777 and most Permission denied messages vanish. But that is taking the door off because the key would not turn.

The right fix is to add the one character that is missing, as you did with u+x last lesson. Or to match a shape that means something, like 644 or 755.

Having been through this chapter, you need never type 777 again, because you can read which permission is missing in which digit.

07 / 08

Numbers or symbols?

You can use both now, so let us settle when to use which.

SituationWhich suits
adding the one missing charactersymbols (u+x)
bringing things to a set shapenumbers (644)
writing a set of instructionsnumbers (one definite result)
when you do not know the current statenumbers (you can state the answer)

Symbols move you from where you are; numbers ignore where you are and decide. Which is why instructions are written in numbers: whoever runs them, from whatever state, gets the same result.

For reading, you need both, because other people's instructions are mostly numbers and explanations of errors are mostly symbols.

Get so you can turn a number into symbols and symbols into a number in your head. Today's practice does exactly that.

08 / 08

Now have a go

Your home holds the same things as last lesson.

  • run.sh — the file you want to run
  • memo.txt — an ordinary note
  • secret.txt — one you want kept secret
  • docs/ — a box

When you see a number, split it in three in your head and undo the sums. 751 is "7 = rwx, 5 = r-x, 1 = --x".

One more handy tool for checking: stat -c %a puts the permissions out as a number (%A was the symbols). Instruct in numbers and check in numbers, which makes marking your own work easy.