Linux SystemD

Overview

On my journey to Linux, I have found that I needed to use sudo systemctl ... quite a lot to manipulate services and systems. Here are my notes on my findings so far.

What is SystemD

SystemD is a software suite on Linux, it uses multiple system applications to manage services. It replaces older system and service management system like System V. It aimed to harmonize the management between the existing linux distributions. To achieve this it provides replacements for various daemons and utiities such as device management, login management, network management, event logging…

ℹ️ Note

systemd comes from the Unix convnetions to name daemons by appending the letter ’d’. Of course there is also a double meaning as “System D” refers to the ability to adapt quickly to solve problems.

systemd is configured with plain-text files called unit-file using declarative language instead of bash scripts.

Commands

systemctl

  • status : gives information of a given process/service. Ex: systemctl status docker
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fha@TESTBOX:~/Repos/FhaNotes$ systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; preset: enabled)
     Active: active (running) since Sat 2024-11-16 13:43:02 CET; 1 week 0 days ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 1822 (dockerd)
      Tasks: 102
     Memory: 242.9M (peak: 272.8M)
        CPU: 5min 43.273s
     CGroup: /system.slice/docker.service
             ├─1822 /usr/bin/dockerd -H fd:// -H tcp://127.0.0.1:2375 --containerd=/run/containerd/containerd.sock
             ├─2214 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 32768 -container-ip 172.17.0.2 -container-port 5000
             ├─2221 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 32768 -container-ip 172.17.0.2 -container-port 5000
             ├─2228 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 9000 -container-ip 172.17.0.3 -container-port 9000
             ├─2236 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 9000 -container-ip 172.17.0.3 -container-port 9000
             ├─2247 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 9443 -container-ip 172.17.0.3 -container-port 9443
             ├─2254 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 9443 -container-ip 172.17.0.3 -container-port 9443
             ├─5854 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 6667 -container-ip 172.18.0.2 -container-port 6667
             └─5861 /usr/bin/docker-proxy -proto tcp -host-ip :: -host-port 6667 -container-ip 172.18.0.2 -container-port 6667

Nov 16 13:43:02 TESTBOX dockerd[1822]: time="2024-11-16T13:43:02.519291865+01:00" level=info msg="API listen on /run/docker.sock"
Nov 16 13:43:02 TESTBOX dockerd[1822]: time="2024-11-16T13:43:02.519301540+01:00" level=info msg="API listen on 127.0.0.1:2375"
Nov 16 13:43:02 TESTBOX systemd[1]: Started docker.service - Docker Application Container Engine.
  • start
1
fha@TESTBOX:~/Repos/FhaNotes$ systemctl start docker
  • stop
1
2
3
fha@TESTBOX:~/Repos/FhaNotes$ systemctl stop docker
Stopping 'docker.service', but its triggering units are still active:
docker.socket
  • restart
1
fha@TESTBOX:~/Repos/FhaNotes$ systemctl start docker
  • enable
1
2
3
4
fha@TESTBOX:~/Repos/FhaNotes$ sudo systemctl enable docker
Synchronizing state of docker.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
  • disable
1
2
3
4
5
6
7
fha@TESTBOX:~/Repos/FhaNotes$ sudo systemctl disable docker
[sudo] password for fha:          
Synchronizing state of docker.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install disable docker
Removed "/etc/systemd/system/multi-user.target.wants/docker.service".
Disabling 'docker.service', but its triggering units are still active:
docker.socket
  • reload In some specific situation, this can be used to reload the configuration of a service without completely restarting it. A usefull usecase is when configuration from a webserver must be updated, to prevent disconnecting users, the reload option can be used. This works only if the service is actually implementing that functionality.

  • edit This command edits the config file of a service. There is 2 differentr way to use it. The default creates a delta file with the changes of the file in a subdirectory. The otehr way with –full will create a copy of the config file from the /lib/systemd/system folder to the /lib/systemd/system folder if it doesnt exists, and let you edit the full file as is.

1
sudo systemctl edit --full docker.service
  • daemon-reload When updating service configuration, this allows to reload the sytemd service to take the new changes into consideration.
1
sudo systemctl daemon-reload

systemd-analyze

1
2
3
fha@TESTBOX:~/Repos/FhaNotes$ systemd-analyze
Startup finished in 45.965s (firmware) + 10.776s (loader) + 1.867s (kernel) + 14.834s (userspace) = 1min 13.444s 
graphical.target reached after 14.789s in userspace.

Blame command systemd-analyze blame

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
fha@RIVA-SHUTTLE:~/Repos/FhaNotes$ systemd-analyze blame 
17.741s fstrim.service
 5.798s NetworkManager-wait-online.service
 4.052s docker.service
 3.793s fwupd.service
 3.398s apt-daily.service
 2.105s systemd-udev-settle.service
 2.011s systemd-journald.service
 1.302s apt-daily-upgrade.service
 1.191s NetworkManager.service
 ...

Check Unit-File before forcing the system to run :

1
systemd-analyze verify /etc/systemd/system/docker.service

Generate a pictures showing the bootup load sequence and time spent.

1
systemd-analyze plot > /tmp/bootup.svg

systemd-analyze plot output

Service files (Unit-Files)

Extension : .service The files can be in several folders in order of priority:

  • /etc/systemd/system :
  • /run/systemd/system : shows
  • /lib/systemd/system : When new packages are installed for services, the .service file will be created in this folder. Files created here can be overwritted when the package is updated.

3 sections

  • Unit : General information about the service.
  • Service : Defines how service is started, stopped or reloaded.
  • Install : -

Unit file Types

  • .service
  • .socket
  • .device (automatically initiated by systemd[67])
  • .mount
  • .automount
  • .swap
  • .target
  • .path
  • .timer (which can be used as a cron-like job scheduler[68])
  • .snapshot
  • .slice (used to group and manage processes and resources[69])
  • .scope (used to group worker processes, not intended to be configured via unit files[70])

References