Debian

How to use systemd in Debian

The root in the tree structure of Linux kernel processes is systemd. As a result, it may be used to conduct highly useful activities such as automatically controlling your services and machine, running them on boot. We will look at all of the key activities that one can perform using systemd. We will begin with an introduction and then move on to the implementation. Let’s get started!

What is systemd?

The systemd software suite provides the foundation for the Debian Operating system. The root process in the process tree of Linux is responsible for managing other processes and applications, and it is used to control applications in kernel-level mode. For instance, running Docker as a service.

How to Use Systemd for Various Tasks

List Unit files

Systemd employs ”unit” to handle all the system services and processes. Systemd units use configuration files to govern their different activities. Unit configuration files are classified into three types:

“default unit configuration files” with configuration files contained in the directory “/usr/lib/systemd/system

“system-specific unit configuration files” with system-specific unit configuration files contained in “/etc/systemd/system

“run-time unit configuration file”” with configuration files found in the directory” “/run/systemd/system

Below is the command to list Unit files along with their output

$ systemctl list-unit-files

Output

UNIT FILE                                     STATE  
proc-sys-fs-binfmt_misc.automount             static  
dev-hugepages.mount                           static
dev-mqueue.mount                              static
proc-sys-fs-binfmt_misc.mount                 static  
sys-fs-fuse-connections.mount                 static  
sys-kernel-config.mount                       static  
sys-kernel-debug.mount                        static  
tmp.mount                                     disabled
brandbot.path                                 disabled
systemd-ask-password-console.path             static  
systemd-ask-password-plymouth.path            static  
systemd-ask-password-wall.path                static  
session-1.scope                               static  
arp-ethers.service                            disabled
auditd.service                                enabled
autovt@.service                               enabled

Try using Linux grep command to filter only enabled services.

$ systemctl list-unit-files |grep enabled

Output

auditd.service                                enabled
autovt@.service                               enabled
crond.service                                 enabled
dbus-org.fedoraproject.FirewallD1.service     enabled
dbus-org.freedesktop.nm-dispatcher.service    enabled
firewalld.service                             enabled
getty@.service                                enabled
irqbalance.service                            enabled
kdump.service                                 enabled
lvm2-monitor.service                          enabled

Start and Stopping Services a Service

$ sudo systemctl start <service_name>
$ sudo systemctl stop <service_name>

Below is an example of running a docker container as a service.

To run your application (Let’s call it “X”) as a systemd service, create the following file in the “/etc/systemd/system” directory and name it as
‘docker.some_name.service’ :

[Unit]
Description=X container
After=docker.service
Wants=network-online.target docker.socket
Requires=docker.socket

[Service]
Restart=always
ExecStart=/usr/bin/docker start -a some_name
ExecStop=/usr/bin/docker stop -t 10 some_name

[Install]
WantedBy=multi-user.target

To start a service with a system boot, use the following command:

systemctl enable docker.some_name

Use the following commands to stop and start the service manually:

sudo service stop docker.some_name
sudo service start docker.some_name

Check Service Status

Below is the command to check the status of the running service.

$ sudo systemctl status <service_name>

Restart a Service

You can also restart a service without needing to stop and start it, which requires more effort. Below is the command to perform this task.

$ sudo systemctl restart firewalld

Reboot and Shutdown

Below are the commands to reboot and shut down the system, respectively. (As a system admin, you should also know how to shut down the system using a command line)

$ sudo systemctl reboot
$ sudo systemctl poweroff

Starting Services at Boot-Time

Services like Docker need to be started as soon as your machine is turned on. To accomplish it, you can mention the below command.

$ sudo systemctl enable firewalld

Try booting your machine, and you will notice that the service starts automatically without human interaction.

Conclusion

In this post, we saw how systemd could perform common administration and configuration tasks with your machine. A system administrator can use the above commands to automate the configuration of services running in your machine. Make sure to try these commands with your custom services and realize the power of the systemd.

About the author

Simran Kaur

Simran works as a technical writer. The graduate in MS Computer Science from the well known CS hub, aka Silicon Valley, is also an editor of the website. She enjoys writing about any tech topic, including programming, algorithms, cloud, data science, and AI. Travelling, sketching, and gardening are the hobbies that interest her.