Linux › Section F · How user space starts › Topic F.3
systemd operation, dependencies, and targets
Unit files describe what could run; systemctl and the dependency rules decide what does run, in what order, and in which overall state the machine ends up. These are the everyday tools of a Linux administrator.
Seeing what systemd is doing
You talk to systemd through systemctl. Run with no arguments, it performs list-units, its default command, showing the units currently loaded in memory in columns: UNIT, LOAD (whether the file was read), ACTIVE (the general state), SUB (a type-specific state such as running or exited) and DESCRIPTION. Add --all to include inactive units, --full to stop long names being shortened, or --type=service to show only services. systemctl --failed lists units that have failed. To see unit files rather than loaded units, systemctl list-unit-files shows every installed file with its enablement state, such as enabled, disabled or static.
Reading systemctl status
systemctl status sshd.service gives a compact report; on Debian-family systems the unit is called ssh.service. The Loaded: line shows the unit file's path and whether it is enabled; Active: shows the state, such as active (running) or failed, and since when. Then come the main PID and the control group, for example /system.slice/sshd.service, with the processes inside it; "Control groups and performance tuning" explains cgroups. The last few journal lines follow. For the full log of a unit since the current boot, use journalctl -u sshd.service -b, described in "System logging: the journal and syslog". systemd also ships helper daemons that run as ordinary units, such as systemd-journald for logs, systemd-udevd for devices and systemd-logind for user sessions.
Starting, stopping and reloading
systemctl start, stop and restart act on a unit immediately. systemctl reload asks a running service to reread its own configuration, using the unit's ExecReload= command, without stopping it. That is different from systemctl daemon-reload, which makes systemd itself reread all unit files after you change them. reload-or-restart reloads if the service supports it and restarts otherwise, and try-restart restarts a unit only if it is already running. Every such request becomes a job, a queued change of state. systemctl list-jobs shows any still pending, which on a settled system is usually none. These jobs are unrelated to the shell's job control.
Requirement dependencies
Dependencies decide which other units are pulled in. Wants= is the weak form: starting the unit also starts the listed units, but their failure does not affect it. Requires= is stronger: if the required unit is stopped or restarted, this unit follows, and if it fails to start while this unit is ordered After= it, this unit is not started. Requisite= does not start anything; it fails at once unless the listed unit is already active. BindsTo= is like Requires= but also stops this unit whenever the other one stops for any reason. Conflicts= is negative: starting one unit stops the other. The systemd documentation recommends Wants= wherever possible, because a fault in one service then cannot drag down everything that depends on it.
Ordering is a separate question
Requirement directives say nothing about order. If A wants B and nothing else is said, systemd starts both at the same time, which is why boots are fast. To delay A until B has finished starting, add After=B to A, or equivalently Before=A to B. So a service that needs the network usually says both Wants=network-online.target and After=network-online.target. systemd also adds default dependencies: a service normally requires sysinit.target and is ordered after basic.target, and a target is automatically ordered after the units it wants. DefaultDependencies=no turns these off. Conditions such as ConditionPathExists=/etc/app.conf are different again: if the test fails, the unit is skipped without being marked failed.
Enabling: the [Install] section
Starting a unit lasts until it stops or the machine reboots; enabling it arranges for it to start automatically. In the [Install] section, WantedBy=multi-user.target says which unit should want this one. systemctl enable then creates a symbolic link in /etc/systemd/system/multi-user.target.wants/ that points at the unit file, and reloads systemd's configuration. Enabling does not start the unit; systemctl enable --now does both, and disable --now reverses both. A unit with no [Install] section shows as static: it cannot be enabled, only pulled in by other units. systemctl mask links the unit to /dev/null so nothing can start it, even by hand, until systemctl unmask.
Worked example: installing a new service
You have written /etc/systemd/system/inventory.service, with WantedBy=multi-user.target, as in "systemd units and unit files". 1. Run sudo systemctl daemon-reload so systemd reads the new file. 2. Run sudo systemctl enable --now inventory.service. It reports that it created the symlink /etc/systemd/system/multi-user.target.wants/inventory.service pointing to /etc/systemd/system/inventory.service, then starts the service. 3. systemctl is-enabled inventory.service prints enabled, and systemctl is-active inventory.service prints active. 4. If it failed, read journalctl -u inventory.service -b, fix the file, then daemon-reload and restart. 5. Check: after a reboot the service is running, because multi-user.target wants it.
Targets: named system states
A target is a unit that groups others to define a state of the whole system. At boot systemd activates default.target, which is usually an alias for graphical.target on a desktop or multi-user.target on a server. Along the way the boot passes through sysinit.target for low-level setup and basic.target for basic services; multi-user.target adds the remaining non-graphical services, and graphical.target adds a login screen. rescue.target and emergency.target are minimal repair states, covered in "Emergency booting and single-user mode". systemctl get-default shows the default target, and systemctl list-dependencies multi-user.target prints the tree of units it pulls in.
Worked example: a desktop becomes a server
A machine used as a desktop will now run headless. 1. systemctl get-default prints graphical.target. 2. sudo systemctl set-default multi-user.target replaces the link /etc/systemd/system/default.target so it points to multi-user.target; future boots stop there, without a login screen. 3. To change the running system now, sudo systemctl isolate multi-user.target starts that target and its dependencies and stops every unit it does not need, including the display manager. Warning: that ends any graphical session, so save work first. isolate works only on targets that allow it with AllowIsolate=yes. 4. Check: get-default now prints multi-user.target, and set-default graphical.target would undo the change.
On-demand and parallel startup
systemd can create a service's listening socket itself and start the service only when a client connects. For example, a socket unit clock.socket containing [Socket] with ListenStream=4000 and Accept=yes makes systemd listen on TCP port 4000. For each connection it starts an instance of a matching template, clock@.service, whose [Service] section has ExecStart=/usr/bin/date and StandardOutput=socket, so each client receives the date. The same trick speeds up booting, because dependent units can start before the service behind a socket is ready. systemd-analyze blame lists how long each unit took to start and systemd-analyze critical-chain shows the slowest path.
Common mistakes to avoid
First, confusing enable with start: an enabled unit may not be running, and a started unit may not survive a reboot. Second, confusing reload, which reconfigures a running service, with daemon-reload, which rereads unit files. Third, writing Wants= or Requires= and expecting an order; without After=, units start together. Fourth, using Requires= everywhere, so one failure cascades. Fifth, disabling a unit and expecting nothing to start it; another unit can still pull it in, so mask it if it must never run.
Key takeaways
systemctl lists units, shows status, and starts, stops, restarts and reloads them; each request is a job. daemon-reload rereads unit files. Wants= is a weak dependency, Requires= a strong one, Requisite= needs an already active unit, BindsTo= follows the other unit down, and Conflicts= is negative. Ordering comes only from After= and Before=. Enabling follows [Install] WantedBy= and creates a symlink in a .wants directory; enable --now also starts the unit, and mask blocks it completely. default.target usually points to graphical.target or multi-user.target; change it with set-default and switch live with isolate. Socket units allow on-demand startup.
Flashcards
Flashcard
Lists units loaded in memory
Test your understanding
Question 1 of 5
Unit A contains Wants=b.service and no ordering directives. When A is started, what happens?
Question 2 of 5
After systemctl enable backup.service, the service is not running. Why?
Question 3 of 5
You edited /etc/systemd/system/web.service by hand. Which command makes systemd use the new file?
Question 4 of 5
A server should boot to a text console from now on. Which command sets that?
Question 5 of 5
A unit file has no [Install] section. What does systemctl is-enabled most likely report for it?
