linux tux

What is systemd and how does it work?

systemd is a suite of tools that is meant to manage the machine and its services from the boot process, and it is an init system.

An init system is responible of managing the lifecycle of processes and services since the boot of the machine, and provide tools and controls to manage and monitor the state of the system during its execution.

Imagine to have an assistant that as soon as you turn on your machine is deputed to manage the execution of your core services on the machine, allowing you to see if something is going wrong and eventually restart your services when needed.

Units are organized in files, with an extension corresponding to the type of unit, for instance .target for targets, .service for services and so on.

Unit files are stored in three different locations:

/usr/lib/systemd/user/ –> Here the ‘templates’ for unit files are stored. You should not edit those files here, but rather copying it on the highest precedence folder, then edit them there.

/run/systemd/units/ –> Runtime location for unit files, here you can find actually running (invocated) units for the running system.

/etc/systemd/system/ –> This directory takes precedence over the other directories, and can be used to stor new services that have to be registered to systemd

Nice to meet you systemctl!

The control interface for systemd is the control command systemctl that represents the command available to users to interact with systemd daemon.

With systemctl we will be able to:

  • List available units
  • Enable services
  • Check the status of services
  • Start/stop/restart services
  • Choose boot target (graphic, multi-user, emergency, etc)
  • Define timers
  • Etc.

Let’s start with the beginning, now we will see what is the command to list all the available untis at the moment we are running the command, despite they are running, enabled or not. I will avoid printing the output since it is 200+ lines, so no need to spam it!

With this command, you can see the whole list of enabled/not enabled started/not started units defined in systemd.

Starting/Stopping services

With systemctl you can also start and stop services. It is very easy and intuitive, but it requires a little time to get used if you come from init.d.

Once you started the service, you can check the status with the ‘status’ command:

To stop a service, simply use the ‘stop’ command.

As you can see, stop and start are not verbose at all if everything goes right. We will then see how to inspect services logs with journald.

Service reload/restart

I wanted to separate restart and reload from start and stop for mainly two reasons. The first one is about order and clean reading! The second one regards the fact that even if they can seem similar, there are deep differences between these two commands.

First of all, we need to specify that service behaviours are related to the init scripts capabilities.

This means that methods like start, stop, reload, restart are supported as long as they are supported from the init script provided with the service.

When we talk about the restart, it is very simple, it is only needed that the init script provides a start and stop command, so that they can be called sequencially during the restart.

Restart, as I said, means stop&start, so what if we just need to reload a particular part of configuration without downtime?

Yay! systemctl reload on the rescue!

With reload if the service supports it, it will just reload the configuration, so that if any changes occurred it could be easily integrated without downtime.

Great, isn’t it?

Enable/Disable units

Reloading stuff on boot is very comfortable, it allows you to use your favourite services withour needing to restart them manually at every reboot.

BUT! You need to first enable them!

Once you installed your unit in the above mentioned folders, you can then enable (or disable) them, so that they will run (or not run) at boot time:

For instance, now the httpd service is enabled and will start at boot.

If we want to disable it, just use the dual command:

If you need to enable a service at boot and starting it at the same time, just use the –now option!

The output is the same as the simple enable, but I can assure it also started the service, 100%!

Conclusion

For more information about systemd, you can read its man page http://man7.org/linux/man-pages/man1/systemd.1.html

We will probably see it again in another article, trying to explain how processes work, and how kernel manages resources when it comes to containers and virtual machines.

If you like the article, feel free to share it!