What is systemd?

·

5 min read

Prerequisite

  • Have a basic understanding of Linux
  • Have a basic understanding of the Linux Filesystem

Expectations

  • Understand systemd and its purpose
  • Understand how systemd works and fits in the big picture
  • Understand the relationship between Init and systemd
  • Understand the relationship between systemctl and systemd

Introduction

Three major steps occur when a Linux machine is turned on:

  1. Hardware boot: Initializes the system hardware
  2. Linux boot: Loads the Linux kernel and then systemd or Init
  3. Linux startup: Where systemd or Init prepares the host machine for productive work

systemd is the program for managing processes in the Linux operating system. It is a daemon or process manager that starts as soon as the system is powered on.

In Linux, daemon processes start with the letter 'd'. For example, httpd which is used to play the role of server in a client-server model using HTTP and/or HTTPS network protocols, has a 'd' at its end showing it is a daemon process. Other examples include init.d.

Process

A process is a task or a program your Linux machine is currently working on at any point in time it can be running, sleeping, or zombie. When you type in a command on the terminal in a Unix/Linux machine, it starts a process, when you open a program e.g your google browser, your Linux system starts a process for that.

Some processes run in the background meaning another process can run while it's running and some run in the foreground meaning no other process can run while it is running. To see the processes currently running on your Linux machine, in the terminal type

ps

It displays the:

  • Process ID PID
  • The terminal linked to the process `TTY``
  • The cumulated CPU TIME in hh:mm:ss format
  • The executable name CMD

Screenshot from 2022-09-18 12-35-55.png

Daemons

Imagine You go out for a dinner in a nice restaurant and everything just seems to work perfectly, the waiter is nice and seems to be exactly where you need them to be, the meals come on time, and everything is perfect which is what you want because you just want to have your meal in a nice place.

In the background, there's the restaurant manager, running around, making sure everything is right where it is supposed to be, everyone is where there are supposed to be, doing what they are supposed to do, the way they are supposed to do it. That is likened to what a daemon process does.

Daemons are utility programs that run silently in the background to monitor and take care of certain subsystems to ensure that the operating system runs properly.

The pstree command is a utility that shows the processes currently running on your system and shows them in a tree diagram. This tree diagram shows the processes in a hierarchy format, the child processes are shown under the parent processes which makes them easier to understand and look up. Open up a terminal and type in this command:

pstree

Screenshot from 2022-09-18 13-39-06.png

systemd vs Init

In Linux, init is an abbreviation for Initialization. Before the existence of systemd, Init was used. The init is also a daemon process that starts as soon as the computer starts and continues running till it is shut down. It is, just like systemd the first process that starts when a computer boots, making it the parent of all other running processes directly or indirectly, and hence typically it is assigned “pid=1“.

Init runs its services in a particular sequence meaning one process has to run before the next runs. The inability of Init to run processes in parallel was one of the pain points that led to its replacement with systemd . The following are alternatives that were developed, before systemd was generally accepted.

  • Upstart – A init replacement daemon implemented in Ubuntu GNU/Linux and designed to start processes asynchronously.
  • Epoch – A init replacement daemon built around simplicity and service management, designed to start process single-threaded.
  • Mudar – A init replacement daemon written in Python, implemented on Pardus GNU/Linux and designed to start processes asynchronously.
  • systemd – A init replacement daemon designed to start processes in parallel, implemented in several standard distributions – Fedora, OpenSuSE, Arch, RHEL, CentOS, etc.

systemd

systemd is the program for managing processes in the Linux operating system.

It is a daemon or process manager that starts as soon as the system is powered on. systemd may refer to all the packages, utilities, and libraries around daemon, it is a package of programs used for just about all process control, not just startup.

When other processes are being run in the system, systemd runs them in parallel for better efficiency while communicating with each other via sockets for on-demand communication. Sockets are a way to allow messages to be sent and received between programs running on a server, or between programs running on separate servers.

An advantage of systemd over init that allows it to be the chosen preference in recent Linux distributions is its ability to run processes in parallel while communicating with each other via sockets.

systemd and systemctl

The systemctl command is a utility that is responsible for examining and controlling the systemd system and service manager. It can be likened to a control interface for systemd service, allowing one to communicate with systemd and perform operations.

To use systemctl to start a service:

sudo systemctl start servicename

sudo systemctl start apache2

In this example, I am not running this command as the root-user hence the need for sudo

To use systemctl to stop a service:

sudo systemctl stop servicename

sudo systemctl stop apache2

Conclusion

Init and systemd are both init daemons, Init uses service whereas Systemd uses systemctl to manage Linux services. systemd provides extensive parallelization during startup, better management of processes, and overall a saner, dependency-based approach to the control of the Linux system it is the modern replacement for the legacy Linux initialization systems, System V (SysV) and Init.