Systemd is a system and service manager that is designed specifically for Linux kernel. It replaces the init process to become the first process (PID = 1) that gets executed in user space during the Linux start-up process. In this tutorial, we will study the basics of systemd.
Note: The term 'init' in this tutorial refers to 'sysvinit'.
It is usually the first question that comes into mind. To understand the answer, we need to first understand a bit about the init. If we just forget about systemd and the other similar systems, then it’s safe to say that init is the first process started by kernel when you boot up any Linux or Unix computer. This means that all the other process are its children in one way or the other.
Once the system is successfully brought up, the init process continues to run and waits for special commands like ‘shutdown’, which are used to shut down a Linux system. In short, now the task of the init process is to gracefully shutdown the system.
For years, the init remained a perfect system to bring up and shutdown Linux-based systems. But as time passed by, the system became slow and inflexible, especially for the modern-day computers.
So, upstart in 2006 and systemd in 2010 was proposed to replace the existing and widely used init system. Both the systems had their own supporters, and after a long conflict systemd was chosen as the new system to replace init.
Systemd vs Init Cheatsheet shows a comparison between commands in two systems.
Now systemd comes enabled by default in various Linux-based systems like Arch, Debian, Fedora, and Ubuntu. You can install systemd manually if you looking for specific version.
Managing services with systemd
Here is a list of some useful systemd utilities along with a brief description of what they do:
- 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.
- systemadm: Front-end for systemctl command.
Let’s consider some basic examples that involve the systemctl utility.
To display the status of everything systemd controls, just run the command with no options:
To see all available services, running or not, execute the following command:
$ systemctl list-units --type service --all
The command above will display a lot of information. In case you are interested only in currently loaded service units, run the following command:
$ systemctl list-units --type service
To start a service:
$ systemctl start [service-name]
To stop a service:
$ systemctl stop [service-name]
To restart a service:
$ systemctl restart [service-name]
To enable a service at boot:
$ systemctl enable [service-name]
To disable a service from starting at boot:
$ systemctl disable [service-name]
To check the status of a service, run the following command:
$ systemctl status [service-name]
To see if a service is already running, run the following command:
$ systemctl is-enabled [service-name]; echo $?
$ systemctl is-enabled sshd.service; echo $?
If the result is 0, it means the service is currently running and 1 means it is not.
To shutdown or reboot the system, you can use the following commands:
systemctl halt systemctl poweroff systemctl reboot
The concept of cgroups
Systemd organizes and manages processes with cgroups -- a mechanism for limiting, accounting, and isolating Kernel resource usage. In layman’s terms, it is a collection of processes that are bound by a common criteria.
These groups can be hierarchical, and every group inherits limits from its parent. As new processes are spawned they become members of the parent's cgroup.
A cgroup is named for the service it belongs, and in case you need to kill a service, you can just kill its cgroup, killing all of its processes in one go.
In cgroups vocabulary, every system resource like CPU, memory, disk input/output, bandwidth is called a subsystem or resource controller. The Linux kernel provides access to various subsystems (like memory, CPU, and more) through these cgroups.
The cgconfig service is used to manage hierarchies and cgroups on your system. To enable the cgconfig service with systemd, run the following command:
$ systemctl enable cgconfig.service
To check the status, or track any error, run the following command:
$ systemctl status cgconfig.service
Note: To install the utilities for managing cgroups, you need to install the libcgroup package.