systemd Basics
Every Linux server you’ll touch runs systemd. Learn five unit types, five commands, and one config file — that’s 90% of the day-to-day.
Unit types you’ll see
Section titled “Unit types you’ll see”| Suffix | What it describes |
|---|---|
.service | A long-running process |
.socket | A socket-activated service |
.timer | A cron replacement |
.target | A grouping (like a runlevel) |
.mount | A filesystem mount, alternative to /etc/fstab |
The service unit you’ll write
Section titled “The service unit you’ll write”[Unit]Description=Reetwiz APIAfter=network-online.targetWants=network-online.target
[Service]Type=notifyUser=reetwizGroup=reetwizWorkingDirectory=/opt/reetwiz-apiExecStart=/opt/reetwiz-api/bin/apiRestart=on-failureRestartSec=5s
# SandboxingNoNewPrivileges=trueProtectSystem=strictProtectHome=truePrivateTmp=trueReadWritePaths=/var/lib/reetwiz-api
[Install]WantedBy=multi-user.targetVerify hardening
Section titled “Verify hardening”systemd-analyze security reetwiz-api.service# Overall exposure level: 2.1 OKAim for < 3.0.
Timers — the cron replacement
Section titled “Timers — the cron replacement”[Unit]Description=Nightly backup
[Service]Type=oneshotExecStart=/usr/local/bin/backup.sh[Unit]Description=Run nightly backup
[Timer]OnCalendar=*-*-* 02:30:00Persistent=trueRandomizedDelaySec=15min
[Install]WantedBy=timers.targetSockets — auto-start on connect
Section titled “Sockets — auto-start on connect”[Unit]Description=Echo socket
[Socket]ListenStream=127.0.0.1:2222Accept=yes
[Install]WantedBy=sockets.targetNow the service is started only when someone connects. Great for infrequently-used tools.
The five commands
Section titled “The five commands”- Reload after editing units
Terminal window sudo systemctl daemon-reload - Enable + start
Terminal window sudo systemctl enable --now reetwiz-api.service - Status + recent logs
Terminal window systemctl status reetwiz-api.service - Follow logs
Terminal window journalctl -u reetwiz-api.service -f --since=-15m - What’s blocking boot?
Terminal window systemd-analyze blame | head -20systemd-analyze critical-chain
Cheat sheet: file locations
Section titled “Cheat sheet: file locations”Directory/etc/systemd/system/ # your custom units (highest precedence)
- …
Directory/run/systemd/system/ # runtime-generated units
- …
Directory/lib/systemd/system/ # distribution defaults
- …
- /etc/systemd/journald.conf # journal size, forwarding
- /etc/systemd/logind.conf # session limits, tty handling