An Ultimate Guide of How to Manage Linux Systemd Services With Systemctl Command

September 8th, 2022
An Ultimate Guide of How to Manage Linux Systemd Services With Systemctl Command

Systemd is the default service manager that provides core functionality for many modern Linux distros, and systemctl is how users can interface with systemd services. That makes systemctl an important part of a Linux administrator's toolbox.

In this article, we'll explore how you can use systemctl to perform common administration tasks on systems that use systemd

What Is Systemd?

Systemd is a suite of tools that manages key aspects of Linux systems including services, network configuration, runtime settings, and logging. Systemd is sometimes called a "system and service manager" for Linux distros.

One of the most important functions of systemd is acting as an init system. The init system is the first process started after the Linux kernel boots and always uses a process ID (PID) of 1. The init system starts other processes and manages them until the system shuts down. Today, systemd is the default init system on many popular flavors of Linux including Ubuntu, Debian, Fedora, and openSUSE.

However, while systemd is popular, it's not without controversy. There are a variety of other init systems available like Upstart, SysVinit, and OpenRC, and many in the open source community were averse to seeing systemd gain widespread adoption. Criticisms of systemd often centered around the ideas that systemd design went against the Unix philosophy and it is too bloated and complex for what an init system should be. As an example of just how intense the aversion to systemd is, note there is still a "Not systemd" filter on Distro Watch.

We won't rehash all the details of the "init wars" of the 2010s here. Instead, we'll focus on the technical side of systemd for the rest of the article.

What Is The Linux Systemctl Command?

The Linux sytemctl command allows you to view and control the systemd init system and services.

In many ways, the Linux systemctl command acts as an interface to systemd. For example, you can use systemctl to perform administrative tasks like:

  • Starting/stopping services
  • Enabling/disabling services
  • Viewing a service's status
  • Checking a service's dependencies

How To Manage Systemd Services With Systemctl Command

Now let's dive into using the systemctl command to manage systemd services.

Prerequisites

Before we begin, you'll need:

  • Access to the terminal of an Linux system that uses systemd. We'll use an Ubuntu 20.04 virtual private server (VPS). If you don't have one, you can spin up a VPS on Cherry Servers.
  • sudo/root privileges

Systemd Service Management

Let’s begin by reviewing commands used to work with Systemd services.

Check The Status Of A Service With The Linux Systemctl Command

To check the status of a specific service, the general systemctl command syntax is:

systemctl status <servicename>

For example, to check the status of the ufw firewall:

systemctl status ufw

Output should look similar to:

systemctl status ufw

In the output you can see not only the status of the service and PID, but also a snippet of recent logs from journalctl.

Note that you can also use the full <servicename.service> unit file name (like ufw.service) to run the systemctl statuscommand and other service-related systemctl commands.

Show Properties Of a Service With The Linux Systemctl Command

Systemd properties define granular information on how units behave. For example, DefaultEnvironment defines the environment variables passed to processes. Many property values are defined in systemd-system.conf.

To show all the properties for a systemd service, use this general command syntax:

systemctl show <servicename>

For example, to view all the properties for apache2 execute this command:

systemctl show apache2

Output should look similar to:

systemctl show apache2

To display a specific property, specify it using the -p switch. For example, to display apache2's UMask property, use:

systemctl show -p UMask apache2

Output should be similar to:

systemctl show apache2 property

Starting And Stopping Services With The Linux Systemctl Command

Now, we'll use the Linux systemctl command to demonstrate different ways to modify the status of a service using the Apache web service, apache2, as an example.

Stop A Service

The general syntax to stop a service is:

systemctl stop <servicename>

To stop apache2, run:

systemctl stop apache2
Start A Service

The general syntax to start a service is:

systemctl start <servicename>

To start apache2, run:

systemctl start apache2
Restart A Service

The general syntax to restart (stop and then start) a service is:

systemctl restart <servicename>

To restart apache2, run:

systemctl restart apache2
Reload A Service (Reread Configuration Files)

To "reload" a service — which means to make it reread configuration files without restarting — the general systemctl command syntax is:

systemctl reload <servicename>

To reload apache2 run:

systemctl reload apache2
Attempt To Reload A Service, And Fallback To Restarting If It Fails

Not all services support reloading. If you try to reload a service that doesn't support reloading, you may see an error similar to:

systemctl reload error

The systemctl reload-or-restart <servicename> command will:

  1. Try to reload the service. If the reload works, exit.
  2. If the reload fails and the service is running, restart the service.
  3. If the reload fails and the service is NOT running, start the service

To run the reload-or-restart command on apache2 use:

systemctl reload-or-restart apache2

The systemctl try-reload-or-restart <servicename> command is similar, but does not start services that are not running. For try-reload-or-restart, the logic is:

  1. Try to reload the service. If the reload works, exit.
  2. If the reload fails and the service is running, restart the service.
  3. If the reload fails and the service is NOT running, exit without starting the service.

Enable A Service With The Linux Systemctl Command

For a service to automatically start whenever the system starts, it should be enabled. The basic systemctl command syntax for enabling a service is:

systemctl enable <servicename>

For example, to enable apache2:

systemctl enable apache2

Output should look similar to:

systemctl enable apache2

If you want to both enable and start a service at the same time, you can append the --now switch like this:

systemctl enable <servicename> --now

Disable A Service With The Linux Systemctl Command

If a service currently starts when the system starts and you want to disable that behavior, you can use:

systemctl disable <servicename>

For example, to disable apache2:

systemctl disable apache2

Output should look similar to:

systemctl disable apache2

If you want to both disable and stop a service at the same time, you can append the --now switch like this:

systemctl disable <servicename> --now

Masking A Service With The Linux Systemctl Command

Disabled services can still be manually started. Masking a service disables it and disallows a user or process from starting it without first unmasking it.

The general command to mask a service is:

systemctl mask <servicename>

Note that to mask a systemd service, you'll generally need to ensure there is not a corresponding .service file at /etc/systemd/system/. For example, to mask apache2 on our Ubuntu 20.04 system, we can use these commands:

  1. Change the name of (move) the /etc/systemd/system/apache2.service file to /etc/systemd/system/apache2.service.old
mv /etc/systemd/system/apache2.service /etc/systemd/system/apache2.service.old
  1. Mask the service with the Linux systemctl command
systemctl mask apache2

Output should look similar to:

systemctl mask apache2

Unmasking A Service With The Linux Systemctl Command

The general command syntax to unmask a masked service unit is:

systemctl unmask <servicename>

For example, to unmask apache2 run this command:

systemctl unmask apache2

Output should look similar to:

systemctl unmask apache2

Systemd Unit Management

Now, let’s look at commands that are useful for working with Systemd units.

Listing Units With The Linux Systemctl Command

Systemd units are the target of most systemctl commands. Units are plaintext files that describe a resource like a service, socket, device, or filesystem mount point. Systemd services are defined in unit files that end in .service. For example, the sshd's unit file is sshd.service and RSYSLOG's unit file is syslog.service.

To list all the loaded units on a system, run the command systemctl or systemctl list-units (both commands do the same thing).

The output should look similar to:

systemctl list-units

Here is what each of the columns means:

  • UNIT- The name of the systemd unit
  • LOAD- Indicates if the configuration is loaded (configuration parsed by systemd and stored in memory), error, not-found, bad-setting, or masked.
  • ACTIVE- Indicates if the state of the unit. Example states include active, inactive, and failed.
  • SUB- Short for "substates", the SUB value displays more detailed information on the status of the unit. For services, common SUB values include running, stop, and exited. You can view available substates for systemd units with the command systemctl --state=help.
  • DESCRIPTION- A description of the systemd unit.

By default, systemctl and systemctl list-units output only displays units that are loaded. To display all available units, add the --all option.

systemctl list-units --all

You can also use the --state and --type parameters to filter systemctl output.

For example, to view all active and loaded service units use:

systemctl list-units --state=active --type=service

List Dependencies With The Linux Systemctl Command

You can view the other units a service depends on with this general systemctl command syntax:

systemctl list-dependencies <servicename>

For example, to view the apache2's dependencies:

systemctl list-dependencies apache2

Output should look similar to:

systemctl list-dependencies apache2

Displaying Unit Files With The Linux Systemctl Command

The systemctl cat command displays the unit files systemd is actively using. This information can be useful to help determine if changes to a unit file are loaded to the system.

The general syntax for displaying unit files with the systemctl cat command is

systemctl cat <unit name>

For example, to display the unit file for apache2.service, use this command:

systemctl cat apache2

Output should look similar to:

systemctl cat apache2

Edit Unit Files With The Linux Systemctl Command

The Linux systemctl command can edit the unit files that define services. Service unit files use ini-style formatting with sections such as [Unit], [Install], and [Service] that define different unit characteristics and behavior.

To edit an existing service unit file, the general command syntax is:

systemctl edit --full <servicename>

For example, to edit the apache2.service file, run the command:

systemctl edit --full apache2

The unit file will open in your default text editor.

To create a new unit file snippet instead of editing an existing unit file, omit the --full switch, like this:

systemctl edit <servicename>

Saving the new snippet will create an override.conf file in a <servicename.d> directory. Settings in override.conf will supersede settings in the default unit file.

For example, if we run the command systemctl edit apache2 and then save these settings in out snippet:

[Unit]
Description=The Pepper And Egg HTTP Server

An /etc/systemd/system/apache2.service.d/ directory with an override.conf file is created. The override.conf file includes the exact content we saved after using the systemctl edit command. We can view the contents with this command:

cat /etc/systemd/system/apache2.service.d/override.conf

Output should look similar to:

check apache2 override.conf unit file

We can use this systemctl command to reload the apache2 service unit to make it use the new settings:

systemctl reload apache2

To learn more about service unit files, see systemd.unit and systemd.service.

Working With Targets

Systemd targets group units together and define the different states a system can enter when it boots. While there are many differences between the two concepts (see the systemd.special man page for a deep dive on targets), systemd targets are analogous to SysV init system "runlevels".

At a high level, we can map systemd targets and runlevels as follows:

  • poweroff.target maps to runlevel 0 - This target is used for system shutdown.
  • rescue.target maps to runlevel 1 - This target is used to enter single-user mode.
  • multi-user.target maps to runlevels 2-4 This target is used to enter the standard multi-user mode without a graphical interface.
  • graphical.target maps to runlevel 5 - This target is used for multi-user mode with networking and a display manager.
  • reboot.target maps to runlevel 6 - This target is used to reboot the system.

To check the current "runlevel" (default target) on a systemd system, run this command:

systemctl get-default

The output should look similar to:

systemctl get-default

To list all targets, run this command:

systemctl list-units --type target --all

Output should look similar to:

systemctl list-units

To set a target as the default target, the general systemctl command syntaxt is:

systemctl set-default <target_name>.target

For example, to set the default target to multi-user mode, run the command:

systemctl set-default multi-user.target

You should see output similar to:

systemctl set-default multi-user.target

To switch to a specific runlevel (target) immediately, the general command is:

systemctl isolate <target_name>.target

For example, to set the default target to multi-user mode, run the command:

systemctl isolate multi-user.target

Run Systemctl Commands On A Remote host

By default, systemctl commands run on the local server where the commands are executed. To execute systemctl commands on a remote server, use the -H or --host option and specify a username and remote host address in the <username>@<hostname> format.

For example, to run the command systemctl status apache2 as root on remotehost, execute this command:

systemctl status apache2 -H root@remotehost
root@remotehost password:

Output should look similar to:

systemctl status of remote host service

Conclusion

With a solid understanding of how to use the Linux systemctl command, you can efficiently manage services on many modern Linux systems. To learn more about systemd and systemctl, review the systemd man pages.

Mantas is a hands-on growth marketer with expertise in Linux, Ansible, Python, Git, Docker, dbt, PostgreSQL, Power BI, analytics engineering, and technical writing. With more than seven years of experience in a fast-paced Cloud Computing market, Mantas is responsible for creating and implementing data-driven growth marketing strategies concerning PPC, SEO, email, and affiliate marketing initiatives in the company. In addition to business expertise, Mantas also has hands-on experience working with cloud-native and analytics engineering technologies. He is also an expert in authoring topics like Ubuntu, Ansible, Docker, GPU computing, and other DevOps-related technologies. Mantas received his B.Sc. in Psychology from Vilnius University and resides in Siauliai, Lithuania.

Start Building Now

Deploy your new Cloud VPS server in 3 minutes starting from $5.83 / month.

We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: 5bc831c3.737