Skip to content

Block B: permissions and users (7-16)

Permissions

🔴 Three digits: owner, group, others. r=4, w=2, x=1. Linux checks only the first matching class.

Task 07: public and private

cd /srv/linux-practice/files && echo "public info" > public.txt && echo "private info" > private.txt
chmod 644 public.txt && chmod 600 private.txt && ls -l public.txt private.txt

✅ -rw-r--r-- and -rw-------.

Task 08: removing write permission

echo "first" > write-test.txt && chmod u-w write-test.txt && ls -l write-test.txt
echo "second" >> write-test.txt          # Permission denied
chmod u+w write-test.txt && echo "second" >> write-test.txt && cat write-test.txt

💡 The file was -r--rw-r--: the group could write, yet the owner was refused.

✅ The message is Permission denied, not No such file: the cause is permissions.

Task 09: directory without x

mkdir dir-test && echo "inside" > dir-test/note.txt
chmod 600 dir-test && cat dir-test/note.txt     # Permission denied
chmod 700 dir-test && cat dir-test/note.txt     # inside

🔴 On a directory, x means you may pass through it.

Task 10: three users

for u in labuser1 labuser2 labguest; do sudo useradd -m -s /bin/bash "$u"; done
ls -ld /home/labuser1 /home/labuser2 /home/labguest

💡 We do not add them to the sudo group. No password is needed: we open sessions with sudo -iu.

Task 11: UID, groups, home, shell

for u in labuser1 labuser2 labguest; do id "$u"; getent passwd "$u"; done

getent passwd fields: name : x : UID : GID : comment : home : shell.

Task 12: labteam

sudo groupadd labteam && sudo usermod -aG labteam labuser1 && sudo usermod -aG labteam labuser2
getent group labteam; id labuser1; id labuser2; id labguest

🔴 -aG appends. -G alone removes the other supplementary groups.

Task 13: shared folder

sudo chgrp labteam /srv/linux-practice/shared && sudo chmod 770 /srv/linux-practice/shared
ls -ld /srv/linux-practice /srv/linux-practice/shared
flowchart TD
    P["/srv/linux-practice<br>root root, 755<br>everyone can pass"] --> S["shared<br>root labteam, 770"]
    S --> M["labuser1, labuser2<br>enter and create"]
    S -. "denied" .- G["labguest"]

Task 14: team.txt

sudo -iu labuser1
cd /srv/linux-practice/shared && echo "line from labuser1" > team.txt && chgrp labteam team.txt && chmod 660 team.txt
exit
sudo -iu labuser2
cd /srv/linux-practice/shared && cat team.txt && echo "line from labuser2" >> team.txt && cat team.txt
exit

✅ -rw-rw---- labuser1 labteam, both lines are there.

🔴 Group membership takes effect in a new session.

Task 15: the outsider

sudo -iu labguest
cd /srv/linux-practice/shared; cat /srv/linux-practice/shared/team.txt
exit

✅ Permission denied on both.

Task 16: owner writes, group reads

cd /srv/linux-practice && sudo touch shared/owner-test.txt
sudo chown labuser2:labteam shared/owner-test.txt && sudo chmod 640 shared/owner-test.txt && sudo ls -l shared

✅ labuser2 writes. labuser1 reads, but writing gives Permission denied: with 640 the group has only r.