Systemd Quickstart: Services, Units, and the Journal

Updated on November 21, 2023
Systemd Quickstart: Services, Units, and the Journal header image

Introduction

Systemd is a very popular init system used by most Linux distributions. It replaced the older SysVinit system. In this article, you will get a basic idea of systemd and its ecosystem, and you will learn to perform the most common tasks using systemd.

What Is Systemd

Systemd is a Linux initialization system that starts all the other Linux processes to bring the system to a usable state. It manages the system state and processes. It is not a binary file, but a collection of programs and libraries that serve different functions. A few systemd components:

  • journald - system logging
  • logind - session management
  • networkd - network configuration management

Unit Files in Systemd

Systemd operates on units. These units have unit files. Unit file stores important data about the unit which systemd requires. There are many types of unit files, some of which are:

  • .service (defines processes)
  • .device (defines hardware and devices)
  • .socket (defines inter-process communication sockets).

Systemd provides systemctl command to manage services and processes.

Basic Unit Management

You can start, stop, restart, or reload a unit using the systemctl command.

Note: This guide uses nginx.service unit to show command usage. Install nginx using your package manager if you want to use the commands in this guide.

  1. Start a unit:

     # systemctl start nginx.service
  2. Stop a unit:

     # systemctl stop nginx.service
  3. Restart a service:

     # systemctl restart nginx.service
  4. Reload the configuration file of a unit without stopping it if the unit is capable of doing so:

     # systemctl reload nginx.service
  5. reload-or-restart reloads the unit if possible, otherwise restarts it:

     # systemctl reload-or-restart nginx.service

Note: While using unit files, specify the name of the file along with the file type extension. For example, if you are specifying a socket file, use socket_name.socket in the command. If you skip the file extension, systemd will treat the unit file as a .service file.

Enabling and Disabling a Unit

If you want a unit to start at boot, use enable command. To do the opposite, use the disable command.

# systemctl enable nginx.service
# systemctl disable nginx.service

Note: This command does not start or stop the service in the current session. To do so, use the start and stop commands.

Masking a Unit

Making a unit prevents any user to start and enable it. Use the mask command to mask a unit.

# systemctl mask nginx.service

Note: Like the enable command, mask will not stop a currently active unit. To stop the unit in the current session, use the stop command.

Checking the Status of a Unit

Checking the status of a unit is essential to managing your system. status command lists the current status and some other details of the unit.

$ systemctl status nginx.service

Sample output:

  ● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2022-07-01 17:01:57 UTC; 2 months 18 days ago
     Docs: man:nginx(8)
 Main PID: 745 (nginx)
    Tasks: 2 (limit: 1081)
   Memory: 8.2M
   CGroup: /system.slice/nginx.service
           ├─   745 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─719388 nginx: worker process

This output lists some important details about the unit, like:

  • PID
  • Current state of the unit
  • If the unit is "enabled" or "disabled" at system boot.

If all you want to check is whether the unit is currently running or not, use the is-active command.

$ systemctl is-active nginx.service

This will either return active or inactive depending on the current state of the unit.

Looking at System State

All the active (currently running) units define the state of a system. Systemd has commands that provide information about the state of your system.

Listing Active Units

To see all the active units, use:

$ systemctl list-units

Note: You can use the systemctl command without the list-units and you will get the same output.

To see a list of unit files that systemd loaded or attempted to load, append the --all flag.

$ systemctl list-units --all

Note: This command excludes those units which systemd never attempted to load.

Listing All Units

To see a full list of installed unit files on your system, use:

$ systemctl list-all-units

Viewing Basic Log Information

The systemd suite of applications includes a journald service that collects and stores different types of log data from different parts of the system, like:

  • Standard output and standard error from service units
  • Kernel log messages from the kernel
  • Audit records from kernel audit subsystem
  • Other system log messages.

To see all the log entries stored by journald, including the ones generated in the previous boot sessions, use:

$ journalctl

Note: Not all systems store log data from previous boot sessions. To change this behavior, edit the /etc/systemd/journald.conf file and set the value of Storage option as:

  • persistent to store logs from previous boot sessions.
  • auto to disable this persistent storage of logs.
  • none to completely disable logging of any data.

To view log data from the current boot session, append the journalctl command with -b flag.

$ journalctl -b

To view kernel logs, use the -k flag.

$ journalctl -k

To view logs of a unit, use the -u flag along with the unit's name.

$ journalctl -u nginx.service

To view the output in reverse order, use the -r flag.

$ journalctl -u nginx.service -r

This will display the output in reverse order, showing the latest logs first. Also, you can use multiple flags together. For example, you can use the -r flag and -b flag together to list the logs generated in the current boot session in reverse order.

$ journalctl -u nginx.service -b -r

Inspecting Unit Files

Unit files contain information about the unit that systemd uses to run and manage the unit. You can use the cat command to view the contents of a unit file.

$ systemctl cat nginx.service

To see all the properties and details of a unit that are specific to your system, use show command.

$ systemctl show nginx.service

Some unit files require other unit files to be in the active state before they can run. To see a list of dependency units of a unit file, use:

$ systemctl list-dependencies nginx.service

Some dependency units may depend on other units. To see a recursive list of dependency units along with their dependency units, use the -all flag:

$ systemctl list-dependencies -all nginx.service

Modifying Unit Files

To modify a unit file, use the edit command.

# systemctl edit nginx.service

This creates a new directory nginx.service.d (directory naming format: unit.type.d) in the /etc/systemd/system/ directory and creates a .conf file (unit snippet file) in this directory. This file adds new properties and overrides the existing properties of the original unit file. To undo the modifications made on a unit file, use the revert command.

# systemctl revert unit.type

Targets in Systemd

Systemd groups multiple units which serve a common goal into a Target file. For example, graphical.target groups all the units required to initialize the graphical user interface. One target can depend on other units. Calling a target file brings the system to a specific state.

Listing available Target files

To get a list of all the available target files on your system, use

$ systemctl list-unit-files --type=target

Note: This guide uses basic.target target file to show command usage.

Listing Dependencies of a Target

To see all the dependencies of a target, use:

$ systemctl list-dependencies basic.target

Default target file

The default target file is the first target file that systemd calls during the system boot process. To know the default target of your system use:

$ systemctl get-default

Changing default target

To change the default target of your system, use:

# systemctl set-default multi-user.target

Stopping and rebooting the server

Systemctl is powerful enough to change the major states of a server, like the power state of your system. Some useful commands are

  • sudo systemctl poweroff - Power off the server
  • sudo systemctl reboot - Reboot the server
  • sudo systemctl suspend - Suspend the system
  • sudo systemctl rescue - boot into rescue mode