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: DynamicUser=yes gives a systemd service a throwaway user account

Systemd
~/tips/tip-systemd-dynamicuser-throwaway-service-account

DynamicUser=yes

Runs a service as an auto-created temporary user and gives it one writable data folder.

For small daemons I skip useradd and let systemd allocate the user at start.

[Service]
DynamicUser=yes
StateDirectory=myapp
ExecStart=/usr/local/bin/myapp

systemd allocates a free UID and group when the service starts, and the name is derived from the unit name. The option implies ProtectSystem=strict, ProtectHome=read-only, NoNewPrivileges= and RestrictSUIDSGID=. Persistent data goes in StateDirectory=, which is created below /var/lib/private and is not removed when the service stops. Files left outside the managed directories can end up owned by a UID that no longer exists, so keep all data inside it.

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