part 4: SystemD - Working with systemd Targets

part 4: SystemD - Working with systemd Targets

4 Working with systemd Targets

SystemD uses targets which serve a similar purpose as runlevels but act a little different. Each target is named instead of numbered and is intended to serve a specific purpose with the possibility of having multiple ones active at the same time. Some targets are implemented by inheriting all of the services of another target and adding additional services to it. There are systemd targets that mimic the common SystemVinit runlevels so you can still switch targets using the familiar telinit RUNLEVEL command.

There are systemd targets that mimic the common SystemVinit runlevels so you can still switch targets using the familiar telinit RUNLEVEL command.

Systemd targets are represented by target units. Target units end with the .target file extension and their only purpose is to group together other systemd units through a chain of dependencies. For example, the graphical.target unit, which is used to start a graphical session, starts system services such as the GNOME Display Manager (gdm.service) or Accounts Service (accounts-daemon.service) and also activates the multi-user.target unit. Similarly, the multi-user.target unit starts other essential system services such as NetworkManager (NetworkManager.service) or D-Bus (dbus.service) and activates another target unit named basic.target.

Comparison of SysV Runlevels with systemd Targets

Runlevel Target Units Description
0 runlevel0.target, poweroff.target Shut down and power off the system.
1 runlevel1.target, rescue.target Set up a rescue shell.
2 runlevel2.target, multi-user.target Set up a non-graphical multi-user system.
3 runlevel3.target, multi-user.target Set up a non-graphical multi-user system.
4 runlevel4.target, multi-user.target Set up a non-graphical multi-user system.
5 runlevel5.target, graphical.target Set up a graphical multi-user system.
6 runlevel6.target, reboot.target Shut down and reboot the system.

Comparison of SysV init Commands with systemctl

Old Command New Command Description
runlevel systemctl list-units --type target Lists currently loaded target units.
telinit runlevel systemctl isolate name.target Changes the current target.

4.1 Viewing the Default Target


To determine which target unit is used by default, run the following command:

~]$ systemctl get-default
graphical.target

This command resolves the symbolic link located at /etc/systemd/system/default.target and displays the result. We can see, that default target unit for our linux system is graphical.target

4.2 Viewing the Current Target


To list all currently loaded target units, type the following command at a shell prompt:

~]$ systemctl list-units --type target

UNIT                  LOAD   ACTIVE SUB    DESCRIPTION                                                                                                                                                                                 
--------------------------------------------------------------------------     
basic.target          loaded active active Basic System                                                                                                                                                                                     
cryptsetup.target     loaded active active Encrypted Volumes                                                                                                                                                                                
getty.target          loaded active active Login Prompts                                                                                                                                                                                    
graphical.target      loaded active active Graphical Interface                                                                                                                                                                              
local-fs-pre.target   loaded active active Local File Systems (Pre)                                                                                                                                                                         
local-fs.target       loaded active active Local File Systems                                                                                                                                                                               
multi-user.target     loaded active active Multi-User System                                                                                                                                                                                
network-online.target loaded active active Network is Online                                                                                                                                                                                
network.target        loaded active active Network                                                                                                                                                                                          
paths.target          loaded active active Paths                                                                                                                                                                                            
remote-fs.target      loaded active active Remote File Systems                                                                                                                                                                              
slices.target         loaded active active Slices                                                                                                                                                                                           
sockets.target        loaded active active Sockets                                                                                                                                                                                          
swap.target           loaded active active Swap                                                                                                                                                                                             
sysinit.target        loaded active active System Initialization                                                                                                                                                                            
time-sync.target      loaded active active System Time Synchronized                                                                                                                                                                         
timers.target         loaded active active Timers                                                                                                                                                                                           

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

17 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

For each target unit, this commands displays its full name (UNIT) followed by a note whether the unit has been loaded (LOAD), its high-level (ACTIVE) and low-level (SUB) unit activation state, and a short description (DESCRIPTION).

4.3 Changing the Default Target


To configure the system to use a graphical.target unit by default, type the following at a shell prompt as root:

~]$ systemctl set-default graphical.target

Removed /etc/systemd/system/default.target.
Created symlink /etc/systemd/system/default.target → /lib/systemd/system/graphical.target.

This command replaces the /etc/systemd/system/default.target file with a symbolic link to /usr/lib/systemd/system/name.target, where name is the name of the target unit you want to use.

4.4 Changing the Current Target


To change to a different target unit in the current session, type the following at a shell prompt as root:

~]$ systemctl isolate name.target

Replace name with the name of the target unit you want to use (for example, multi-user).

This command starts the target unit named name and all dependent units, and immediately stops all others.

4.5 Changing to Rescue Mode


Rescue mode provides a convenient single-user environment and allows you to repair your system in situations when it is unable to complete a regular booting process. In rescue mode, the system attempts to mount all local file systems and start some important system services, but it does not activate network interfaces or allow more users to be logged into the system at the same time.

In Debian based linux distros like Ubuntu or Debian Stretch rescue mode is equivalent to single user mode and requires the root password.

To change the current target and enter rescue mode in the current session, type the following at a shell prompt as root:

~]$ systemctl rescue

This command is similar to systemctl isolate rescue.target, but it also sends an informative message to all users that are currently logged into the system. To prevent systemd from sending this message, run this command with the --no-wall command line option:

~]$ systemctl --no-wall rescue

4.6 Changing to Emergency Mode


Emergency mode provides the most minimal environment possible and allows you to repair your system even in situations when the system is unable to enter rescue mode. In emergency mode, the system mounts the root file system only for reading, does not attempt to mount any other local file systems, does not activate network interfaces, and only starts a few essential services.

In Debian based linux distros like Ubuntu or Debian Stretch emergency mode requires the root password.

To change the current target and enter emergency mode, type the following at a shell prompt as root:

~]$ systemctl emergency

Another parts of this guide: