Linux
    Overview
    Process
    Sockets
    File
    File System Hierarchy
    System Calls
    Libraries
    Device
    systemd
    Service
    namespaces
    Memory
    Modules
    Signals
    System Monitoring
    Desktop Environment
    Users and Groups
    Logs
    Network
    Terminal
    GNU

Linux - systemd

Updated: 2022-10-09

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

When run as 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.

init system:

  • to initialize 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.

Units

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

types:

  • Service (.service files)
  • Targets are special unit files that describe a system state or synchronization point. Like other units, the files that define targets can be identified by their suffix, which in this case is .target. 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
# This will show you a list of all of the units that systemd currently has active on the system.
$ systemctl list-units
# or
$ systemctl

# show all
$ 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

systemctl

Use Systemctl to Manage Systemd Services and Units.

journalctl

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;