These are the notes of a training course on systemd I gave as part of my work
with
Truelite.
Exploring the state of a system
systemctl status [unitname [unitname..]] show status of one or more units,
or of the whole system. Glob patterns also work: systemctl status "systemd-fsck@*"
systemctl list-units or just systemctl show a table with all units, their
status and their description
systemctl list-sockets lists listening sockets managed by systemd and what
they activate
systemctl list-timers lists timer-activated units, with information about
when they last ran and when they will run again
systemctl is-active [pattern] checks if one or more units are in active state
systemctl is-enabled [pattern] checks if one or more units are enabled
systemctl is-failed [pattern] checks if one or more units are in failed state
systemctl list-dependencies [unitname] lists the dependencies of a unit, or
a system-wide dependency tree
systemctl is-system-running check if the system is running correctly, or if
some unit is in a failed state
systemd-cgtop like top
but processes are aggregated by unit
systemd-analyze produces reports on boot time, per-unit boot time charts,
dependency graphs, and more
Start and stop services
Similar to the System V
service command,
systemctl provides commands to start/stop/restart/reload units or services:
start: starts a unit if it is not already started
stop: stops a unit
restart: starts or restarts a unit
reload: tell a unit to reload its configuration (if it supports it)
try-restart: restarts a unit only if it is already active, otherwise do nothing, to prevent accidentally starting a service
reload-or-restart: tell a unit to reload its configuration if supported, otherwise restart it
try-reload-or-restart: tell a unit to reload its configuration if supported, otherwise restart it. If the unit is not already active, do nothing to prevent accidentally starting a service.
Changing global system state
systemctl has
halt,
poweroff,
reboot,
suspend,
hibernate, and
hybrid-sleep commands to tell systemd to reboot, power off, suspend and so
on.
kexec and
switch-root also work.
The
rescue and
emergency commands switch the system to rescue and emergency
mode (see
man systemd.special.
systemctl default switches to the default mode, which also happens when
exiting the rescue or emergency shell.
Run services at boot
systemd does not implement runlevels, and services start at boot based on their
dependencies.
To start a service at boot, you add to its
.service file a
WantedBy=
dependency on a well-known
.target unit.
At boot, systemd brings up the whole chain of dependency started from a default
unit, and that will eventually activate also your service.
See
systemctl get-default for what unit is currently the default in your
system. You can change it via the
systemd.unit= kernel command line,
so you can configure multiple entries in the boot loader that boot the system
running different services. For example
systemd.unit=rescue.target for a
rescue mode,
systemd.unit=multi-user.target for a non-graphical mode, or add
your own
.target file to implement new system modes.
See
systemctl list-units -t target --all for a list of all currently
available targets in your system.
systemctl enable unitname enables the unit to start at boot, by creating
symlinks to it in the .wants directory of the units listed in its
WantedBy= configuration
systemctl disable unitname removes the symlinks created by enable
systemctl reenable unitname removes and readds the symlinks for when you
changed WantedBy=
Notes:
systemctl start activates a unit right now, but does not automatically
enable it at boot
systemctl enable enables a unit at boot, but does not automatically start
it right now
* a disabled unit can still be activated if another unit depends on it
To disable a unit so that it will never get started even if another unit
depends on it, use
systemctl mask unitname. Use
systemctl unmask unitname
to undo the masking.
Reloading / restarting systemd
systemctl daemon-reload tells systemd to reload its configuration.
systemctl daemon-reexec tells systemd to restart iself.