A systemd Cheat Sheet
Generally, systemd
is the standard init system across Linux
distributions. systemctl
is its central CLI management tool.
This is a brief overview and cheat sheet.
Background
systemd
is responsible for initializing and managing components, services, and daemons that must be started after the kernel is booted. Such components are often referred to as “userland” components.- resources managed by
systemd
are called units; these are defined in unit files - service management unit files are suffixed with a
.service
(though there are other types of units too, such as.socket
,.device
,.mount
, etc. This overview focuses on.service
units, though)
Starting/Stopping services
systemctl start something.service
- start a servicesystemctl stop something.service
- stop a servicesystemctl restart something.service
- restart a service
Reloading configuration
systemctl reload something.service
- reload a service’s configuration without restarting the service, assuming the service is capable of thissystemctl reload-or-restart something.service
- reload a service’s configuration in place, if the service is capable of doing so (otherwise, restart the service to pick up the new configuration)systemctl daemon-reload
reloads the entiresystemd
process
Enabling services
systemctl enable something.service
- enable a service, causing the service to be automatically started at boot (Note, however, this does not start the service in the current session; that still requires astart
)systemctl disable something.service
- disable a service from starting at boot
Querying status
systemctl status something.service
- outputs the service’s state, the cgroup hierarchy, and the first serveral log linessystemctl is-active something.service
- reports if a service is runningsystemctl is-enabled something.service
- reports if a service is enabledsystemctl is-failed something.service
- reports if a service is in a failed state
Learning more about a system’s units
systemctl list-units
- lists active unitssystemctl list-units --all --state=active
- lists all active units
(Other options exist too.)
Learning more about a unit’s details
systemctl cat something.service
- output the service’s unit filessystemctl list-dependencies something.service
- outputs a hierarchy of the service’s dependencies (i.e. other services required by it)systemctl show something.service
- outputs the properties associated with a service
Unit files
- unit files usually live in
/lib/systemd/system
.conf
files in.d
directories like/etc/systemd/system/something.service.d
containing “snippets” are merged on load with a unit definition to override or extend the unit definition with the “snippet”.service
unit files in/etc/systemd/system
completely override the unit definition usually found in/lib/systemd/system
- Understanding systemd Units and Unit Files offers a detailed overview of unit file “anatomy”
A very basic example:
[Unit]
Description=something
[Service]
ExecStart=/bin/bash -c "something"
[Install]
WantedBy=multi-user.target
journalctl
journalctl
is the CLI tool for journal
, which is responsible for collecting and managing systemd
logs.
A few examples of working with journalctl
:
journalctl
- outputs thesystemd
logsjournalctl -u something -f
- outputs the logs for a servicejournalctl --since yesterday
orjournalctl --since 09:00 --until "1 hour ago"
can be used to filter logs to a specific window of timejournalctl -p err -b
- shows only log entries logged at the error level or abovejournalctl -o json-pretty
- shows logs in formatted JSON (other output options exist, too)