Service Isolation with systemd
You'll learn how to sandbox system services using systemd's built-in security directives to limit blast radius if a service is compromised.
What Is Service Isolation?
Think of systemd service isolation like putting each service in its own secure compartment on a submarine—if one compartment floods, watertight doors prevent the entire vessel from sinking. Systemd provides security directives that restrict what resources a service can access, even if an attacker gains control of that service process.
Core Isolation Directives
PrivateTmp=yes creates a unique, isolated /tmp directory for the service. This prevents attackers from reading temporary files created by other services or placing malicious files where other processes might find them.
ProtectHome=yes makes /home, /root, and /run/user inaccessible to the service (or read-only with ProtectHome=read-only). Most system services have no legitimate reason to access user data—this directive enforces that boundary.
NoNewPrivileges=yes prevents the service and its child processes from gaining additional privileges through mechanisms like SUID binaries or file capabilities. Once set, there's no escalation path, even if the service is exploited.
Implementing in Service Files
Add these directives to your service unit files (typically in /etc/systemd/system/):
[Service]
PrivateTmp=yes
ProtectHome=yes
NoNewPrivileges=yes
After modifying a service file, reload systemd configuration and restart the service to apply changes.
The Defense-in-Depth Value
These directives create multiple layers of defense. Even if an attacker exploits a vulnerability in your web server, they can't read SSH keys from /root, can't plant backdoors in shared /tmp space, and can't escalate privileges through traditional means.
Key Takeaway: Systemd security directives like PrivateTmp, ProtectHome, and NoNewPrivileges isolate services into restricted sandboxes, dramatically reducing what an attacker can access or achieve if they compromise a single service process.