Category: RHEL7

Systemd – Targets

Command demonstrated in this article is tested on RHEL 7 but it can work on CentOS / Fedora / Amazon Linux / Ubuntu and other Linux distros as well. This is the third part of series of post that I would be covering entire Systemd and its sub-systems.

Part 1 What is Systemd
Part 2 Systemd – Services
Part 3 Systemd – Targets (Current post)

Targets are used for grouping and ordering units. They are somewhat of a rough equivalent to runlevels in that at different targets different services, sockets, and other units are started. Unlike runlevels they are much more free-form and you can easily make your own targets for ordering units, and targets have dependencies among themselves.

Systemd targets are represented by target units. Target units end with the .target file extension and their only purpose is to group together with 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.

Use the following command to check the new runlevels/targets:-

# ls -al /lib/systemd/system/runlevel*
lrwxrwxrwx. 1 root root 15 Sep  8 22:11 /lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 Sep  8 22:11 /lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 Sep  8 22:11 /lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Sep  8 22:11 /lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Sep  8 22:11 /lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 Sep  8 22:11 /lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 Sep  8 22:11 /lib/systemd/system/runlevel6.target -> reboot.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.

To view current default target, use following command:-

# systemctl get-default 
graphical.target

To set up default target, use following command:-

# systemctl set-default graphical.target
rm '/etc/systemd/system/default.target'
ln -s '/usr/lib/systemd/system/graphical.target' '/etc/systemd/system/default.target'

To list all active currently loaded target units, use following command:-

# 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.target      loaded active active Network
nfs.target          loaded active active Network File System Server
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
sound.target        loaded active active Sound Card
swap.target         loaded active active Swap
sysinit.target      loaded active active System Initialization
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'.

To list all active/inactive currently loaded target units, use following command:-

# systemctl list-units --type target --all
UNIT                   LOAD   ACTIVE   SUB    DESCRIPTION
basic.target           loaded active   active Basic System
cryptsetup.target      loaded active   active Encrypted Volumes
emergency.target       loaded inactive dead   Emergency Mode
final.target           loaded inactive dead   Final Step
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 inactive dead   Network is Online
network.target         loaded active   active Network
nfs.target             loaded active   active Network File System Server
nss-lookup.target      loaded inactive dead   Host and Network Name Lookups
nss-user-lookup.target loaded inactive dead   User and Group Name Lookups
paths.target           loaded active   active Paths
remote-fs-pre.target   loaded inactive dead   Remote File Systems (Pre)
remote-fs.target       loaded active   active Remote File Systems
rescue.target          loaded inactive dead   Rescue Mode
shutdown.target        loaded inactive dead   Shutdown
slices.target          loaded active   active Slices
sockets.target         loaded active   active Sockets
sound.target           loaded active   active Sound Card
swap.target            loaded active   active Swap
sysinit.target         loaded active   active System Initialization
syslog.target          not-found inactive dead   syslog.target
time-sync.target       loaded inactive dead   System Time Synchronized
timers.target          loaded active   active Timers
umount.target          loaded inactive dead   Unmount All Filesystems

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.

28 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.


Changing run level/target at runtime can be achieved using “isolate” option, eg:-

# systemctl isolate graphical.target


Above command changes the run level/target to graphical mode.

# systemctl isolate rescue.target


Above command switch the system to rescue mode.

# systemctl halt


Above command halt the system.

# systemctl poweroff


Above command initiate a full shutdown.

# systemctl reboot


Above command reboot the system.

Below table list out the comparative commands between old sysVinit and new systemctl features that would really help in day to day operation.

sysVinit command systemd command Comment
telinit 3 systemctl isolate multi-user.target Move system into another target (change runlevels.)
No such Command systemctl show -p “Wants” multi-user.target Lists units pulled in by a given target.
No such Command systemctl show -p “After” sshd.service Shows dependent services and other targets.
No such Command systemd-analyze plot > boot.svg Generates a diagnostically useful graphical representation of the boot process.
ps xawf -eo pid,user,cgroup,args systemd-cgls Display control group process tree.
Advertisements