Understanding RunLevels (init) and Targets (systemd)

Understanding RunLevels (init) and Targets (systemd)

🐧Exploring System States: RunLevels and systemd Targets

Β·

6 min read

🐧 What is Runlevels ?

So you'd be thinking, What is System Run Level ?

Runlevel is simply a current operating state of system or numbered states that will define the system's operational mode at any given time.

Runlevels          Description
     0                    Halt (shutdown the system)
     1                    Single User Mode (used for maintenance)
     2                    Multi-User Mode without Networking
     3                    Multi-User Mode with Networking, no GUI
     4                    User-definable (often unused)
     5                    Multi-User Mode with Networking and GUI
     6                    Used when Restarting the System

By Default system will boot either with Multi User mode with Networking File system including GUI or without GUI.

🐧 Why Runlevels ?

Now, the question is Why Runlevels are required ?

  • System Resource Management-

Not everyone needs GUI if you are working in infrastructure rather than CLI is preferred.

Runlevels define different states or configurations in which a system can operate to optimize resources which are limited.

  • System Maintenance or Troubleshooting-

What if server is experiencing performance issue and you have to isolate the cause.

Here switching to rescue mode (runlevel 1/S) starts the system with minimal services for administrative tasks, like without starting network services or other non-essential daemons., allowing admin to troubleshoot server performance issue effectively by isolating causes without interference, before restoring normal operation.

  • Service Control-

What if any server needs to be configured as per need to act as a DB server or Webserver or both.

Runlevels classify system states (like, single-user mode, multi-user with GUI), ensuring which services start or stop during boot. This will ensures only essential services are active and optimizing resources enhancing security based on operations.

  • Others-

Startup control, which requires the database to be started before the webserver, and flexibility, which requires the development server to swiftly move between configs.

We may also alleviate the use of user-defined runlevels for different needs, allowing us to change the system's behavior and allocate resources as needed.

🐧 What is Target ?

Systemd targets are units representing specific operational states or groups of services.

Rather we can say it provides the similar functionality with advance features similar to like Windows 8 and Windows11.

So lets first figure out now in modern system which runlevel is mapped to which target,

Below we can see runlevel0.target specifies System Power Off

[ajeet@centos2 system]$ pwd
/usr/lib/systemd/system
[ajeet@centos2 system]$ cat runlevel0.target
[Unit]
Description=System Power Off
Documentation=man:systemd.special(7)
DefaultDependencies=no
Requires=systemd-poweroff.service
After=systemd-poweroff.service
AllowIsolate=yes
JobTimeoutSec=30min
JobTimeoutAction=poweroff-force

[Install]
Alias=ctrl-alt-del.target

Further to list which runlevel is mapped to which modern target, refer below,

[ajeet@centos2 ~]$ ls -l /usr/lib/systemd/system/runlevel?.target
lrwxrwxrwx. 1 root root 15 May 22 13:33 /usr/lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 May 22 13:33 /usr/lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 May 22 13:33 /usr/lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 May 22 13:33 /usr/lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 May 22 13:33 /usr/lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 May 22 13:33 /usr/lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 May 22 13:33 /usr/lib/systemd/system/runlevel6.target -> reboot.target
πŸ”— To list all targets present

To see all the targets present we can use the below command that will list all the active and inactive targets present on system.

systemctl list-units --type=target -all

Now, it seems the picture is little more clearer why we need targets instead of runlevels.

🐧 Why Targets ?

Above we can see multiple types of targets we can use as per requirement rather than runlevel which is providing only 7 options.

Systemd targets replace traditional runlevels by offering enhanced flexibility, parallel service startup, and dynamic state changes, accommodating modern technologies like containerization and cloud computing also.

πŸ”—Some of the commonly used target,
  • multi-user.target: similar to runlevel 3

  • graphical.target: Starts (GUI) with a display manager, similar to runlevel 5

  • network.target: Ensures network configuration and connectivity.

  • rescue.target: similar to runlevel 1

  • emergency.target: Emergency shell for critical system repairs (extremely limited services).

  • reboot.target: Initiates system reboot, similar to runlevel 6

  • halt.target: Powers off the system.

  • getty.target: Starts login prompts on virtual consoles for text-based logins.

  • httpd.target/nginx.target: Starts web server (Apache or Nginx, depending on your system).

πŸ”— Man Page for sytemd.special

When i mentioned command cat runlevel0.target earlier if you have noticed their is documentation mentioned in output we can definitely always look into systemd.special manual page for more references.

[ajeet@centos2 ~]$ man systemd.special | less

🐧 Some Practicals

Now lets move ahead see some practicals,

πŸ”— How to Manage it ?

Previously in older systems we can manage it with the inittab config but now that is depreciated as seen below.

[ajeet@centos2 ~]$ cat /etc/inittab
# inittab is no longer used.
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
# To view current default target, run:
# systemctl get-default
# To set a default target, run:
# systemctl set-default TARGET.target

Now rather its defined as targets, as you see in output of systemctl get-default ,

[ajeet@centos2 ~]$ who -r
         run-level 5  2024-07-16 10:28                   last=3
[ajeet@centos2 ~]$ systemctl get-default
graphical.target
πŸ”— Switching Targets

Let's switch from graphical.target to multi-user.target and vice versa, as we can see what the output is after switching tagets.

πŸ”— Setting Default Target

We can change the default target also,

[ajeet@centos2 ~]$ systemctl get-default
graphical.target
[ajeet@centos2 ~]$ systemctl set-default multi-user.target
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-unit-files ====
Authentication is required to manage system service or unit files.
Authenticating as: root
Password:
==== AUTHENTICATION COMPLETE ====
Removed "/etc/systemd/system/default.target".
Created symlink /etc/systemd/system/default.target β†’ /usr/lib/systemd/system/multi-user.target.
==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon ====
Authentication is required to reload the systemd state.
Authenticating as: root
Password:
==== AUTHENTICATION COMPLETE ====
[ajeet@centos2 ~]$ systemctl get-default
multi-user.target

🐧 Runlevels vs Targets

DescriptionRunlevels (init)Targets (systemd)
Management File/etc/inittabNot Applicable
Commandstelinit, runlevelsystemctl
FlexibilityLimited (7 predefined states)High (customizable)
State RepresentationNumbers (0-6)Descriptive Names
Example Commands (Check Current State)runlevelsystemctl get-default
Example Commands (Switch State)telinit 3systemctl isolate multi-user.target
Reboot Required (for changes)YesUsually Not

🐧Conclusion

In short, Targets have replaced traditional RunLevels by offering better flexibility, service control, and dependency management, with descriptive names, faster boot, and better resource optimization, making it ideal for modern computing needs like cloud deployments and containerization.

🐧Thank you for joining me on this journey through runlevels and targets. I truly appreciate it. Keep exploring, stay curious, and happy coding!

Β