Working With Permissions, Users and Groups
Goal
You create users and groups, set the special bits a shared directory needs, fix permissions in both numeric and symbolic notation, and even build a script that checks by itself whether permissions are as expected. In the last two steps you solve "Permission denied" incidents that service accounts run into.
Why it matters
Most permission incidents happen because people look only at "file permissions" and forget "directory permissions." What you need to delete a file is not write permission on that file but write permission on the directory that contains it. That is why even a read-only file can be deleted by someone else, and Unix added the sticky bit separately to close that hole. For the same reason, a directory used by several people needs setgid; without it, each person creates files with their own primary group and the situation where people cannot read each other's files keeps repeating.
The incident steps show how this principle actually blows up. Reports that "there is disk space left but writes fail" hide different causes behind the same message, and running it as root works fine, which makes it more confusing (root in this lab is not blocked by permission bits). The goal of these steps is the habit of becoming that account and following the path one step at a time. Opening it so anyone can write makes the symptom go away, but that is not a fix, it is a hole, and it does not pass.
Steps
- Create
/root/secret.txtand set its permissions to600. - Create the user
student1, create the groupdevs, and addstudent1to it as a supplementary group (keep the primary group as it is). - Create
/root/shared/report.txtand change its owner:group tostudent1:devs. - Set the group of the
/root/shareddirectory todevsand its permissions to2770. - Create the
/root/shared/incomingdirectory and set its permissions to1777. - Create
/root/shared/run.sh(first line#!/bin/bash, exits with 0 when run) and set it to750, and on/root/shared/report.txtremove only read for others (o) so it becomes640. Try doing it in symbolic notation. - Create
/root/shared/checkmode.sh <파일경로> <기대권한>(file path, expected permissions). It must be executable, and it must exit with code 0 when the permissions match and a non-zero code when they differ, printing the current permissions in that case. It must also exit with a non-zero code when the file does not exist. - Incident
lb-write-denied: three jobs cannot write their results as their own service accounts. Pick and record the cause for each job, and fix them without opening write access to everyone. - Incident
lb-group-share: members of the same team cannot open each other's new files in the shared folder. Pick and record the cause, and fix it so that both the current files and files uploaded in the future can be opened by each other.
Notes
stat -c %a 파일prints only the octal permissions. If a special bit is set, it comes out as four digits.- With
id -nG student1you can see the list of groups the user belongs to. - Incident tools:
incident listshows the list of incidents, andincident show <이름>(name) shows a ticket again. Raising the same incident again returns it to its original broken state. - The incident report is
/root/incident/<이름>/report.txt(name is the incident name). If there is one cause, writecause: <코드>(code); if there are several targets, write<대상>: <코드>(target: code) one per line, and at the end addevidence: <원인을 보여 준 출력 한 줄>(one line of output that showed the cause, copied as is). Pick the codes from the candidates listed in the step description. - To run a single command as another account, use
runuser -u <계정> -- <명령>(account, command). - Common mistake 1: in step 2, if you write
usermod -G devs student1without-a, all existing supplementary groups disappear, and if you use-g, the primary group changes. - Common mistake 2: in step 4, if you only run
chmod 770, the leading setgid digit is missing. Specify four digits. - Common mistake 3: in step 6,
o=empties the others field entirely, whileo-rremoves only read. When you want to "change just one thing and leave the rest as is", use + and -.
Lock down the secret file's permissions
Create /root/secret.txt and set its permissions to 600.
The three octal digits are owner, group, and others, in that order. What gives read and write access to the owner only?
Create a user and add it to a group
Create the user student1, create the group devs, and add student1 to it as a supplementary group. student1's primary group (student1) must stay as it is.
There are separate commands for adding a user (user + add), creating a group, and modifying an existing user. The option that sets supplementary groups "replaces" the whole list, so you must also give the "append" option; there is a separate option that changes the primary group, so do not mix them up. When you are done, check the primary and supplementary groups with id.
Change the owner and group
Create /root/shared/report.txt and change its owner:group to student1:devs.
The file must exist first (and so must its parent directory). The command that changes the owner can take both at once in the "owner:group" form. After changing it, check the third and fourth columns of ls -l.
Set setgid on the shared directory
Set the group of the /root/shared directory to devs and its permissions to 2770.
In the four octal digits, the leading digit is the special bit. Find the value responsible for group inheritance.
Set sticky on the upload directory
Create the /root/shared/incoming directory and set its permissions to 1777.
Check the permissions of /tmp with ls -ld. Make yours the same form.
Fix permissions in symbolic notation
Create /root/shared/run.sh (first line #!/bin/bash, exit code 0 when run) and set its permissions to owner rwx, group r-x, others nothing (750). Then, on /root/shared/report.txt from step 4, remove only read for others (o) so it becomes 640 (owner and group stay as they are). Try using symbolic notation instead of numbers.
Symbolic notation strings together "who" (u owner, g group, o others), "how" (+ add, - remove, = set entirely), and "what" (r w x), and joins multiple targets with commas. = sets that field entirely, while + and - change only those bits from the current value and leave the rest untouched, which makes them the right fit for "leave everything else as is and remove just one".
Write a permission check script
Create /root/shared/checkmode.sh <파일경로> <기대권한> (file path, expected permissions). It must be executable, and it must exit with code 0 when the permissions match and a non-zero code when they differ, printing the current permissions in that case. It must also exit with a non-zero code when the file does not exist.
The skeleton is: take the arguments → check whether the file exists → read the current permissions → compare with the expected value. Use stat's format specifier to read the permissions in octal only, and report the result through the exit code. On a mismatch, print the current permissions as well.
Incident: there is disk space left, but three jobs cannot write
Running incident lb-write-denied raises the incident. Jobs a, b, and c each run as their own service account (lbsvc-a, lbsvc-b, lbsvc-c) and write results, but all three get Permission denied (reproduce it with something like runuser -u lbsvc-a -- /srv/lbjobs/bin/job a). Disk space and inodes are plentiful. Find the cause for each job and fix it so all three accounts can write again. However, opening write access to everyone (o+w, 777 or 666) is not allowed, and you must not delete or recreate /srv/lbjobs/a/out.log, which is an audit record. In /root/incident/lb-write-denied/report.txt, write three lines, a: <코드>, b: <코드>, and c: <코드>, plus evidence: <관찰한 출력 한 줄> (one line of output you observed). Each job's code is one of the following: file-owner (the owner or permissions of the result file block that account), dir-no-exec (an intermediate directory on the path cannot be passed through), dir-no-write (a new file cannot be created in the directory), disk-full (not enough disk space or inodes).
Direction: do not check as root; root in this lab is not blocked by permission bits. Become that account and walk down the path one step at a time from the top to see where it is blocked. Tools: runuser, namei, stat, df. Concretely: namei -l <경로> (path) shows the permissions, owner, and group of each step of a path, one line each. A directory needs x to pass through and w to create a new name inside it, and appending to an existing file needs w on that file. Whether a job "appends to the same file" or "creates a new file every day" is written in /srv/lbjobs/bin/job.
Incident: same team, but they cannot open each other's new files in the shared folder
Running incident lb-group-share raises the incident. lbalice and lbbob are both in the lbteam group, and /srv/lbshare is an lbteam directory, yet opening each other's files gives Permission denied (runuser -u lbbob -- cat /srv/lbshare/plan.txt). Find the cause, make the three existing files openable by each other, and fix it so files uploaded in the future work the same way. Opening access to people outside the team (others) and changing file owners are not allowed. In /root/incident/lb-group-share/report.txt, write cause: <코드> and evidence: <관찰한 출력 한 줄> (one line of output you observed). The code is one of the following: setgid-missing (the directory does not pass its group on to new files), umask-too-strict (group permissions are dropped at creation), not-in-group (not a member of the group), sticky-bit (the sticky bit blocks it).
Direction: "can the group read it?" is decided by both the file's group permission bits and which group the file belongs to. Eliminate the four candidates one by one with evidence. Tools: id, ls -l, stat. Concretely: look at the groups the two people belong to with id, at the permissions, owner, and group of each file with a long listing of the shared directory, and at the permission string (%A) and group (%G) of the directory itself. Check whether the file's group field is lbteam, whether the group permissions include r, and whether any of the directory's permission bits decides the group of new files. If you fix only the existing files, the same thing happens with tomorrow's new files.