tips
88 tips Systemd
One command, setting or habit at a time about Systemd: what it does, the line to copy and a caveat or two.
Unit files 25
-
Sets how long a service gets to stop after SIGTERM before systemd sends SIGKILL.
[Service] -
A type-level drop-in folder applies one setting to all units of a type.
sudo mkdir -p /etc/systemd/system/service.d -
Terminates a service and marks it failed when it exceeds a maximum run time.
[Service] -
Let systemd create and own the data folder of a service, with the right user.
[Service] -
Let an optional preparation step fail without stopping the service from starting.
[Service] -
Give each override of a unit its own named file instead of one big override.conf.
sudo systemctl edit --drop-in=limits myapp.service -
Replace names like sh, python3 or java in log lines with the name of your service.
[Service] -
Skip a service quietly when a check command says now is not the time.
[Service] -
Back off gradually when a service keeps crashing instead of hammering it every few seconds.
[Service] -
Make systemctl reload send a signal to the main process of your own service.
ExecReload=/bin/kill -HUP $MAINPID -
Write one unit file with %i and start as many named instances of it as you need.
[Unit] -
Stop a service from showing as failed when its program exits with a harmless code.
[Service] -
Keeps a service from starting before the disk or share it writes to is mounted.
sudo systemctl edit myapp.service -
Blocks a systemd service from being started by anything until you unmask it.
sudo systemctl mask --now cups.service -
Checks on the spot whether a unit's Condition lines would let it start on this machine.
systemd-analyze condition 'ConditionHost=nas*' 'ConditionVirtualization=!vm' -
Skips starting a service, without marking it failed, when a chosen file is absent.
sudo systemctl edit mysync.service -
Loads a service's environment variables from a separate file you can edit freely.
[Service] -
Keeps a run-once setup service active after its script exits, so stop runs cleanly.
RemainAfterExit=yes -
Lets a helper service follow its main service when that one stops or restarts.
sudo systemctl edit myapp-worker.service -
Sends a service's output to a normal text log file instead of the journal.
sudo systemctl edit myapp.service -
Triggers a notification unit automatically whenever another service ends up failed.
OnFailure=notify@%n.service -
Restarts a crashed service after a pause and gives up after too many quick failures.
Restart=on-failure -
Checks a unit file for unknown directives and missing binaries without starting anything.
systemd-analyze verify /etc/systemd/system/myapp.service -
Replaces the start command of a service in a drop-in by clearing the inherited one first.
ExecStart= -
Prints the unit file plus every drop-in override in application order.
systemctl cat nginx.service
Timers and sockets 14
-
See every socket systemd listens on and the service each one would start.
systemctl list-sockets -
Trigger any service from a timer, so two timers can share one job.
[Timer] -
Make a timer fire close to the exact time instead of anywhere in the next minute.
[Timer] -
Serve a tiny network service from a script, with systemd handling the socket.
Accept=yes -
Schedule a timer in local office time while the server itself runs on UTC.
[Timer] -
Creates a one-off transient timer that starts a command after the delay you give.
sudo systemd-run --on-active=30 --timer-property=AccuracySec=100ms /bin/touch /tmp/foo -
Runs a service every few minutes, counted from its last run instead of the clock.
[Unit] -
Runs a service automatically whenever a watched file or folder changes.
[Path] -
Schedules a single command for later without at or a unit file.
sudo systemd-run --on-active=30min /usr/local/bin/backup.sh -
Gives one timer several schedules, such as different times on weekdays and weekends.
[Unit] -
Starts a test listener that launches a program per connection, mimicking socket activation.
systemd-socket-activate -l 2000 --inetd -a cat -
Adds a random delay to each timer run, so many hosts do not hit a server at once.
[Timer] -
Runs a missed timer job once after the machine comes back instead of skipping it.
Persistent=true -
Shows the normalised form and the next run times of a timer schedule expression.
systemd-analyze calendar "Mon..Fri *-*-* 03:30" --iterations=3
systemd 14
-
Delete a service's cache, state or log folders with one command, without guessing paths.
sudo systemctl clean --what=cache myapp.service -
Clean up the failed state of units so the list only shows current problems.
sudo systemctl reset-failed myapp.service -
See the full process tree of one unit, including children the main process started.
systemd-cgls -u nginx.service -
Check which PATH and other variables services get, since they never read your shell profile.
systemctl show-environment -
Find out which service or session started a process you spotted in top.
systemctl status 4211 -
Opens an interactive shell under the same conditions a systemd service runs in.
sudo systemd-run --shell -p User=www-data -p ProtectHome=yes -
Runs a command as a service and prints its runtime and exit status afterwards.
sudo systemd-run --wait -P -G -p CPUAccounting=yes -p MemoryAccounting=yes /usr/local/bin/backup.sh -
Lets systemd user services start at boot and keep running when you are not logged in.
sudo loginctl enable-linger alice -
Lists every service by how long it took to start during the last boot, slowest first.
systemd-analyze blame -
Shows every scheduled systemd timer with its last and next run time.
systemctl list-timers --all -
Lists services by how long they took to start, so you can see what slows the boot.
systemd-analyze blame -
Runs a command in a temporary cgroup with memory and CPU limits, no unit file needed.
systemd-run --user --scope -p MemoryMax=2G -p CPUQuota=200% make -j8 -
Applies memory and CPU limits to a running service, temporarily or permanently, without editing files.
systemctl show nginx.service -p MemoryMax -p CPUQuotaPerSecUSec -
Displays the chain of units that actually held up the boot, with start times.
systemd-analyze critical-chain
Hardening 10
-
Encrypts a secret into a file that only systemd can decrypt for a named credential.
printf 'hunter2' | sudo systemd-creds encrypt --name=dbpass - dbpass.cred -
Make sure a service and its children can never gain more rights through sudo or setuid binaries.
NoNewPrivileges=yes -
Keep a service's temporary files away from other users and programs.
PrivateTmp=yes -
Give a service a minimal /dev without disks, so a bug can not read raw devices.
PrivateDevices=yes -
Forbid a service from calling things like mount or reboot at the kernel level.
[Service] -
Gives a service a password as a private read-only file instead of an environment variable.
sudo systemctl edit myapp.service -
Lets a service running as a normal user listen on ports below 1024 without root.
[Service] -
Runs a service as an auto-created temporary user and gives it one writable data folder.
DynamicUser=yes -
Mounts the file system read-only for one service and re-opens only the folders it needs.
ProtectSystem=strict -
Rates the sandboxing of a service from 0 to 10 and lists the options that are still open.
systemd-analyze security nginx.service
Analysis 7
-
Parses a time span like the ones in unit files and prints it normalized.
systemd-analyze timespan 1h30min -
Read the full effective config of journald, logind or resolved in one place.
systemd-analyze cat-config systemd/journald.conf -
List all overridden, extended and masked systemd files on a machine in one go.
systemd-delta --no-pager -
See in one picture which units start in parallel and which ones hold up the boot.
systemd-analyze plot > boot.svg -
Looks up what a numeric service exit status means and where it comes from.
systemd-analyze exit-status 203 -
Prints a single unit property without labels, so scripts can read it directly.
systemctl show nginx.service -p MainPID --value -
Shows which units depend on a given unit, before you stop or disable it.
systemctl list-dependencies --reverse docker.service
Kernel and system 5
-
Give a busy service more file handles than the default, set in the unit itself.
sudo systemctl edit nginx.service -
Restart every service and the service manager quickly, while the kernel keeps running.
sudo systemctl soft-reboot -
Let a script check if it is on bare metal, in a VM or inside a container.
systemd-detect-virt -
Shows CPU, memory and I/O use grouped per systemd unit, sorted live.
systemd-cgtop -c -n 1 -
Runs a command while preventing the machine from sleeping or shutting down.
systemd-inhibit --why="Copying photos" rsync -a /mnt/user/photos/ /mnt/backup/photos/
Network and DNS 3
-
Lists the records systemd-resolved has cached, grouped by network scope.
resolvectl show-cache -
Raises systemd-resolved's log level on the fly so lookups show up in the journal.
sudo resolvectl log-level debug -
Shows what systemd-resolved knows about each DNS server and makes it probe again.
resolvectl show-server-state
Boots and kernel 2
-
Lists systemd-boot entries with bootctl list and picks one for the next boot only.
sudo bootctl set-oneshot ID -
Adds one option to the kernel command line for a single boot, to land in rescue or emergency mode.
systemd.unit=rescue.target
DNS and Pi-hole 2
-
Turn off the local stub resolver so Pi-hole or another DNS server can bind to port 53.
DNSStubListener=no -
Makes systemd-resolved send all DNS queries over an encrypted TLS connection.
[Resolve]
Filtering 2
-
Get a clean list of every enabled unit, or every masked or disabled one.
systemctl list-unit-files --state=enabled -
Read the system journal as your normal user, without typing sudo every time.
sudo usermod -aG systemd-journal $USER
Logs and diagnostics 2
-
Turn on debug logging for the service manager at runtime, without a reboot.
sudo systemd-analyze log-level debug -
Logs the output of a script or command to the journal with a tag and priority you choose.
systemd-cat -t backup /usr/local/bin/backup.sh
Mounts and writes 2
-
Name mount unit files correctly so systemd accepts them, using systemd-escape.
systemd-escape -p --suffix=mount /mnt/backup -
Mounts a network share only when a program opens the folder, so boot never waits for it.
192.168.1.x:/export/media /mnt/media nfs x-systemd.automount,x-systemd.idle-timeout=10min 0 0
Nothing in this filter yet.