terminal.cab

Linux Basics Lesson 17 of 38

guestLevel 1Badges and progress0/100 XP, XP to the next levelStreak0

Reading and setting permissions

9 minutes

Learning goals

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:

  • -R also everything inside a folder
  • +x add the right to run it, for everyone the umask allows
  • 640 the three digits: owner, group, others

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.sh

in the glossary: chmod

Starting 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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.sh

Before the command, in your home folder ~

  • ~/deploy.sh
What does chmod u+x,g-r deploy.sh && stat -c %a deploy.sh print?

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

  1. Question 1 of 3What does drwxr-x--- tell you?

    Check works once you have chosen an answer.

  2. Question 2 of 3You have r but not x on a folder. What happens?

    Check works once you have chosen an answer.

  3. Question 3 of 3A web app cannot write its log and a colleague suggests chmod -R 777. What is the better first step?

    Check works once you have chosen an answer.

Cards

  1. Card 1 of 3

    Tap the card or press Space to turn it over.

  2. Card 2 of 3

    Tap the card or press Space to turn it over.

  3. 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

Next: lesson 18, Owners and the starting mode (9 min)

What you can do now: chmod +x deploy.sh

+0 XP in this lesson

  1. Lesson 1: open
  2. Lesson 2: open
  3. Lesson 3: open
  4. Lesson 4: open
  5. Lesson 5: open
  6. Lesson 6: open
  7. Lesson 7: open
  8. Lesson 8: open
  9. Lesson 9: open
  10. Lesson 10: open
  11. Lesson 11: open
  12. Lesson 12: open
  13. Lesson 13: open
  14. Lesson 14: open
  15. Lesson 15: open
  16. Lesson 16: open
  17. Lesson 17: done
  18. Lesson 18: open
  19. Lesson 19: open
  20. Lesson 20: open
  21. Lesson 21: open
  22. Lesson 22: open
  23. Lesson 23: open
  24. Lesson 24: open
  25. Lesson 25: open
  26. Lesson 26: open
  27. Lesson 27: open
  28. Lesson 28: open
  29. Lesson 29: open
  30. Lesson 30: open
  31. Lesson 31: open
  32. Lesson 32: open
  33. Lesson 33: open
  34. Lesson 34: open
  35. Lesson 35: open
  36. Lesson 36: open
  37. Lesson 37: open
  38. Lesson 38: open
1 of 38

Next: lesson 18, Owners and the starting mode (9 min) That's enough for today

Path complete

You finished all 38 lessons of Linux Basics.

Back to the path

Cheat sheet

The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.