systemd targets

Controlling Targets - runlevels with systemd

What is Systemd?

systemd is a replacement to the older traditional "System V init" system . systemd stands for system daemon. systemd was designed to allow for better handling of dependencies and have the ability to handle more work in parallel at system startup. systemd supports snapshotting of your system and the restoring of your systems state, keeps track of processes stored in what is known as a "cgroup" as opposed to the conventional "PID" method. systemd is now shipping by default with many popular Linux distributions such as Fedora, Mandriva, Mageia, Arch Linux, CentOS 7, RHEL 7.0 (Red Hat Enterprise Linux) and Oracle Linux 7.0. systemd refers to runlevels as targets.

In the following examples, we will show you how to display and work with different runlevels (targets). The system used to demonstrate the following commands is a RHEL 7.0 Standard Desktop configuration.

Controlling Runlevels

To display the current runlevel of your system, you will need to issue the following command: systemctl -get-default

[root@rhel07a ~]# systemctl get-default

The reply back from the system is "". Basically the runlevel "" is the equivalent to the traditional runlevel 5, Full user access with Graphical Display and networking.

You can display the new runlevels/targets by issuing the following command:

ls -al /lib/systemd/system/runlevel*

[root@rhel07a /]# ls -al /lib/systemd/system/runlevel*
lrwxrwxrwx. 1 root root 15 Apr 25 10:31 /lib/systemd/system/ ->
lrwxrwxrwx. 1 root root 13 Apr 25 10:31 /lib/systemd/system/ ->
lrwxrwxrwx. 1 root root 17 Apr 25 10:31 /lib/systemd/system/ ->
lrwxrwxrwx. 1 root root 17 Apr 25 10:31 /lib/systemd/system/ ->
lrwxrwxrwx. 1 root root 17 Apr 25 10:31 /lib/systemd/system/ ->
lrwxrwxrwx. 1 root root 16 Apr 25 10:31 /lib/systemd/system/ ->
lrwxrwxrwx. 1 root root 13 Apr 25 10:31 /lib/systemd/system/ ->

From the above we can see that we still have seven different runlevels ranging from system poweroff to system reboot.

Runlevel Systemd Description

Traditionally the default runlevel was contained within the "/etc/inittab" file and could be displayed with the following command:
cat /etc/inittab | grep initdefault. This would typically report back with an entry similar to: id:5:initdefault:.

Now if you try to display the "/etc/inittab" file on a system using systemd, you will see a message similar to the following:

[root@rhel07a /]# cat /etc/inittab
# inittab is no longer used when using systemd.
# Ctrl-Alt-Delete is handled by /etc/systemd/system/
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
# analogous to runlevel 3
# analogous to runlevel 5
# To set a default target, run:
# ln -sf /lib/systemd/system/<target name>.target /etc/systemd/system/

Setting a new Default Runlevel

In the following example we are going to change the runlevel from "" to "". (Runlevel 5 to Runlevel 3).

To do this we simply issue the following commands:

rm /etc/systemd/system/
ln -s /lib/systemd/system/ /etc/systemd/system/

Alternatively you could issue the link command with the "-f" parameter indicating that the destination file is to be removed:

ln -sf /lib/systemd/system/ /etc/systemd/system/

Here we are first deleting the existing "" and then replacing with our link command. Our new "target.default" will be that of "".

[root@rhel07a /]# rm /etc/systemd/system/
rm: remove symbolic link ‘/etc/systemd/system/’? y
[root@rhel07a /]# ln -s  /lib/systemd/system/  /etc/systemd/system/
[root@rhel07a /]# systemctl get-default

Now if we were to reboot the system, it would start in "runlevel 3 -".
To revert back to the original runlevel "runlevel 5 -" we would simply issue the following commands:

[root@rhel07a ~]# systemctl get-default
[root@rhel07a ~]# rm /etc/systemd/system/
rm: remove symbolic link ‘/etc/systemd/system/’? y
[root@rhel07a ~]# ln -s  /lib/systemd/system/  /etc/systemd/system/
[root@rhel07a ~]# systemctl get-default

For the system to switch to the new runlevel, you would need to reboot your system or issue the "init" command followed by the relevant runlevel.