RHEL/CentOS 7 : The SystemD Service and Setting Runlevels

0
60

The systemd service doesn’t use runlevels as SysV or Upstart do. The alternatives for systemd are called targets. Their purpose is to group a set of systemd units (not only services, but also sockets, devices, and so on) through a chain of dependencies.

Managing targets with systemd is pretty simple, as shown through the following steps:

  1. List all target units, as follows:
    ~]# systemctl list-unit-files --type target
    UNIT FILE                 STATE   
    anaconda.target           static  
    basic.target              static  
    bluetooth.target          static  
    cryptsetup.target         static  
    ctrl-alt-del.target       disabled
    default.target            enabled
    ...
    
    sysinit.target            static  
    system-update.target      static  
    time-sync.target          static  
    timers.target             static  
    umount.target             static  
    
    58 unit files listed.
    ~]#
    

    This list shows all target units available followed by information regarding whether the target is enabled or not.

  2. Now, show the currently loaded target units.The systemd targets can be chained unlike SysV runlevels, so you’ll not only see one target but a whole bunch of them, as follows:
    ~]# 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
    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
    nfs-client.target     loaded active active NFS client services
    paths.target          loaded active active Paths
    remote-fs-pre.target  loaded active active Remote File Systems (Pre)
    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.
    
    18 loaded units listed. Pass --all to see loaded but inactive units, too.
    To show all installed unit files use 'systemctl list-unit-files'.
    ~]#
    
  3. Next, change the default systemd target by running the following commands:
    ~]# systemctl set-default graphical.target
    rm '/etc/systemd/system/default.target'
    ln -s '/usr/lib/systemd/system/graphical.target' '/etc/systemd/system/default.target'
    ~]#
    

Sometimes, you want to change targets on the fly as you would in the past with runlevel or telinit. With systemd, this is accomplished in the following way:

~]# systemctl isolate <target name>

Here’s an example:

~]# systemctl isolate graphical.target

Let’s take an overview of the former runlevels versus the systemd targets in the following table:

Runlevel Target units Description
0 runlevel0.target or poweroff.target This is used to shut down and power off the system
1 runlevel1.target or rescue.target This is used to enter a rescue shell
2 runlevel2.target or multi-user.target This is used to set up a command-line multiuser system
3 runlevel3.target or multi-user.target This is used to set up a command-line multiuser system
4 runlevel4.target or multi-user.target This is used to set up a command-line multiuser system
5 runlevel5.target or graphical.target This is used to set up a graphical multiuser system
6 runlevel6.target or reboot.target

This is used to reboot the system

LEAVE A REPLY

Please enter your comment!
Please enter your name here