brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ProtectSystem=strict makes the file system read-only for a systemd service

Systemd
~/tips/tip-systemd-protectsystem-strict-readonly-disk

ProtectSystem=strict

Mounts the file system read-only for one service and re-opens only the folders it needs.

One line in a unit file stops a compromised service from writing to most of the disk.

[Service]
ProtectSystem=strict
StateDirectory=myapp
ReadWritePaths=/srv/uploads

strict mounts the whole file system hierarchy read-only for the service, except /dev, /proc and /sys. StateDirectory=myapp creates /var/lib/myapp and keeps it writable, so it needs no extra line. ReadWritePaths= opens any other directory the service must write to, here /srv/uploads. If the service logs to a file under /var/log, add that path too or use LogsDirectory=, or it will fail with “Read-only file system”. Check the result with systemd-analyze security. Other related options are ProtectHome= and PrivateTmp=.

Further reading: systemd.exec manual page

More Systemd tips [Service] sudo mkdir -p /etc/systemd/system/service.d [Service] printf 'hunter2' | sudo systemd-creds encrypt --name=dbpass – dbpass.cred systemd-analyze timespan 1h30min All Systemd tips (88) All topics

Link