Linux Labs

Users, permissions, and processes / Stage 3 of 13

Changing and diagnosing permissions

Not running Starting usually ~20 s
Leave the lesson

Step 3 of 8 · on a machine · 5 min

Mode as a number

A triple can be written as one digit, and the whole mode as three. That way you set the permissions in full with a single command.

Each letter has a weight: r is 4, w is 2, x is 1. A triple's digit is the sum of the allowed letters: rw- is 6, r-x is 5, r-- is 4, --- is 0. The three digits follow the triples' order: owner, group, others. 640 is rw-r-----.

bash
stat -c '%a %A %n' /etc/passwd /etc/shadow

stat prints the mode as a number and as a string: 644 -rw-r--r-- for /etc/passwd and 640 -rw-r----- for /etc/shadow.

chmod 640 file sets all three triples at once. Symbolic notation changes one place; a number sets the whole mode. A number is handy when you know what the whole mode should become.

Warning

chmod -R 777 directory gives everyone read, write, and run on every file in the tree. The denial goes away, and so does the protection: anyone can change anyone's files.

Check yourself 1 question

What will stat -c '%A' deploy.sh print after chmod 750 deploy.sh?

  • The digits follow the triples' order: owner, group, others. The first, 7, belongs to the owner, and the last, 0, to others.

  • rw- is 6 (4 + 2). The group's digit is 5: 4 + 1, which is r-x.

  • The owner has 7, all three letters: 4 + 2 + 1. rw- would be 6.

7 = 4 + 2 + 1 gives the owner rwx, 5 = 4 + 1 gives the group r-x, and 0 gives others ---. Try it on any file of yours: chmod 750 file, then stat -c '%A' file.

Key points

  • A triple's digit is the sum of the weights: r is 4, w is 2, x is 1; the three digits are owner, group, others.
  • Symbolic notation changes one place in the string; a number sets the whole mode.
Ubuntu 24.04
Loading…