terminal.cab

Linux Basics Lesson 18 of 38

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

Owners and the starting mode

9 minutes

Learning goals

Who are you, and who owns this?

Every process runs as a user with a set of groups. id shows the numbers and names, groups only the names.

id
uid=1000(lerner) gid=1000(lerner) groups=1000(lerner),4(adm),27(sudo)
groups
lerner adm sudo

chown and chgrp

chgrp changes the group. You may do that yourself if the file is yours and you are a member of the new group. chown changes the owner, and giving a file away is something only root may do:

chgrp adm deploy.sh

Purpose: Sets who owns a file (chown) or which group it belongs to (chgrp).

Syntax: chown [-R] OWNER[:GROUP] FILE... | chgrp [-R] GROUP FILE...

Options:

  • -R also everything inside a folder

Example:

chgrp adm deploy.sh
ls -l deploy.sh
-rw-r--r-- 1 lerner adm 34 Oct 17 08:30 deploy.sh
chown root deploy.sh
chown: changing ownership of 'deploy.sh': Operation not permitted
ls -l /srv/shared/data.txt
-rw-r--r-- 1 root root 12 Oct 17 08:30 /srv/shared/data.txt
chown lerner /srv/shared/data.txt
chown: changing ownership of '/srv/shared/data.txt': Operation not permitted

in the glossary: chgrp

Both attempts with chown fail with Operation not permitted. On a real server, this job needs sudo chown, and that is the next lesson. chown OWNER:GROUP sets both at once, the classic after a root deployment being sudo chown -R www-data:www-data /var/www/app.

umask: the mask on new files

A program that creates a file usually asks for 666, a folder for 777. Before that takes effect, the kernel covers it with the umask: every bit that is set in the umask is taken away. Nothing more happens than this.

umask

Purpose: Shows or sets the mask that takes rights away from new files and folders.

Syntax: umask [-S] [MODE]

Options:

  • -S show what stays, as letters

Example:

umask
0022
umask -S
u=rwx,g=rx,o=rx
touch new.txt && mkdir newdir && stat -c %a new.txt newdir
644
755

in the glossary: umask

666 & ~022 = 644 and 777 & ~022 = 755, read as "666 with the bits of 022 taken away". umask -S shows the same mask from the other side: what is allowed to stay. Now a mask where digit-by-digit arithmetic would mislead you:

umask 033 && touch a && mkdir d && stat -c %a a d
644
744

Practice

Terminal task 1/2

deploy.sh should belong to the group adm, so that the admins can read it as a team. You stay the owner.

Task 1/2: deploy.sh should belong to the group adm, so that the admins can read it as a team. You stay the owner.

deploy.sh should belong to the group adm, so that the admins can read it as a team. You stay the owner.

Loading terminal …

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

Esc, then Tab leaves the terminal

Not done yet

Terminal task 2/2

Set the umask to 027 and create the empty file private.txt in the same shell. Group may read, others get nothing.

Task 2/2: Set the umask to 027 and create the empty file private.txt in the same shell. Group may read, others get nothing.

Set the umask to 027 and create the empty file private.txt in the same shell. Group may read, others get nothing.

Loading terminal …

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

Esc, then Tab leaves the terminal

Not done yet

Fill the gap

With umask 077, a new file gets the mode:

With umask 077, a new file gets the mode: …

Check works once you have typed an answer.

Predict the output

Command

umask 027 && mkdir team && stat -c %a team

Before the command, in your home folder ~

  • ~/.bashrc
  • ~/.profile
  • ~/.secret
  • ~/Documents/
  • ~/logs/
  • ~/notes.txt
  • ~/projects/
  • ~/archive/
  • ~/deploy.sh
  • ~/secrets.env
What does umask 027 && mkdir team && stat -c %a team 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: how to do the things that need root, one command at a time.

Quiz

  1. Question 1 of 3Why is a new file 644 at umask 022?

    Check works once you have chosen an answer.

  2. Question 2 of 3Which mode does a new file get at umask 033?

    Check works once you have chosen an answer.

  3. Question 3 of 3You run chown root on your own file and get Operation not permitted. Why?

    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 19, Root for a moment (9 min)

What you can do now: chgrp adm deploy.sh, umask

+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: open
  18. Lesson 18: done
  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 19, Root for a moment (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.