Linux course index · Linux Advanced 2 category · Lesson 1 of 7
AI disclosure — AI-generated content, posted as is. AI can make mistakes. This extension adds new material and links related posts from this site. The newly written explanations and examples may be freely reused. Linked source material retains its own terms.
Learning goal
Run a small systemd service with a dedicated dynamic identity and a narrow writable directory.
Concepts and setup
The site’s boot, logging, and SELinux posts explain separate layers of service control. This extension adds systemd isolation. DynamicUser allocates a temporary identity. StateDirectory provides one managed writable path. ProtectSystem=strict otherwise makes the service’s filesystem view mostly read-only. NoNewPrivileges blocks privilege gains through execution. Use a disposable systemd VM, an unused unit name, and console access. Check application behavior separately from the security score.
Write and start the restricted unit
sudo tee /etc/systemd/system/oe-linux-hardening.service >/dev/null <<'EOF'
[Unit]
Description=Open Education service isolation lab
[Service]
Type=oneshot
DynamicUser=yes
StateDirectory=oe-linux-hardening
ProtectSystem=strict
ProtectHome=yes
NoNewPrivileges=yes
PrivateTmp=yes
ExecStart=/usr/bin/touch /var/lib/oe-linux-hardening/check
ExecStart=/usr/bin/echo state-write-ok
EOF
sudo systemd-analyze verify /etc/systemd/system/oe-linux-hardening.service
sudo systemctl daemon-reload
sudo systemctl start oe-linux-hardening.service
sudo journalctl -u oe-linux-hardening.service -n 8 --no-pager
sudo test -f /var/lib/oe-linux-hardening/check && printf 'proof exists\n'
Possible output
... state-write-ok
... Finished Open Education service isolation lab.
proof exists
StateDirectory is an explicit writable exception to the service’s read-only view. A successful oneshot can be inactive after the command exits.
Inspect controls and remove the fixture unit
systemctl show oe-linux-hardening.service -p DynamicUser -p NoNewPrivileges -p ProtectSystem
systemd-analyze security oe-linux-hardening.service
sudo systemctl stop oe-linux-hardening.service
sudo rm /etc/systemd/system/oe-linux-hardening.service
sudo systemctl daemon-reload
Possible output
DynamicUser=yes
NoNewPrivileges=yes
ProtectSystem=strict
...
Overall exposure level for oe-linux-hardening.service: ...
The score is a checklist aid, not a guarantee. Removing the unit does not remove persistent state. Retire the named lab directory after recording results.
Questions only
Quick-check questions
- Which directive supplies the writable directory?
- Does a good security score demonstrate that the application works?
Assessment questions
- Why use controls and an application check?
- Name two controls that restrict this unit at the operating-system level.
- ProtectHome breaks a service. How might you keep that control without breaking the service?
Questions and answers
Quick-check answers
- Which directive supplies the writable directory?
Answer — StateDirectory, here for oe-linux-hardening. - Does a good security score demonstrate that the application works?
Answer — No. Verify the task outcome and logs.
Assessment answer key
- Why use controls and an application check?
Answer — Controls reduce what is accessible. A check confirms the service can still do its intended work. - Name two controls that restrict this unit at the operating-system level.
Answer — Examples include DynamicUser, ProtectSystem, NoNewPrivileges, ProtectHome, and PrivateTmp. - ProtectHome breaks a service. How might you keep that control without breaking the service?
Answer — Move only necessary inputs to a managed application directory with narrowly scoped access and retest.
Related source articles on this site
- Q & A: Linux: Switch Users, Boot Process, File System
- Define and describe Selinux in general terms
- Experiment with Firewalls in Linux (CentOS/RedHat) #DevOps #RHCSA
- ইউনিক্সঃ সিস্টেম লগিং (Unix – System Logging)
Technical references
Continue the course
Next lesson 02 — Rootless Containers with Podman.
Linux course index · Linux Advanced 2 category · Lesson 1 of 7
