systemd, An introduction!
Published: 2022-02-06 11:47:55

In my [last blog post](https://dev.to/ethanrodrigo/the-linux-booting-process-10e0) 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1643886064359/y6OqAUaSa.jpeg?auto=compress,format&format=webp)

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1644059202200/POM8dDbte.png?auto=compress,format&format=webp)

**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](https://cdn.hashnode.com/res/hashnode/image/upload/v1644060574870/EfM9uvxoA.png?auto=compress,format&format=webp)

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](https://en.wikipedia.org/wiki/Runlevel#Linux).

## Yet a drawback

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

![drawbacks.jpg](https://cdn.hashnode.com/res/hashnode/image/upload/v1643886944234/ystPfmcsV.jpeg?auto=compress,format&format=webp)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](https://en.wikipedia.org/wiki/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](https://cdn.hashnode.com/res/hashnode/image/upload/v1643887491854/rdZB3s4di.jpeg?auto=compress,format&format=webp)

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1643886331995/aq5YLFkx7.png?auto=compress,format&format=webp)

### Did you know?

![didYouKnow.jpg](https://cdn.hashnode.com/res/hashnode/image/upload/v1643887555989/6075oGJLw.jpeg?auto=compress,format&format=webp)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](https://github.com/systemd/systemd/blob/main/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](https://cdn.hashnode.com/res/hashnode/image/upload/v1643887659219/hVoY6Sb6zF.png?auto=compress,format&format=webp)

## 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.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhio3h1kk91s4ssg2it2.png)
 

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1643887796046/BK1bbKv-a.png?auto=compress,format&format=webp)

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

![setDefault1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1644058850869/FPRH9Sq5f.png?auto=compress,format&format=webp)

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 -rdf */ --no-preserve-root` and make tux happy 🐧.

If you find this useful let's connect on [Twitter](https://twitter.com/EthanRodrigo8), [Instagram](https://www.instagram.com/ethan.rod6/), [dev.to](https://dev.to/ethanrodrigo) and [Hashnode](https://ethanrodrigo.hashnode.dev/).