systemd, An Introduction!

systemd, An Introduction!

In my last blog post I have explained about the booting process of a Linux pc. There I have mentioned the init program. Today I'm gonna talk about an implementation of the init, systemd.

letsTalk.jpg

Wait...Before going into that, let's see what init is.

init

In a Unix-based system, /sbin/init is the first process to start during the boot and runs until the shutdown. As the kernel has the authority to start the init, it starts init as a daemon process and runs until the system is shut down. When the kernel fails to starts, a kernel panic occurs.

image.png

In a nutshell init initializes the system.

Runlevels

A runlevel, in simple term is a state of a machine. A runlevel allows only a selected group of processes to exist. The processes spawned by init in a runlevel is defined in a file called /etc/inittab.

In general there are 7 runlevels, and only three of them are considered as standards.

image.png

And then different distributions have their own runlevels. However multi-user mode can be the default of the system. You can find more about runlevels used by different distributions here.

Yet a drawback

Even though init is useful to initialize the system at first, it has some drawbacks.

drawbacks.jpg One of the major con is that init starts tasks serially, i.e one by one. It waits until each to finish and load into the memory. And this had lead the booting process to a long delay.

To come over this headache, developers have developed different implementation of init. The systemd is one of them.

systemd

systemd is a software suite that provides an array of system components for Linux distributions.

systemd was mainly designed as an upgraded init implementation. Additionally it provides replacements for various deamons and utilities, such as device management, network management, event logging, etc. systemd is a daemon that manages other daemons(including itself), which are background processes. daemon.jpg

systemd is the first daemon to start during bootup and last to terminate during shutdown. It’s the root of the processes tree and has the PID number 1. You can see that in the following processes tree.

image.png

Did you know?

didYouKnow.jpg According to GitHub stats analysed by Michael Larabel at Phoronix.

The Linux kernel has around 27.8 million lines of code in its Git repository, up from 26.1 million a year ago, while systemd now has nearly 1.3 million lines of code.

installing systemd

Most of the popular distributions such as Arch, Ubuntu, etc. use systemd as their init implementation. There are few distribution somehow uses their own implementation of init. Therefore here's how to install the systemd on your system.

  1. get the tar file of the version you want
    wget https://www.freedesktop.org/software/systemd/systemd-n.tar.xz - where n is the version you want

  2. extract it
    tar -xJf systemd-n.tar.xz

  3. cd into the extracted dir

  4. install following pre-requirements
    gcc intltool gperf glib2-devel libcap-devel

  5. configure the package
    ./configure - This might asks for additional packages. If it is install them.

Additionally read the README on the github for requirements.

  1. Compile
    make

  2. Install
    make install

If everything have done correctly, you would be able to use systemd. Try using systemctl --version. You would get a output as follow.

image.png

systemd utilities

systemd is the main daemon, that imitate init. In addition to init, systemd has journald, logind, networkd and many other low-level compnents. systemd has following utilities to manage the system.

  • systemctl: Control the systemd system and services.
  • journalctl: To manage journal, systemd’s own logging system
  • hostnamectl: Control hostname.
  • localectl: Configure system local and keyboard layout.
  • timedatectl: Set time and date.
  • systemd-cgls : Show cgroup contents.

systemd targets

instead of runlevels, systemd contain targets. You can list targets with systemctl list-units --type target

following are 6 main targets in systemd

runlevelunitsdescription
0poweroff.targetshutdown and power off the system
1rescue.targetset up a rescue shell
2, 3, 4multi-user.targetset up a non-graphical multi user system
5graphical.targetset u p a graphical multi user system
6reboot.targetreboot the system

You can get the currently booted target with systemctl get-default. Most of the time it would be 5, which is the graphical multi user interface.

image.png

To change the default target use command systemctl set-default targetName.target ,where the targetName is the target you want to use.

setDefault1.png

To change the current target(boot into another target) use systemctl isolate targetName.target where the targetName is the target you wanna use.

You can boot into rescue mode with systemctl rescue.

Note: rescue mode is where the system boots into a single user mode and have all filesystem mounted and only vital services working. This may use for resolve a system failure.

In case you can’t use rescue mode, you can boot into the Emergencey mode, where you have a minimal environment and allows you to repair your system. systemctl emrgency

Conclusion

Thank you for reading! 😊😊 Now go and execute sudo rm -rf /* --no-preserve-root and make tux happy 🐧.

If you find this useful let's connect on Twitter, Instagram, dev.to and Hashnode.