#Why this matters
Even with tight file permissions and hardened SSH access, a compromised daemon running as root with full filesystem access can read credentials, modify binaries, and plant persistence. Isolating services limits what a hijacked process can touch, while kernel-level auditing records what actually happened when an intrusion occurs.
Combining systemd sandboxing with auditd closes both gaps: you shrink each service's blast radius before an attack and leave tamper-resistant forensic breadcrumbs if one succeeds.
#Inspecting processes and service exposure
Every running process carries an effective user ID (euid), a parent process ID (ppid), and a set of Linux capabilities. Before tightening a service, inspect what runs, who owns it, and which ports it binds:
$ ps -eo pid,ppid,user,euid,comm --sort=-pid | head -n 6
PID PPID USER EUID COMMAND
2418 1 www-data 33 report-api
2305 1 root 0 sshd
$ ss -tulpn | grep 8080
tcp LISTEN 0 128 127.0.0.1:8080 0.0.0.0:* users:(("report-api",pid=2418,fd=6))
$ systemd-analyze security report-api.service | tail -n 3
-> Overall exposure level for report-api.service: 9.2 UNSAFEA score near 10.0 (UNSAFE) means the unit runs largely unrestricted. Even if User=reportsvc is set, the process can still see /home, potentially probe /etc or /tmp, or execute SUID helpers unless sandboxed.
#Sandboxing services with systemd drop-ins
Never edit vendor unit files in /usr/lib/systemd/system/ directly; package updates overwrite them. Instead, create a drop-in override using systemctl edit report-api.service, which writes to /etc/systemd/system/report-api.service.d/override.conf:
[Service]
User=reportsvc
NoNewPrivileges=yes
PrivateTmp=yes
ProtectHome=yes
ProtectSystem=strict
ReadWritePaths=/var/lib/report-api
CapabilityBoundingSet=
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX| Directive | Effect on the running service |
|---|---|
NoNewPrivileges=yes |
Blocks SUID/SGID and file-capability privilege escalation |
PrivateTmp=yes |
Mounts isolated /tmp and /var/tmp directories for the unit |
ProtectHome=yes |
Makes /home, /root, and /run/user inaccessible |
ProtectSystem=strict |
Mounts the whole filesystem read-only except /dev, /proc, /sys |
ReadWritePaths=... |
Re-grants write access strictly to the service's data directory |
CapabilityBoundingSet= |
Drops all Linux capabilities when left empty |
Reload and re-check the exposure score after applying the drop-in:
sudo systemctl daemon-reload
sudo systemctl restart report-api.service
systemd-analyze security report-api.serviceAlways pair service sandboxing with journalctl -u report-api.service -n 30 right after restarting so you catch legitimate permission denials before moving on.
#Kernel auditing with auditd and auditctl
Standard application logs tell you what a program chose to print; auditd logs what the kernel observed in /var/log/audit/audit.log. Rules are managed temporarily with auditctl and persisted in /etc/audit/rules.d/*.rules (applied via augenrules --load).
Two rule styles cover most investigations:
- File watches (
-w): track reads, writes, executions, or attribute changes (-p rwxa) on sensitive paths. - Syscall rules (
-a): trigger when specific system calls finish, filtered by architecture, UID, or exit status.
$ sudo auditctl -w /etc/ssh/sshd_config -p wa -k sshd_cfg
$ sudo auditctl -a always,exit -F arch=b64 -S execve -F euid=0 -k root_exec
$ sudo auditctl -l
-w /etc/ssh/sshd_config -p wa -k sshd_cfg
-a always,exit -F arch=b64 -S execve -F euid=0 -k root_exec- auditctl ruleKernel audit hook | /var/log/audit/audit.log | ausearch -k <key> -i
#Reading audit records with ausearch
Raw audit.log lines store epoch timestamps and numeric IDs. Pass -i (interpret) to ausearch to decode timestamps, usernames, and syscall names, and -k to match a rule key:
$ sudo ausearch -k sshd_cfg -i | tail -n 6
----
type=PROCTITLE msg=audit(11/18/2026 14:22:09.314:412) : proctitle=vi /etc/ssh/sshd_config
type=PATH msg=audit(11/18/2026 14:22:09.314:412) : item=0 name=/etc/ssh/sshd_config inode=131098 ...
type=CWD msg=audit(11/18/2026 14:22:09.314:412) : cwd=/home/operator
type=SYSCALL msg=audit(11/18/2026 14:22:09.314:412) : arch=x86_64 syscall=openat success=yes exit=3 ... auid=operator uid=root gid=root euid=root tty=pts1 comm=vi exe=/usr/bin/vi subj=unconfined key=sshd_cfgNotice three critical details grouped under the shared event ID 412:
auid=operatorshows the original authenticated user, even thoughuid=rootandeuid=rootshow the command ran viasudo.comm=viandexe=/usr/bin/vireveal both the command name and the exact binary path executed.PATHandCWDshow which file was touched and from which working directory.
#Common mistakes
- Forgetting
ReadWritePaths=withProtectSystem=strict: the service fails on startup when trying to write state or PID files. Checkjournalctl -u <unit>immediately after restarting. - Confusing
uidwithauid: if an attacker escalates viasudoor a misconfigured SUID binary,uid/euidbecomesroot, whileauidstays anchored to the login account (orunsetfor unauthenticated daemons). - Running
auditctlwithout saving to/etc/audit/rules.d/: rules added only withauditctlvanish on reboot unless persisted and loaded viaaugenrules --load. - Logging every
execvewithout filters: broad syscall rules withouteuid=0or directory scope can fill/var/log/auditand trigger disk-full audit actions.
#Summary
- Inspect process ownership,
ppid, and listening sockets (ps,ss) before changing service privileges. - Measure unit exposure with
systemd-analyze securityand apply drop-in overrides (NoNewPrivileges,ProtectSystem=strict,ProtectHome,PrivateTmp,CapabilityBoundingSet) viasystemctl edit. - Deploy
auditctlfile-watch (-w) and syscall (-a) rules with-kkeys, and persist them under/etc/audit/rules.d/. - Use
ausearch -k <key> -ito linkSYSCALL,PATH, andPROCTITLErecords and rely onauidto identify the originating login session.