Skip to content

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.

SuffixWhat it describes
.serviceA long-running process
.socketA socket-activated service
.timerA cron replacement
.targetA grouping (like a runlevel)
.mountA filesystem mount, alternative to /etc/fstab
/etc/systemd/system/reetwiz-api.service
[Unit]
Description=Reetwiz API
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
User=reetwiz
Group=reetwiz
WorkingDirectory=/opt/reetwiz-api
ExecStart=/opt/reetwiz-api/bin/api
Restart=on-failure
RestartSec=5s
# Sandboxing
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
PrivateTmp=true
ReadWritePaths=/var/lib/reetwiz-api
[Install]
WantedBy=multi-user.target
Terminal window
systemd-analyze security reetwiz-api.service
# Overall exposure level: 2.1 OK

Aim for < 3.0.

/etc/systemd/system/backup.service
[Unit]
Description=Nightly backup
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
/etc/systemd/system/echo.socket
[Unit]
Description=Echo socket
[Socket]
ListenStream=127.0.0.1:2222
Accept=yes
[Install]
WantedBy=sockets.target

Now the service is started only when someone connects. Great for infrequently-used tools.

  1. Reload after editing units
    Terminal window
    sudo systemctl daemon-reload
  2. Enable + start
    Terminal window
    sudo systemctl enable --now reetwiz-api.service
  3. Status + recent logs
    Terminal window
    systemctl status reetwiz-api.service
  4. Follow logs
    Terminal window
    journalctl -u reetwiz-api.service -f --since=-15m
  5. What’s blocking boot?
    Terminal window
    systemd-analyze blame | head -20
    systemd-analyze critical-chain
  • 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