This tutorial explains systemd units in detail including their states. Learn what the systemd units are and how they are categorized in different types and what is the meaning of different unit states.
What is a systemd unit?
A unit is a systemd object that performs or controls a particular task or action. Systemd uses units to start/stop/manage services, organize boot process, maintain tasks and processes, create sockets, mount file-system and initialize hardware.
A systemd unit consists of a name, type, and configuration file. 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 controllers of the bus such as gear, clutch, lever, brake, accelerator, etc. Just like, to drive and control the bus, the bus driver uses controllers, the systemd uses units to control the system processes and services.
Systemd Unit types
For easier management, units are categorized into several types. There is no specific rule for categorization. To categorize units, a Linux flavor or version may use the same types as other flavor or version use or may use more or fewer types than others.
The following table lists common unit types along with a brief description of each unit type.
Unit type | Description |
Target | A group of units that defines a synchronization point. The synchronization point is used at boot time to start the system in a particular state. |
Service | A unit of this type starts, stops, restarts or reloads a service daemon such as Apache webserver. |
Socket | A unit of this type activates a service when the service receives incoming traffic on a listening socket. |
Device | A unit of this type implements device-based activation such as a device driver. |
Mount | A unit of this type controls the file-system mount point. |
Automount | A unit of this type provides and controls on-demand mounting of file systems. |
Swap | A unit of this type encapsulates/activates/deactivates swap partition. |
Path | A unit of this type monitors files/directories and activates/deactivates a service if the specified file or directory is accessed. |
Timer | A unit of this type activates/deactivates specified service based on a timer or when the set time is elapsed. |
Snapshot | A unit that creates and saves the current state of all running units. This state can be used to restore the system later. |
Slice | A group of units that manages system resources such as CPU, and memory. |
Scope | A unit that organizes and manages foreign processes. |
busname | A unit that controls DBus system. |
- A daemon is a process that starts at boot time, or when a service starts, and runs in the background performing various tasks. A daemon continuously runs until it is stopped manually or the system is shutdown. For convention, daemon program uses a letter "d" at the end of its name.
- A socket is a primary communication channel. A service or process uses it to communicate with other services or processes running in 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 state of the system.
Listing Systemd unit types
To list all available unit types, you can use the "systemctl -t help" command. The following image shows the output of this command on Ubuntu and RHEL system.
To list all available units in a particular type, use the following command.
# systemctl --all list-units | grep .[unit-type]
For example, the following command lists all available units in the type service.
# systemctl --all list-units | grep .service
The following image shows the sample output of this command.
In the output, the first column lists the name of the unit, the second column displays whether the unit configuration was loaded properly or not, the third and fourth columns tell the state of the unit and the last column provides a description of the unit.
The following table briefly explains the terms that the systemd uses to describe the status of units.
Status | Description |
loaded | The unit's configuration file has been successfully read and processed. |
Active (exited) | Successfully executed the one-time configuration and after execution, the unit is neither running an active process nor waiting for an event. |
Active (running) | Successfully executed the one-time configuration and after execution, the unit is running one or more active processes. |
Active (waiting) | Successfully executed the one-time configuration and after execution, the unit is waiting for an event. |
Inactive (dead) | Either the one-time configuration failed to execute or not executed yet. |
You can filter the output to view more specific list of units. For example, to view the list of only active units in the type service, use the following command.
# systemctl --all list-units | grep active | grep .service
Same way, to view the list of all inactive units in the type service, use the following command.
# systemctl --all list-units |grep inactive | grep .service
The following image shows the output of both commands.
By default, without any switch and option, the systemctl command displays the list of all active units. This default behavior makes the list-units option optional. In easy language, to list all active units, you can use any one of the following commands.
#systemctl
Or
#systemctl list-units
The --all switch instructs the systemctl command to include inactive, maintenance, and failed units also in the output.
Listing the status of systemd units at the startup
To list the status of systemd units at startup, we use the following command.
#systemctl list-unit-files
The following image shows the output of this command.
By default, this command lists the status of all units from all unit types. To view the status of all units of a particular unit type, we can filter the output of this command. For example, to view the status of only the type socket, we can use the following command.
#systemctl list-unit-files | grep .socket
The following command displays the output of this command.
As you can see in the above output, a unit can be in three states; enabled, disabled and static.
- If a unit is in the enabled state, the systemd starts it at boot time.
- If a unit is in the disabled state, the systemd does not start it at startup.
- If a unit is in the static state, neither the systemd starts it at startup nor allows us to change its state.
In easy language, if a unit is in the disabled or enabled state, you can change its state from the "systemctl enable/disable" command. But if a unit is in the static state, you can’t change its state. Technically, if a unit is in the static sate, then it means that the unit does not have the "install" section in its configuration file which is required to start a unit at boot time.
To filter the output based on state, you can use the following command.
#systemctl list-unit-files |grep .[unit-type] | grep [state]
The following image shows examples of this command with output.
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.
That’s all for this article. In the next part of this article, we will understand the configuration file of units. If you like this tutorial, please don’t forget to share it with friends.