systemd and units
systemd starts and supervises the services that make a Linux system useful. Its building blocks are units, described in simple configuration files.
What systemd does
systemd, created by Lennart Poettering and Kay Sievers and first released in 2010, is PID 1 on most distributions. It starts services in parallel based on dependencies, supervises and restarts them, manages mounts, sockets, timers, and devices, collects logs, and handles shutdown. It replaced older init systems that ran shell scripts in a fixed order.
Units
Everything systemd manages is a unit, described by a unit file. Types include service units (.service) for daemons, socket units (.socket) for listening sockets, timer units (.timer) for scheduled tasks, mount units (.mount), target units (.target) grouping other units, path units, and slice units for resource control. Unit files live in /usr/lib/systemd/system/ (from packages) and /etc/systemd/system/ (local overrides, which take precedence).
systemctl
systemctl controls units: systemctl start nginx starts a service now; stop stops it; restart restarts; reload asks it to reload configuration; status shows whether it is running, its main PID, recent log lines, and its cgroup. enable makes it start automatically at boot by linking it into a target; disable removes that; enable --now does both enable and start. mask prevents a unit from starting at all.
A service unit file
A simple unit: [Unit] Description=Report generator, After=network-online.target; [Service] ExecStart=/usr/local/bin/report-server --port 8080, User=reports, Restart=on-failure; [Install] WantedBy=multi-user.target. After daemon-reload, systemctl enable --now report.service starts it and arranges for it to start at boot. Type= (simple, exec, forking, notify, oneshot) tells systemd how the service signals readiness.
Dependencies and ordering
Requires= and Wants= express dependencies: if A requires B, starting A starts B, and B failing stops A; Wants= is a weaker version. After= and Before= express ordering only, independently of dependencies, so services usually need both a Wants= or Requires= and an After=. systemctl list-dependencies shows the dependency tree.
Targets
Targets group units into system states, replacing SysV runlevels: multi-user.target for a non-graphical server, graphical.target for a desktop, rescue.target for single-user repair, and emergency.target for a minimal shell. systemctl get-default and set-default view and change the default target; systemctl isolate switches the running system to another target.
Drop-ins and overrides
To change a packaged unit, do not edit files in /usr/lib, which updates overwrite. Instead, systemctl edit nginx creates a drop-in file under /etc/systemd/system/nginx.service.d/ containing only changed settings, such as Restart=always or extra Environment= lines. systemctl cat nginx shows the combined configuration, and systemctl daemon-reload makes systemd reread unit files.
Worked example
A custom API keeps crashing overnight and staying down. systemctl status api.service shows Active: failed (Result: exit-code) and the last log lines. systemctl edit api.service adds [Service] Restart=on-failure and RestartSec=5s. After systemctl daemon-reload and systemctl restart api, a crash is followed by an automatic restart five seconds later, visible in the journal, while the team investigates the root cause.
Key takeaways
systemd (2010) is PID 1 on most distributions, starting, supervising, and stopping services in parallel by dependency. It manages units: services, sockets, timers, mounts, targets, paths, and slices, with files in /usr/lib/systemd/system and overrides in /etc/systemd/system. systemctl start, stop, restart, reload, status, enable, disable, and mask control units. Service files define ExecStart, User, Restart, Type, and WantedBy. Requires and Wants express dependencies; After and Before express order. Targets such as multi-user and graphical replace runlevels. Use systemctl edit drop-ins rather than editing packaged files, then daemon-reload.
Flashcard
Init system and service manager
Check your understanding
What is the difference between systemctl start and systemctl enable?
