Systemd re-init in wsl

Mudasir Durrani
6 min readOct 26, 2022

what’s the hype around systemd and why is it so crucial to Linux-based OS

Introduction

In this blog we are going to cover the basics of systemd both theoretically and practically, what exactly is systemd, what is included in systemd, how you’re going to be interacting with systemd, what are the main units and services in systemd, how systemd handles dependencies and execution order and finally you’re also going to get all the ingredients that you need to make a service in systemd and manage it. We are also going to show you a way to add systemd to WSL in windows 10 machines.

So here’s an overview of what we are about to cover in this blog

.The Init process

.What are systemd (and its components)

.Units and services in systemd

.How we interact with it

.All the ingredients for making and managing a service in systemd

.Sytemd in WSL

.How to get systemd on windows 10 and below

Init

So long story short init is a special process that is started by the kernel it’s a special process because it’s the first process that really gets started by the kernel during boot, the process id or pid is one and it’s a process that not only is it responsible for kind of booting the rest of the system and getting the system to a state where things are working, people can log in etc but in addition to starting services and getting the system there it’s also a long running process that has some power over other processes for example when a process is orphaned because its parent process dies it is re-parented under init so it’s kind of the ultimate mommy on the system it takes care of orphans it kills zombie processes it’s responsible for processes that can go wrong during the normal operation of a system.

Init used to be a series of plain text scripts essentially in etc or etc/init.d that’s what’s known as sys5 style init or you might see it written as SysV-style-Init that’s kind of the older UNIX way of doing it.

What is Systemd

In Init you have a bunch of plain text scripts that get run essentially in order the user can kind of mung that order around but long story short there were some drawbacks to that and systemd has kind of replaced how that works in linux it is not a UNIX init system. It is at this point purely in linux so really when you think of systemd it is init but it also handles all system state and services during boot and then during the time that that system is up and running.Systemd is really a collection of programs and libraries it’s not even just one binary:

.systemctl

.journalctl

.Init

.Process management

.Network management (networked)

.Login management (logind)

.Logs(journald)

These are no longer plain text logs like in init, they’re binary files.

Units and services

What is a systemd unit well a unit is really what you might have called a service before on an older style of init. A unit is any entity managed by systemd and that can be a bunch of different things:

.Service

.Socket

.Device

.Mount or Automount point

.Swap file

.Partition

.Startup target

.Watched filesystem path( checking for available entities before a service starts)

.group of externally controlled processes

So as you can see it's much more than just service management its dependency management, requirement management, and target management of all kinds of different entities

How we interact with it

Let's start with where we can find systemd units files:

.lib/systemd/system (standard systemd unit files)

.usr/lib/systemd/system (from locally installed packages like apt-get)

.etc/systemd/system (for custom unit files)

As we have mentioned before systemd boots up the system and it's much faster than init because it starts up the daemons in parallel and no more S and K links as we don't have to start the daemons in a specific order. Systemd can restart a daemon if it crashes and also stop it(instead of znet.d in init). Systemd also has better security because as audits cannot be stopped by anyone including the root user so any activity from the hacker can be monitored. Each process has its own “cgroup” so it has its own restricted resources and this prevents hacker from exploiting the system.

Creating a systemd unit

Now we’ll go through some practical steps to make a unit file in systemd’s custom unit file location as we mentioned above. first of all we change directory to the custom unit file location using:

cd /etc/systemd/systemd

Then we will create a file with an_name.service, as you can see you need root privileges so use sudo. When you create a file the file may not have writing and execution permissions so let’s change that. We changed the root user privileges and added write and execution privileges as you can see below.

Now we edit this file using vim editor.

After this command, a window will pop-up and we have to add the following scripts.

Then we will close the window while saving our script and check the status this service using systemctl.

The reason this program is inactive is because there is no usr/local/bin/testprogram file that is backed by a systemd unit file.

Systemd and WSL

systemd was not previously available in WSL but since 22 September 2022, it is now available in windows 11-based WSL from the Microsoft store. The reason systemd was not included in WSL was because systemd has frowned upon by developers who did not like to shift from init simply because of being attached. But in windows 10 and lower versions systemd is still not officially available.

But I am providing a script that can add systemd to your WSL.

first install git using:

sudo apt install git

and then run the following script:

now you can check that your system has systemd:

--

--

Mudasir Durrani

I’m a life-long student stumbling through the intricate maze of life