Targets
A target is a group of units systemd brings the boot up to:
multi-user.target on a server, rescue.target and emergency.target
for repairs. In detail — boot-targets.
The init system and service manager (PID 1): units, targets, dependencies, journals.
systemctl status nginx
state
systemctl start nginx
start it now
systemctl stop nginx
stop it now
systemctl enable nginx
enable autostart
systemctl reload nginx
reread the config without downtime
systemctl daemon-reload
after editing unit files
journalctl -u nginx -f
the service's logs in real time
systemd-analyze blame
boot timings
systemd is the first process of the system (PID 1) and the service manager of most modern distributions. It starts and controls units, resolves dependencies between them, and parallelizes boot.
Types of units:
| Type | Extension | What it describes |
|---|---|---|
| service | .service |
a service/daemon (nginx, sshd) |
| socket | .socket |
a socket-activation socket |
| timer | .timer |
a schedule (a cron replacement) |
| mount | .mount |
a mount point |
| target | .target |
a group of units (like a runlevel) |
Management is through systemctl, journals through journalctl.
The [Unit] lines answer two questions: what to start together and in
which order.
| Line | What it does |
|---|---|
Wants=x |
when the unit starts, start x too; x failing does not bother the unit |
Requires=x |
the same, and if x is stopped, the unit stops too; with After=x the unit does not start when x failed to start |
After=x |
if both start, this one starts after x has finished starting |
Before=x |
the same the other way round |
After= and Before= set only the order: if
nobody starts x, After=x will not start it. That is why a dependency
is written as a pair — Wants=x and After=x. Wants= without
After= starts both units at once, and the service starts before what
it needs is ready. Requires= without After= does not wait either:
the service is already running when the dependency fails. A missing
unit in Wants= is skipped silently; one in Requires= stops the
service from starting. To check a unit after an edit, see
systemd-analyze; the boot waiting chain is in
systemd-analyze.
Units are searched for in (in order of increasing priority):
/lib/systemd/system/ — packaged units (don't edit these);/etc/systemd/system/ — local overrides (edit here)..service structure:
A unit that comes from a package is changed with a drop-in:
a package update overwrites the unit file itself, while the drop-in stays.
A drop-in is a .conf file in /etc/systemd/system/<unit>.d/. systemd
reads the unit, then its drop-ins in alphabetical order, and their lines
add to or replace the unit's lines.
systemctl edit runs daemon-reload itself. After a manual edit without
daemon-reload, systemd keeps working with the old version of the unit. A
key such as ExecStart= in a drop-in adds a second command; to replace the
first one, put an empty ExecStart= line before the new one.
systemd skips a key in the wrong section: LimitNOFILE= under [Unit] has
no effect, and the service gets the default limit. The warning shows in the
journal after daemon-reload and in systemd-analyze verify — whose exit
code is still 0:
A .socket unit holds the port instead of the service: systemd listens
itself and starts the service of the same name on the first connection.
The lab pair is demo-sock.socket and demo-sock.service:
ListenStream=8085 is a port with no address: the socket listens on all
addresses, IPv4 and IPv6. Until the first request the port is held by the
systemd process, and the service is inactive:
The address is changed with a drop-in. ListenStream= is a list: an
empty value resets the unit's addresses; without it the new address is
added to the old one.
restart of the socket restarts the service with Requires= on it too:
the service's MainPID changed (2769 → 2838). Stopping only the service
is not enough — the next connection starts it again, and systemctl stop
warns:
To close the port for good: sudo systemctl disable --now
demo-sock.socket.
A target is a group of units systemd brings the boot up to:
multi-user.target on a server, rescue.target and emergency.target
for repairs. In detail — boot-targets.
systemctl status <unit> — exit code and the last journal lines.journalctl -xeu <unit> — details with explanations.sudo systemctl daemon-reload.Check the name and path: systemctl cat <unit>. If you edited the file —
daemon-reload.
systemd-analyze critical-chain — who held the boot; the order of the
investigation is in systemd-analyze.
Found an inaccuracy? Report an error in the article