Systemd Cheatsheet

written by

Feedback: Github Issue Tracker

System state

Power off the system
systemctl poweroff
Reboot the system
systemctl reboot
Suspend the system
systemctl suspend
Hibernate the system
systemctl hibernate
Hibernate and suspend the system
systemctl hybrid-sleep
Action on power button
Can be configured in /etc/systemd/logind.conf by HandlePowerKey.
Action on LID switch
Can be configured in /etc/systemd/logind.conf by HandleLidSwitch.

Daemon control

Reload configuration after changes
systemctl [--user] reload-daemon

--user Reload only user configuration.

User management

Automatic start-up of systemd user instances
loginctl enable-linger user-name

Manage services

Start/stop service
systemctl start|stop <servicename>
Restart service
systemctl restart <servicename>
Restart if service is already running.
systemctl condrestart <servicename>
Reload configuration files
systemctl reload <servicename>
Start service at boot
systemctl enable <servicename>
Prevent service start at boot
systemctl disable <servicename>
Check if service is configured to autostart.
systemctl is-enabled <servicename>

Analyze and debug

Show system status
systemctl status
List running units
systemctl list-units
List failed units.
systemctl --failed
Find long running services
systemd-analyze blame
Plot dependency graph as SVG
systemd-analyze plot > graph.svg

Modify services/units

You can override or extend an existing system unit, for example to add additional dependencies or change the executable arguments.

Modify unit
systemctl edit sshd

Opens /etc/systemd/system/sshd.d/override.conf and reloads the unit when done editing.

Replace unit
systemctl edit --full sshd

Opens /etc/systemd/system/sshd (copying installed version) and reloads the unit when done editing.

Revert to vendor version
systemctl revert sshd
Show effective unit
systemctl cat sshd

Show content of unit after apply all overrides.

Find differences
systemd-diff

Show diff for modified unit files.

Runlevels

Determine which target is used by default.
systemctl get-default
Set the default target (runlevel).
systemctl set-default multi-user.target
Switch to another runlevel (target).
systemctl isolate graphical.target

Timers

Timers are a replacement for cronjob.

List timers
systemctl list-timers
Create timer
Create new unit: https://wiki.archlinux.org/index.php/Systemd/Timers

Logging (Basics)

Show complete log
journalctl

-r Show log in reverse order.

-x Show explainations for log entries.

-e Force pager to show the last page.

Show log for specific unit
journalctl -u postfix
Write to log from shell
echo "Hello World" | systemd-cat -t my-tag -p info

logger is still possible with journald, but system-cat has more features.

Start program and redirect stdout to log
systemd-cat /bin/ls

Logging (Advanced)

Show log by unit pattern
journalctl -u postfix* -u dovecot*
Watch for new log entries (follow)
journalctl -u nginx* -f
Filter by priority
journalctl -u ntp -p emerg..warning

Log levels:

emerg 0
alert 1
crit 2
err 3
warning 4
notice 5
info 6
debug 7
Show all critical logs
journalctl -p crit
Filter by date
journalctl -u postfix -S "-5 minutes"
or
journalctl -u postfix -S "today"
Show only kernel messages
journalctl -k

Logging (Coexistence)

  • Systemd's journald replaces the old syslog daemons.
  • Basically this is done by providing /dev/log which is connected to journald.
  • The syslog() API writes to /dev/log and thus messages are forwared to journald.
Persistent logs

Important: journald default configuration is to keep all log messages in memory (lost after a reboot).

Change /etc/systemd/journald.conf to make journal persistent.

rsyslog

If rsyslog is installed, default behavior for systemd is to forward log messages to rsyslog which can filter and store messages in well known locations like /var/log/auth.log.

Configuation files

System units
/usr/lib/systemd/system/
⁘ Units provided by installed packages
/etc/systemd/system/
⁘ Units installed by the sysadmin
User units
/etc/systemd/user/
⁘ System-wide user units installed by the sysadmin
~/.local/share/systemd/user/
⁘ Units provided by user-installed packages
~/.config/systemd/user/
⁘ Custom user units installed by users