Systemd

Introduction to systemd

What is systemd?

systemd is a daemon (or a service) that manages other daemons for Linux OS. When Linux OS starts or reboots, systemd also starts and continues running to centrally control other daemons until the OS shuts down. You can see how systemd is positioned in Linux OS by running the pstree command. The pstree command shows current processes running on the OS in a hierarchical structure.

Command Line - INPUT
pstree
Command Line - RESPONSE
systemd─┬─accounts-daemon───2*[{accounts-daemon}]
        ├─acpid
        ├─2*[agetty]
        ├─amazon-ssm-agen─┬─ssm-agent-worke───8*[{ssm-agent-worke}]
        │                 └─7*[{amazon-ssm-agen}]
        ├─atd
        ├─cron
        ├─dbus-daemon
        ├─5*[grep]
        ├─multipathd───6*[{multipathd}]
        ├─networkd-dispat
        ├─polkitd───2*[{polkitd}]
        ├─rsyslogd───3*[{rsyslogd}]
        ├─snapd───8*[{snapd}]
        ├─sshd─┬─3*[sshd───sshd───bash]
        │      ├─4*[sshd───sshd───sftp-server]
        │      └─sshd───sshd───bash───pstree
        ├─systemd───(sd-pam)
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-network
        ├─systemd-resolve
        ├─systemd-timesyn───{systemd-timesyn}
        ├─systemd-udevd
        ├─unattended-upgr───{unattended-upgr}
        └─uuidd hierarchically.

Units

systemd manages and controls other processes through units. There are several types of units. Below are some examples.

  • .service: starts, stops, restarts, or reloads a service daemon
  • .socket: activates a service when the service receives incoming traffic on a listening socket
  • .path: activates and deactivates a service if a specified file or directory is accessed
  • .target: is a group of units
  • .timer: activates and deactivates a service with a timer

Unit files

When systemd starts, it triggers multiple units by loading unit files. Unit files are configuration files that contain directives for units. For example, when and how the unit will start, dependency on other units, which command will be triggered, etc.

Locations of unit files

There are two major file locations for unit files. The unit files saved under these directories will be loaded by systemd, and systemd will follow the directives written in the unit files.

  • /usr/lib/systemd/system: default location for unit files. Original copies of unit files should be stored in this directory.
  • /etc/systemd/system: custom unit file location. If you have modified unit files, store them in this directory.

When systemd loads unit files, the unit files in the /etc/systemd/system directory are prioritized. For example, if there are files with the same names in both directories, systemd prioritizes the unit file stored in the /etc/systemd/system directory.

systemctl

Systemctl is a command-line tool used to monitor and control systemd. It has sub-commands used to start, stop, restart, reload, enable, and disable units. systemctl is also used to show the statuses of units and unit files available in the system.

List unit files with ‘systemctl list-unit-files’

To view the list of unit files available in the system, run the following command. You can see the files under /usr/lib/systemd/system or /etc/systemd/system but running this command makes viewing the list easier.

List unit files with the systemctl list-unit-files command

If you want to show services only, add the -t service option shown below. The -t option can select only a specified type of unit.

Command Line - INPUT
systemctl list-unit-files -t service

You can see the list of service unit files like the one below.

Command Line - RESPONSE
UNIT FILE                            STATE           VENDOR PRESET
accounts-daemon.service              enabled         enabled      
acpid.service                        disabled        enabled      
apparmor.service                     enabled         enabled      
apport-autoreport.service            static          enabled      
apport-forward@.service              static          enabled      
apport.service                       generated       enabled      
apt-daily-upgrade.service            static          enabled      
apt-daily.service                    static          enabled
    :                                   :               :

To exit the view mode, press the q key.

List units with ‘systemctl list-units’

To view the list of active units in the system, run the following command.

List units with the systemctl list-units command

If you want to show services only, add the -t service option shown below. The -t option can select only a specified type of unit.

Command Line - INPUT
systemctl list-units -t service

You can see the list of active service units like the one below.

Command Line - RESPONSE
 UNIT                         LOAD   ACTIVE SUB     DESCRIPTION       >
  accounts-daemon.service     loaded active running Accounts Service  >
  acpid.service               loaded active running ACPI event daemon >
  apparmor.service            loaded active exited  Load AppArmor prof>
  apport.service              loaded active exited  LSB: automatic cra>
  atd.service                 loaded active running Deferred execution>
  blk-availability.service    loaded active exited  Availability of bl>
          :                      :      :      :         :

To exit the view mode, press the q key.


You can also learn this topic offline. Click AmazonKindle.