Learn › Build and operate services › Give the service only the access it needs
The clock has been sending ticks to the journal. Now it also needs to save them in a file. On this VM the clock runs under its own Linux account, camp-clock. When the program asks to create a file, Linux checks the permissions of that account.
A program must be able to pass through the directories on the way to a file. To create a new file, it also needs permission to write in the directory that will contain it. A directory with mode 700 gives its owner permission to list, change and enter it. The group and everyone else have no permissions there. An administrator using root privileges can still inspect it.
A private output directory. Read this illustrative example from the perspective of an administrator setting up a temperature meter. The example machine already has a service account and group named meter. The clock VM uses a different account. The meter needs to save one reading without giving every user access to it.
$ sudo mkdir /var/lib/meter
$ sudo chown meter:meter /var/lib/meter
$ sudo chmod 700 /var/lib/meter
$ ls -ld /var/lib/meter
drwx------ 2 meter meter 4096 Nov 1 10:00 /var/lib/meter
The first three commands create the directory, assign its owner and group, and give access to the owner alone. ls -ld lists the directory itself. The first field says the owner may list, write and enter. The next two names identify the owner and group. Now watch a write made under that account.
$ printf '18 C\n' | sudo -u meter tee /var/lib/meter/sample.log
18 C
$ sudo -u meter cat /var/lib/meter/sample.log
18 C
sudo -u meter runs one command as meter. Here tee receives the sample reading, writes it to a new file, and also prints it. Running cat afterward reopens the file, so the second 18 C shows the reading was saved. The directory remained private throughout.
Try a write as the clock account. In Terminal, make a separate practice directory. sudo -u camp-clock runs just the following command as the clock account. mktemp -d creates a new private directory with a unique name under the temporary directory. The shell saves its path in practice so you can reuse it.
practice=$(sudo -u camp-clock mktemp -d)
ls -ld "$practice"
printf 'first reading\n' | sudo -u camp-clock tee "$practice/sample.log"
sudo -u camp-clock ls -l "$practice/sample.log"
sudo -u camp-clock cat "$practice/sample.log"
In the first listing, find drwx------ and the owner camp-clock. tee receives the text from printf and writes it to the named file. The second listing tells you who owns that file. cat opens it again and reads the saved line. Now run the same read as your terminal account.
whoami
cat "$practice/sample.log"
whoami names the account making this last request. On the lab VM it is admin. Without sudo, that account gets a permission error because it cannot pass through the clock's private directory. If you are unsure why the two reads differ, compare the directory owner with the account running each cat. You can repeat this practice with a fresh temporary directory.
The real clock accepts --output PATH to append each tick to a file as well as printing it to the journal. The new destination is /var/lib/camp-clock/ticks.log. On this fresh VM the parent directory has not been created. Could the clock create a file at that path if you added the argument now? Explain which part of the path it would need first. Then inspect the machine.
id camp-clock
systemctl status camp-clock.service --no-pager
before_unit_run=$(systemctl show -P InvocationID camp-clock.service)
cat /etc/systemd/system/camp-clock.service
ls -ld /var/lib/camp-clock
id shows the supplied account and its primary group. On this VM, both are named camp-clock. status should show Active: active (running). The shell saves this run's ID as before_unit_run so you can compare it after the edit. The unit should contain User=camp-clock and ExecStart=/usr/local/bin/camp-clock, without --output. The final ls should report that the directory does not exist and return a nonzero exit status.
The missing directory explains the next change. Create it and assign it to the service account and its supplied primary group:
sudo mkdir /var/lib/camp-clock
sudo chown camp-clock:camp-clock /var/lib/camp-clock
sudo chmod 700 /var/lib/camp-clock
ls -ld /var/lib/camp-clock
mkdir creates the directory. chown sets its owner and group to camp-clock. chmod 700 lets the owner enter, read and write there while giving other ordinary accounts no directory access. Root can still administer it. The final ls -ld should show camp-clock as owner and group, with permissions beginning drwx------. Do not change /var/lib or give unrelated accounts access.
The existing process still runs without an output path. Change only the saved unit's ExecStart= line to:
ExecStart=/usr/local/bin/camp-clock --output /var/lib/camp-clock/ticks.log
Open the unit with the editor from Lesson 1:
SUDO_EDITOR=nano sudoedit /etc/systemd/system/camp-clock.service
Save with Ctrl+O, Enter, then exit with Ctrl+X. Inspect the saved unit:
cat /etc/systemd/system/camp-clock.service
Check that User=camp-clock remains and ExecStart= points inside the private directory. Predict whether this edit can change the arguments of the process already running. A process keeps the arguments it started with. The manager must read the edited unit, then start a new run.
sudo systemctl daemon-reload
sudo systemctl restart camp-clock.service
daemon-reload reads the edited unit. restart starts a process from that revised unit.
systemctl status camp-clock.service --no-pager
sudo ls -l /var/lib/camp-clock/ticks.log
sudo tail -n 3 /var/lib/camp-clock/ticks.log
run_id=$(systemctl show -P InvocationID camp-clock.service)
printf 'Before edit: %s\nAfter restart: %s\n' "$before_unit_run" "$run_id"
sudo journalctl _SYSTEMD_UNIT=camp-clock.service _SYSTEMD_INVOCATION_ID="$run_id" -n 10 --no-pager
In status, find Active: active (running), its current since time and Main PID. Both file commands need sudo because the directory is private. ls -l should show the new file owned by camp-clock. Compare the two printed run IDs. They should both contain a value and differ because the revised unit launched a new run. The journal command selects clock messages from that new run. Find the same time-stamped tick in both the file and the filtered journal.
For an independent account check, use the current Main PID from status in this command. Replace 1234 with that number:
ps -ww -o euser,pid,args -p 1234
ps inspects a process. -p selects its PID, -o chooses the columns, and -ww keeps the full command line visible. In the first column, find camp-clock, the effective account Linux uses for ordinary file permission checks. In the command arguments, find --output /var/lib/camp-clock/ticks.log. These observations connect the saved settings to the current process. The private directory and matching tick show what this process could write.
Explain why you chose this owner and directory, which change made the new argument take effect, and how you verified it. Use Check current service to compare the account, directory and current output with the machine check.
Come back to a different directory. On a later visit, leave the clock's configured output alone. Create a second private directory with handoff=$(sudo mktemp -d). This time root created it. The clock account needs to write a file named handoff.log there, and other ordinary accounts must still be kept out. Inspect the directory. Try writing a line as camp-clock and read the result. Which single directory setting needs to change? Make that change, verify a new line as the clock account, then try to read the same file as admin without sudo.
If you need a hint, compare ls -ld "$handoff" with the practice directory listing. Keep the mode private and use what you learned about changing a directory's owner. The write and read commands from the rehearsal work with the new path and filename. This return problem is for practice. The automatic check examines the clock's configured output in /var/lib/camp-clock.
References: systemd.exec, systemd.service, systemctl, chmod, chown, ps, journal fields, journalctl.
Practice Give the service only the access it needs in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.