Systemd Units Explained with Types and States

A unit is a systemd object that performs or controls a particular task or action. Systemd uses units to manage services, organize the boot process, maintain tasks and processes, create sockets, mount file-system and initialize hardware.

A systemd unit consists of a name, configuration file, and type. The name provides a unique identity to the unit. The configuration file defines the responsibility or task of the unit. The type helps the unit to group with other similar types of units.

To understand the relationship between systemd and units, consider systemd as a bus driver and the units as the bus controllers, such as gear, clutch, lever, brake, accelerator, etc. The bus driver uses controllers to drive and control the bus. Similarly, the systemd uses units to control the system processes and services.

Systemd Unit types

The following table lists standard unit types and their descriptions.

Unit Type Description
Target It is a group of units that defines a synchronization point. Linux uses the synchronization point to start the system in a particular state at the boot time.
Service A service unit starts, stops, restarts or reloads a service daemon such as Apache webserver.
Socket A socket unit activates a service when the service receives incoming traffic on a listening socket.
Device A device unit implements device-based activation, such as a device driver.
Mount A mount unit controls the mount point of the file system.
Automount An automount unit provides and controls on-demand mounting of file systems.
Swap A swap unit activates and deactivates the swap partition.
Path A path unit monitors files and directories. It activates and deactivates a service if the specified file or directory is accessed.
Timer A timer unit activates and deactivates the specified service based on a timer or when the set time has elapsed.
Snapshot A snapshot unit creates and saves the current state of all running units that we can use to restore the system later.
Slice It is a group of units. It manages system resources such as CPU and memory.
Scope A scope unit organizes and manages foreign processes.
Busname A bus unit controls the DBus system.
  • A daemon is a process that starts at the boot time or when a service starts and performs various tasks in the background. It continuously runs until we manually stop it or shut down the system. It uses the letter d at the end of its name for naming convention.
  • A socket is a primary communication channel. A service or process uses it to communicate with other services or processes running on the local or remote system.
  • A service refers to one or more daemons. Starting and stopping a service makes a one-time change to the system's state.

This tutorial is part of the tutorial series Systemd service in Linux. Other parts of this series are the following.

Chapter 01  Differences between SysVinit, Upstart and Systemd
Chapter 03  Systemd Unit Configuration Files Explained
Chapter 04  Systemd Target Units Explained
Chapter 05  How to use the systemctl command to manage Systemd services

Listing Systemd unit types

The following command lists all available unit types.

#systemctl -t help

listing all systemd units

The following command lists all available units of a particular type.

# systemctl --all list-units | grep .[unit-type]

For example, the following command lists all service units.

# systemctl --all list-units | grep .service

listing service units

In the output, the first column lists the unit's name, the second column displays whether the unit configuration was correctly loaded or not, the third and fourth columns tell the state of the unit, and the last column describes the unit.

The following table briefly explains the terms the Systemd uses to describe the state of units.

State Description
loaded The unit's configuration file has been successfully read and processed.
Active (exited) Successfully executed the one-time configuration. After execution, the unit is neither running an active process nor waiting for an event.
Active (running) Successfully executed the one-time configuration. After execution, the unit is running one or more active processes.
Active (waiting) Successfully executed the one-time configuration. After execution, the unit is waiting for an event.
Inactive (dead) Either the one-time configuration failed to execute or has not been executed yet.

You can filter the output to view a list of particular unit types. For example, the following command displays a list of only active units in the service type.

# systemctl --all list-units | grep active | grep .service

Similarly, use the following command to view a list of all inactive units in the service type.

# systemctl --all list-units |grep inactive | grep .service

listing system active and inactive units

The --all option instructs the systemctl command to include inactive, under maintenance, and failed units in the output.

Without any option, the systemctl command lists all active units. It is similar to the list-units option. You can use any one of the following commands to list all active units.

#systemctl
#systemctl list-units

Listing the status of systemd units at the startup

The following command lists the state of systemd units at startup.

#systemctl list-unit-files

listing boot time enabled and disabled units

By default, this command lists the state of all units from all types. We can filter the output to view the state of a particular unit type. For example, we can use the following command to view the state of socket units only.

#systemctl list-unit-files | grep .socket

listing boot time enabled units

A unit can be in three states: enabled, disabled and static.

enabled Systemd starts it at the boot time.
disabled Systemd does not start it at the boot time.
static Systemd does not start it at the boot time. It also does not allow us to change its state.

If a unit is in the enabled or disabled state, we can change its state using the systemctl command. However, if it is in the static state, we can not change the state. A unit in the static state does not have the install section in its configuration file required to start it at boot time.

The following command filters the output based on state.

#systemctl list-unit-files |grep .[unit-type] | grep [state]

listing units based on state

The difference between the systemctl or systemctl list-units commands and the systemctl list-unit-files command is that the first two commands give a run-time snapshot of units while the later command displays the status of units at startup.

ComputerNetworkingNotes Linux Tutorials Systemd Units Explained with Types and States

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com