Linux Basics Lesson 17 of 38
Reading and setting permissions
9 minutes
Learning goals
- Read the permission block of ls -l for owner, group and others.
- Set permissions with chmod, as numbers and as letters.
- Explain what x means on a folder.
Reading the block
ls -l deploy.sh
-rw-r--r-- 1 lerner lerner 34 Oct 17 08:30 deploy.sh
The first character is the type: - for a file, d for a folder, l for a symlink. Then come three groups of three: owner, group, others. rw-r--r-- means the owner may read and write,
the group and everyone else may only read. The two names after the link count are the owner and the group.
chmod with numbers
Each group is the sum of read = 4, write = 2, run = 1. So 7 is rwx, 6 is rw-, 5 is r-x, 4 is r-- and 0 is nothing. stat -c %a prints the three digits directly.
stat -c %a secrets.env
644
chmod 600 secrets.env
ls -l secrets.env
-rw------- 1 lerner lerner 23 Oct 17 08:30 secrets.env
chmod with letters
Letters change one thing and leave the rest alone: u, g, o and a for owner, group, others and all, +, - and = for add, remove and set. On a script, this is the usual move. Look at it before and after:
chmod +x deploy.sh
Purpose: Sets who may read, write and run a file.
Syntax: chmod [-R] MODE FILE...
Options:
Example:
ls -l deploy.sh
-rw-r--r-- 1 lerner lerner 34 Oct 17 08:30 deploy.sh
chmod +x deploy.sh
ls -l deploy.sh
-rwxr-xr-x 1 lerner lerner 34 Oct 17 08:30 deploy.shStarting it comes in the scenario at the end of this module. This page does not load the script package, so ./deploy.sh only gives you a hint here.
x on a folder means "enter"
On a folder, r lets you list the names, and x lets you enter it and reach what is inside. Without x you stay outside, even as its owner:
mkdir box && chmod 644 box
cd box
bash: cd: box: Permission denied
chmod 755 box && cd box && pwd
/home/lerner/box
Practice
Terminal task 1/2
Make deploy.sh executable for everyone. Reading and writing stay as they are, so it ends up as 755.
Task 1/2: Make deploy.sh executable for everyone. Reading and writing stay as they are, so it ends up as 755.
Make deploy.sh executable for everyone. Reading and writing stay as they are, so it ends up as 755.
Task complete
Solution
chmod +x deploy.sh Why: Programs create files without the right to run them. That x is a decision someone makes on purpose.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 2/2
secrets.env holds a token, and right now everyone on the machine can read it. Make it readable and writable for you only.
Task 2/2: secrets.env holds a token, and right now everyone on the machine can read it. Make it readable and writable for you only.
secrets.env holds a token, and right now everyone on the machine can read it. Make it readable and writable for you only.
Task complete
Solution
chmod 600 secrets.env Why: Tools such as ssh refuse key files that others may read, for exactly this reason.
Another way
chmod go-r secrets.env Letters only take away what was there. The number sets the end result, whatever the file looked like before. For secrets the number is the safer habit.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Fill the gap
As digits, rw-r----- is:
As digits, rw-r----- is: …
Check works once you have typed an answer.
Predict the output
Command
chmod u+x,g-r deploy.sh && stat -c %a deploy.shBefore the command, in your home folder ~
- ~/deploy.sh
Check works once you have chosen an answer.
Find the error
This command answers with an error. Tap the part that is wrong.
Parts of the command
Check works once you have tapped a part.
Next up: who owns a file, and why new files start out as 644.
Quiz
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
Cards
-
Card 1 of 3
Tap the card or press Space to turn it over.
-
Card 2 of 3
Tap the card or press Space to turn it over.
-
Card 3 of 3
Tap the card or press Space to turn it over.
End of the lesson
Still open: 5 exercises, 3 quiz questions
What you can do now: chmod +x deploy.sh
+0 XP in this lesson
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: open
- Lesson 5: open
- Lesson 6: open
- Lesson 7: open
- Lesson 8: open
- Lesson 9: open
- Lesson 10: open
- Lesson 11: open
- Lesson 12: open
- Lesson 13: open
- Lesson 14: open
- Lesson 15: open
- Lesson 16: open
- Lesson 17: done
- Lesson 18: open
- Lesson 19: open
- Lesson 20: open
- Lesson 21: open
- Lesson 22: open
- Lesson 23: open
- Lesson 24: open
- Lesson 25: open
- Lesson 26: open
- Lesson 27: open
- Lesson 28: open
- Lesson 29: open
- Lesson 30: open
- Lesson 31: open
- Lesson 32: open
- Lesson 33: open
- Lesson 34: open
- Lesson 35: open
- Lesson 36: open
- Lesson 37: open
- Lesson 38: open
Cheat sheet
-
chmod 640 <file>set the three digits -
chmod +x <file>make it executable -
chmod go-r <file>take read away from group and others -
stat -c %a <file>print the mode as digits
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 18 KB)
- View as image (PNG, 52 KB)