Tuesday, July 8, 2014

Managing Systemd Targets (Runlevels)

If you are a Linux geek then you are probably aware of the adoption of systemd in the majority of Linux distros.  One of the differences that we will see is how the “new init” handles running in different modes (mult-user, single-user, graphical , etc). As you probably know, the older init versions such as SysV, used runlevels 0-6 to define the operating system’s mode of operation in which those modes would define which services would be run at the specified mode. Systemd uses targets which are represented by what is called target units that group together other system units through a chain of dependencies to define which services to run. Will be using Fedora 20 machine to demo a few commands on how to manage systemd targets. Also note that the majority of the older init commands still work for now but I would highly suggest learning the systemctl commands because from what I have been reading, all the older commands will slowly go away. 

Systemd’s target units end in a “.target” file extension. So if we wanted to take a look at which mode you are currently running you could execute the following command:


root@localhost ~]# 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
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.

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


This command will tell you the targets name, if it has been loaded, high/low level activation state and a description. 

The default target units for modes of operation are:

poweroff.target - which is used to poweroff the system (Runlevel 0).
rescue.target - used to setup rescue mode (Runlevel 1).
multi-user.target - used as mult-user non-graphical (Runlevels 2,3, and 4).  
graphical.target - graphical multi-user (Runlevel 5).
reboot.target - reboot (Runlevel 6).

If you would like to see what the default target unit is for your system, execute:


    [root@localhost ~]# systemctl get-default 
    graphical.target 


You can also change the default target by using the set-deafult flag:

    [root@localhost ~]# systemctl set-default multi-user.target 
    rm '/etc/systemd/system/default.target'
    ln -s '/usr/lib/systemd/system/multi-user.target'     

    '/etc/systemd/system/default.target'
    [root@localhost ~]# systemctl get-default
    multi-user.target


One thing that I am sure you will have to do at some point in time as a Sys Admin is to switch from one of the target units to another. An example would be in the event a filesystem goes read-only and the system needs to be taken into rescue mode to run an fsck


    [root@localhost ~]# systemctl isolate rescue.target 


In this case "systemctl rescue" would work as well. 

There are tons and tons of material out there on managing targets. If you are looking for more information on systemd, I would suggest checking with your Linux Distros systemd page. Please check back soon for more tutorials on systemd. 

No comments:

Post a Comment