systemd Units

Concept

A unit is the basic object systemd manages. Common types include:

TypeExtensionPurpose
Service.serviceLong-running processes
Socket.socketSocket activation
Timer.timerTime-based activation (cron replacement)
Target.targetGrouping / synchronization points
Mount.mountFilesystem mounts
Path.pathPath-based activation

Why it matters

Understanding unit types, dependencies, and ordering is key to diagnosing why a service fails to start or starts in the wrong order.

Mental Model

Unit file (and drop-ins)
    ↓
systemd reads & activates
    ↓
Dependencies (Requires, Wants, After, Before)
    ↓
Service runs (or fails)

Drop-in files (/etc/systemd/system/<unit>.d/*.conf) let you override parts of a unit without editing the original file.

Key Commands

# List unit files and their state
systemctl list-unit-files --type=service
 
# Show effective unit content (including drop-ins)
systemctl cat <unit>
 
# Show all properties
systemctl show <unit>
 
# Dependencies
systemctl list-dependencies <unit>
systemctl list-dependencies --reverse <unit>
 
# Edit a drop-in safely
systemctl edit <unit>                # creates override.conf
systemctl edit --full <unit>         # full copy
 
# After changing unit files
systemctl daemon-reload

Important Directives (service units)

[Unit]
Description=
After= / Before=
Requires= / Wants=

[Service]
Type=simple|forking|oneshot|notify
ExecStart=
ExecReload=
Restart=on-failure|always
Environment=
EnvironmentFile=
LimitNOFILE=

[Install]
WantedBy=multi-user.target

Common Failure Modes & Symptoms

SymptomWhat to look at
Unit fails to startsystemctl status, journalctl -u
Changes have no effectDid you run daemon-reload? Drop-ins?
Starts too early / too lateAfter= / Before= dependencies
Fails because of missing dependencyRequires= vs Wants=
Environment variables missingEnvironment= / EnvironmentFile=

Investigation Tips

  • Always check systemctl cat <unit> to see the real effective configuration.
  • Prefer drop-ins (systemctl edit) over modifying vendor unit files.
  • Type=notify and Type=forking behave very differently from Type=simple — wrong type is a common cause of startup issues.
  • Use systemd-analyze blame and systemd-analyze critical-chain when investigating slow boots.

Personal Lessons Learned