Systemd Target Units Explained

This tutorial explains Systemd target units in detail. Learn what the systemd target units are and how they are used to boot a Linux system into different states or run-levels.

Systemd manages all services and processes in Linux. To control and manage services and processes, it uses units. A unit represents and manages an individual service or process. A target-unit is the logical collection of other system units.

System state

A system state is an environment in which all the required services and processes run to build and provide the necessary functionality. Depending on the program which controls system services, system states may vary. For example, to define the various system states, the SysVinit uses numbers, known as run-levels, while the Systemd uses keywords, known as target-units.

The SysVinit is a classic program that was used to manage the Linux system for several years. It has been replaced by the systemd. The first part of this article explains the differences between both programs in detail.

This tutorial is the fourth part of the article "Systemd in Linux Explained with Examples". Other parts of this article are the following.

Differences between SysVinit, Upstart and Systemd

Systemd Units Explained with Types and States

Systemd Unit Configuration Files Explained

Run-levels v/s Target-units

Advertisements

In SysVinit, functionalities (services, processes, etc.) were organized in seven run-levels. Each run-level was associated with a level of functionality. In systemd, target-units provide the same functionality which the run-levels used to provide in the SysVinit.

To provide backward compatibility with the old SysVinit run-levels, the systemd uses symbolic links. It creates a symbolic link for each run-level and maps it with the equivalent target unit.

To view which run-level is mapped with which target-unit, you can use the following command.

# ls -l /usr/lib/systemd/system/runlevel?.target

The following image shows the output of this command.

run-levels vs traget-units

The following table provides a brief description of all seven run-levels with equivalent target-units in the systemd program.

Run-level in the classic SysVinit programDescriptionEquivalent target-unit in the systemd program
0 Shutdown all services and power off the system poweroff.target
1 Single user mode for maintenance and recovery. Only the root account is allowed to log in. Networking is not available in this mode. rescue.target
2 Multiuser mode without networking. multi-user.target
3 Multiuser mode with networking. All users can login and access CLI prompt. No Graphical user interface is available. multi-user.target
4 User-defined mode. Users can customize this run-level. Both command line and GUI (graphic user interface) are available for customization. multi-user.target
5 X11 GUI with networking. All users can login into the system graphical.target
6 Shutdown all services and reboot the system. reboot.target

To fulfill modern requirements and best utilize available hardware resources, besides the above-listed units, the systemd also provides a lot of service-specific target-units. These units inherit all services from other target-units and add their own to them.

These service specific target-units are highly used in deploying a dedicated system. A dedicated system is a system that runs only a few services and makes all resources available for them.

To view the list of all available target-units with their status and a brief description, you can use the following command.

#systemctl list-units --type=target --all

The following image shows the output of this command.

listing all target units

The following table lists the most common target-units, used in a regular system.

Target unit name Description
halt Shut down all services and halt the system
poweroff Shut down all services and power off the system
shutdown Shut down the system normally
rescue Single-user (root) mode for maintenance and recovery functions. It mounts all file-system but does not start any networking related service or function.
multi-user Multi-user command line mode for regular tasks. It starts all essential and custom services and provides the CLI prompt for login and work.
graphical Same as the multi-user.target but also includes GUI. A user can use the graphical desktop environment to login and work or can use regular command line interface.
reboot Reboot the system normally.
default Default target to boot the system. Usually set either to multi-user.target or graphical.target.
emergency Start an emergency shell and mount only root file-system. Other file-system and networking remain disabled.
hibernate Save the running state of the system to the hard disk and power off the system. When the system is power on again, the systemd restores the saved state.
suspend Same as the hibernate except the system state is saved in the memory and power to the memory is not turned off.

Switching between different targets

To switch the system into another target or to change the current target into another target, use the following command.

#systemctl isolate [target]

For example, the following command switches the system into the multi-user.target.

# systemctl isolate multiuser.target

To switch back into the graphical target, use the following command.

# systemctl isolate graphical.target

Viewing and changing the default boot target

To view the default boot target, use the following command.

# systemctl get-default

To change the default boot target, use the following command.

# systemctl set-default [target]

For example, the following command changes the current default boot target to multi-user.target.

# systemctl set-default multiuser.target

viewing and setting default target units

That’s all for this part. In the next part of this tutorial, I will explain how to use targets to start, restart, and hibernate the system. If you like this tutorial, please don’t forget to share it with friends.

Advertisements

ComputerNetworkingNotes Linux Tutorials Systemd Target Units Explained