Linux Basics Lesson 18 of 38
Owners and the starting mode
9 minutes
Learning goals
- Read who you are and which groups you are in with id and groups.
- Change the group with chgrp and know when chown needs root.
- Explain the umask as a mask that takes bits away from 666 and 777.
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:
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 permittedBoth 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:
Example:
umask
0022
umask -S
u=rwx,g=rx,o=rx
touch new.txt && mkdir newdir && stat -c %a new.txt newdir
644
755666 & ~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.
Task complete
Solution
chgrp adm deploy.sh Why: You may hand a file of yours to a group you belong to. Giving it to another user is something only root may do, because otherwise you could plant files on someone else.
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
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.
Task complete
Solution
umask 027
touch private.txt Why: 027 is the common choice on hardened servers. The mask works when a file is created, not afterwards.
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
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 teamBefore the command, in your home folder ~
- ~/.bashrc
- ~/.profile
- ~/.secret
- ~/Documents/
- ~/logs/
- ~/notes.txt
- ~/projects/
- ~/archive/
- ~/deploy.sh
- ~/secrets.env
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
-
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: chgrp adm deploy.sh, umask
+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: open
- Lesson 18: done
- 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
-
chgrp <group> <file>change the group -
sudo chown <owner>:<group> <file>change owner and group -
umaskshow the mask -
idwho am I, in which groups
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)