How do you sandbox a script that runs as a systemd service?
AdvancedConceptcron and systemd timers
Answer
[Service]
User=opskit
Group=opskit
NoNewPrivileges=yes
ProtectSystem=strict # whole filesystem read-only...
ReadWritePaths=/var/lib/opskit /var/log/opskit # ...except these
ProtectHome=read-only
PrivateTmp=yes # private /tmp, defeats /tmp races
PrivateDevices=yes
CapabilityBoundingSet=
MemoryMax=256M
CPUQuota=20%
systemd-analyze security name.service scores the unit's exposure. Sandboxing turns a compromised or buggy script from "root on the box" into "can write two directories".
More on cron and systemd timers
Explain the five cron time fields with examples.BeginnerWhat is different about the environment a cron job runs in?BeginnerWhat is the difference between a user crontab, `/etc/crontab`, `/etc/cron.d/` and `/etc/cron.daily/`?IntermediateHow do you prevent overlapping cron runs and capture output properly?IntermediateWhat is a systemd timer and how does it compare to cron?IntermediateWrite a minimal systemd service and timer that run a backup at 02:30 daily.Intermediate