logo

systemd Cheatsheet

systemctl Commands

To show a list of the units:

# show active units
$ systemctl list-units
# or
$ systemctl

# show all units
$ systemctl list-units --all

To see a unit’s dependency tree

$ systemctl list-dependencies sshd.service

List services running in systemd:

$ ls /etc/systemd/system

To create another service, e.g. add foo.service file in /etc/systemd/system.

$ sudo systemctl daemon-reload

# manage services for the current session
$ sudo systemctl start foo.service
$ sudo systemctl stop foo.service
$ sudo systemctl status foo.service
$ sudo systemctl restart foo.service
# reload configuration files without restarting
$ sudo systemctl reload foo.service
$ sudo systemctl reload-or-restart foo.service

# start services automatically at boot or not
# enabling a service does not start it in the current session
$ sudo systemctl enable foo.service
$ sudo systemctl disable foo.service

Troubleshooting

Error: Unit xxx not found

$ systemctl restart kubectl
Failed to restart kubectl.service: Unit kubectl.service not found.

Solution: run daemon-reload first

$ systemctl daemon-reload
$ systemctl restart kubelet

daemon-reload regenerates dependency trees.

If you modify /lib/systemd/system/<servicename>.service and run systemctl restart, it may just restart the old cached settings with no changes. Run systemctl daemon-reload to reload.

journalctl Commands

The systemd journal is not a large text file. It’s a binary file maintained by the daemon. So, it can’t be opened with a text editor.

The journal is controlled by the systemd-journald daemon.

Locations:

  • in-memory journaling: systemd creates its journal files under the /run/log/journal directory.
  • persistent storage, the journal is created under /var/log/journal directory;

To check logs of foo.service, use -u flag:

$ journalctl -u foo.service
$ journalctl -u foo.service --since today

More info

At the last step of the boot process, Linux runs an init system. The init system serves as the first process in the user space and is responsible for starting up other important services and processes.

systemd is an init system and system manager. Adopted by many Linux distributions.

When run as the first process on boot (as PID 1), it acts as init system that brings up and maintains userspace services.

systemd is usually not invoked directly by the user, but is installed as the /sbin/init symlink and started during early boot.

Use cases:

  • initializes the components that must be started after the Linux kernel is booted.
  • also used to manage services and daemons for the server at any point while the system is running.

systemd deviates from the Unix philosophy as it aggregate many functions; it is designed similar to mac's launchd.

systemd vs SysVInit

  • SysVInit
    • the classic initialization process in Linux.
    • individual service install relevant scripts to the /etc/init.d directory.
    • use service command to run the init scripts.
    • a start-once process: it does not track the individual services afterward.
  • systemd
    • the new init system to replace SysVInit.
    • In contrast to SysVInit, SystemD continues to run as a daemon process after the initialization is completed.
    • CLI: systemctl. Contrary to service, it manages the services by interacting with the systemd process instead of running the init script.

Units

systemd unit files (service, etc): /lib/systemd/system/

Types:

  • Service (.service files)
  • Targets (.target files) are special unit files that describe a system state or synchronization point. Targets do not do much themselves, but are instead used to group other units together. They are used as a reference for when certain functions are available. For instance, there is a swap.target that is used to indicate that swap is ready for use. Target units in systemd loosely resemble run levels in System V in the sense that each target unit represents a particular system state.
  • Socket units, which encapsulate local IPC or network sockets in the system, useful for socket-based activation.
  • Device units expose kernel devices in systemd and may be used to implement device-based activation.
  • Mount units control mount points in the file system.

service

service is used to run an init script in /etc/init.d.

$ sudo service <your_service> {start, stop, restart, status}

For example,

$ sudo service docker start

will run the script /etc/init.d/docker and start dockerd. To check the status:

$ service docker status

To check status of all the services:

$ service --status-all
...
[ + ] cgroupfs-mount
[ - ] docker
[ ? ] networking
...

where [ + ] means the service is running, [ - ] is stopped, [ ? ] means status is not implemented.

service is an "high-level" command used for starting and stopping services in different unixes and linuxes. Depending on the "lower-level" service manager, service redirects on different binaries.

systemctl is similar to service but more powerful. In some systems service will redirect to use systemctl.

DNS

systemd-resolved: provides network name resolution to local applications via a D-Bus interface.

Config: /etc/systemd/resolved.conf