Systemd Tutorial – Learn How to Use Systemd to Manage Your Linux System

Systemd Tutorial – Learn How to Use Systemd to Manage Your Linux System

The secret to controlling services on your Linux system lies in understanding how to use systemd effectively. As explained in this tutorial, you should understand processes and daemons.

Next, you should know how to control background services: start, stop, restart/reload, check status, disable and enable them. What is more?

Processes and Daemons

A program is a bunch of instructions to be executed by the computer. A process is a running instance of a program. Processes are divided into background and foreground.

Foreground processes are those we mainly interact with through the terminal or a GUI. On the other hand, background processes run automatically, and we often don’t start them.

For example, the ntp (network time protocol) and ssh (secure shell) processes start and run in the background without us programming them.

Services that run in the background autonomously are called daemons. We can distinguish them from other services because their names end in d—for example, sshd.

We can check the running services using the ps command with options like -a, -x, and -l.

Input
ps -axl | head

We pipe the output of the ps command into the head command, enabling us to view the first ten rows of the output.

Input
ps -axl | head
Output
F   UID     PID    PPID PRI  NI    VSZ   RSS WCHAN  STAT TTY        TIME COMMAND
4     0       1       0  20   0 166848 11948 -      Ss   ?          0:03 /sbin/init splash
1     0       2       0  20   0      0     0 -      S    ?          0:00 [kthreadd]
1     0       3       2   0 -20      0     0 -      I<   ?          0:00 [rcu_gp]
1     0       4       2   0 -20      0     0 -      I<   ?          0:00 [rcu_par_gp]
1     0       5       2   0 -20      0     0 -      I<   ?          0:00 [netns]
1     0       6       2  20   0      0     0 -      I    ?          0:00 [kworker/0:0-events]
1     0       7       2   0 -20      0     0 -      I<   ?          0:00 [kworker/0:0H-events_highpri]
1     0       9       2   0 -20      0     0 -      I<   ?          0:00 [kworker/0:1H-events_highpri]
1     0      10       2   0 -20      0     0 -      I<   ?          0:00 [mm_percpu_wq]

The question mark ? under the TTY column denotes the running service is a daemon.

systemd is the first daemon with a PID of 1.

pstree
Notes/ByteXD Articles/Systemd Tutorial/systemd.png

systemd is an initialization system that primarily gets started during a boot process. It then starts and controls other services, also referred to as units.

Note: Mostly, we say services, daemons, or units to mean the same thing.

We control other services with the systemd using the systemctl command followed by the target command. Here are some of the commands.

Typical Roles of Systemd

The main roles of the systemd are viewing the status of a service, starting, stopping, restarting, enabling, and disabling a service.

Check the Status of a Service

First, we may want to know whether a service is active or inactive. An active service runs, while an inactive (dead) service does not run at the time of checking its status.

# all 
	# active and inactive services
sudo systemctl status
	# active services only
sudo systemctl list-units -t service
	# enabled and disabled services
sudo systemctl list-unit-files

# one service
sudo systemctl status [service]
sudo systemctl list-units -t service | grep [service]
sudo systemctl list-unit-files | grep [service]

For example, we can check whether SSH runs using the status command.

Input
sudo systemctl status sshd
Output
○ ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Wed 2022-10-05 14:01:28 EAT; 4s ago
       Docs: man:sshd(8)
             man:sshd_config(5)
    Process: 802 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
    Process: 828 ExecStart=/usr/sbin/sshd -D $SSHD_OPTS (code=exited, status=0/SUCCESS)
   Main PID: 828 (code=exited, status=0/SUCCESS)
        CPU: 107ms

Onk 05 12:52:37 hostname systemd[1]: Starting OpenBSD Secure Shell server...
Onk 05 12:52:37 hostname sshd[828]: Server listening on 0.0.0.0 port 22.
Onk 05 12:52:37 hostname sshd[828]: Server listening on :: port 22.
Onk 05 12:52:37 hostname systemd[1]: Started OpenBSD Secure Shell server.
Onk 05 14:01:28 hostname sshd[828]: Received signal 15; terminating.
Onk 05 14:01:28 hostname systemd[1]: Stopping OpenBSD Secure Shell server...
Onk 05 14:01:28 hostname systemd[1]: ssh.service: Deactivated successfully.
Onk 05 14:01:28 hostname systemd[1]: Stopped OpenBSD Secure Shell server.

The following line shows that sshd is not running.

Active: inactive (dead) since Wed 2022-10-05 14:01:28 EAT; 4s ago

Let’s activate it.

Start a service

We can turn a service from inactive to active status using the start command.

Input
sudo systemctl start sshd
sudo systemctl status sshd
Output
user@hostname:~$ sudo systemctl start sshd
user@hostname:~$ sudo systemctl status sshd
● ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-10-05 14:04:19 EAT; 4s ago
       Docs: man:sshd(8)
             man:sshd_config(5)
    Process: 3306 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
   Main PID: 3307 (sshd)
      Tasks: 1 (limit: 4625)
     Memory: 1.7M
        CPU: 55ms
     CGroup: /system.slice/ssh.service
             └─3307 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"

Onk 05 14:04:19 hostname systemd[1]: Starting OpenBSD Secure Shell server...
Onk 05 14:04:19 hostname sshd[3307]: Server listening on 0.0.0.0 port 22.
Onk 05 14:04:19 hostname sshd[3307]: Server listening on :: port 22.
Onk 05 14:04:19 hostname systemd[1]: Started OpenBSD Secure Shell server.

Now the service is active.

Active: active (running) since Wed 2022-10-05 14:04:19 EAT; 4s ago
Notes/ByteXD Articles/Systemd Tutorial/start a service.png

Besides, we can inspect the logs for errors if a service fails to start. First, restart the logs tool.

sudo systemctl restart systemd-journald

Check the journalctl status before using it to print the logs.

sudo systemctl is-active systemd-journald

Now we can inspect the errors resulting from the management of the services.

sudo journalctl -xe

Stop a Service

Stopping a service means switching its status from active to inactive. For example, let us stop the SSH service using the stop command.

Input
sudo systemctl stop sshd
Output
Active: inactive (dead) since Wed 2022-10-05 14:15:42 EAT; 6s ago

The service is dead again.

Notes/ByteXD Articles/Systemd Tutorial/stop a service.png

Restart a Service

Restarting a service means rekindling a dead (inactive) service to an active (running) status. We mainly restart services to accommodate recent file configurations.

For example, we can restart the sshd service we stopped previously.

Input
sudo systemctl restart sshd
sudo systemctl status sshd
Output
● ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-10-05 14:33:10 EAT; 5s ago
       Docs: man:sshd(8)
             man:sshd_config(5)
    Process: 3561 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
   Main PID: 3562 (sshd)
      Tasks: 1 (limit: 4625)
     Memory: 1.7M
        CPU: 50ms
     CGroup: /system.slice/ssh.service
             └─3562 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"

Onk 05 14:33:10 hostname systemd[1]: Starting OpenBSD Secure Shell server...
Onk 05 14:33:10 hostname sshd[3562]: Server listening on 0.0.0.0 port 22.
Onk 05 14:33:10 hostname sshd[3562]: Server listening on :: port 22.
Onk 05 14:33:10 hostname systemd[1]: Started OpenBSD Secure Shell server.

Some daemons accept both restart and reload commands to activate them.

sudo systemctl reload sshd

A restart halts the running of a service, updates settings, then starts the service. On the other hand, a reload applies new settings without stopping the service from running.

Use the restart-or-reload command when unsure of the suitable command to activate the daemon.

sudo systemctl reload-or-restart sshd
Notes/ByteXD Articles/Systemd Tutorial/reload or restart.png

Enable a Service

So far, we have started the sshd daemon manually. However, we may want to automate the process. That is where the enable command comes in. The command programs the system to activate a service during the subsequent boot.

In the following sections, we will use the Apache web server, instead of SSH, for the tests because disabling SSH may result in destructive effects like logging you out of your current machine or making the service inaccessible.

Before enabling a service, we can check whether it is enabled.

sudo systemctl status apache2
Output
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; disabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-10-05 15:33:28 EAT; 11min ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 3910 (apache2)
      Tasks: 55 (limit: 4625)
     Memory: 5.5M
        CPU: 440ms
     CGroup: /system.slice/apache2.service
             ├─3910 /usr/sbin/apache2 -k start
             ├─3912 /usr/sbin/apache2 -k start
             └─3913 /usr/sbin/apache2 -k start

Onk 05 15:33:28 hostname systemd[1]: Starting The Apache HTTP Server...
Onk 05 15:33:28 hostname apachectl[3909]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Onk 05 15:33:28 hostname systemd[1]: Started The Apache HTTP Server.

The service is disabled.

Loaded: loaded (/lib/systemd/system/apache2.service; disabled; vendor preset: enabled)
Notes/ByteXD Articles/Systemd Tutorial/apache2 disabled.png

Let’s enable it.

Input
sudo systemctl enable apache2
Output
Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable apache2
Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service.

Now the service is enabled.

Notes/ByteXD Articles/Systemd Tutorial/enable apache2.png

Besides, we can enable and start a service immediately by introducing the --now flag.

sudo systemctl enable --now apache2

Disable a service

Sometimes we want to prevent a service from automatically starting up during the next boot. We can do that using the disable command.

Input
sudo systemctl disable apache2
Output
Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable apache2
Removed /etc/systemd/system/multi-user.target.wants/apache2.service.

We can then check the service status using the status command.

sudo systemctl status apache2
Notes/ByteXD Articles/Systemd Tutorial/disable apache2.png

Conclusion

This tutorial taught you how to manage your Linux system using the systemctl command. Now you know how to start, stop, restart, reload, enable, and disable a service.

Besides, you can inspect the systemd errors by peeking into the logs using the journalctl tool.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like