TT Lab
Blog Get started
Learn Learning paths Courses

Linux Fundamentals

Working With Permissions, Users and Groups

Continue in TT Lab

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

  1. Create /root/secret.txt and set its permissions to 600.
  2. Create the user student1, create the group devs, and add student1 to it as a supplementary group (keep the primary group as it is).
  3. Create /root/shared/report.txt and change its owner:group to student1:devs.
  4. Set the group of the /root/shared directory to devs and its permissions to 2770.
  5. Create the /root/shared/incoming directory and set its permissions to 1777.
  6. Create /root/shared/run.sh (first line #!/bin/bash, exits with 0 when run) and set it to 750, and on /root/shared/report.txt remove only read for others (o) so it becomes 640. Try doing it in symbolic notation.
  7. 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.
  8. 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.
  9. 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

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.