After the boot process, Linux System enters final system initialization, where it needs to start various services. A program that performs a particular duty is called a service or daemon. The initialization daemon (init) determines which services are started and in what order and also allows you to stop and manage the various system service. In this guide we’ll look at how to change runlevels and Boot Targets on a Linux System.

There are two initialization daemons;

  • SysVinit: Which uses runlevels at system boot time to determine what groups of services are going to start.
  • Systemd: Which uses target unit files. The primary purpose of target unit files is to group together various services to start at system boot time.

In this guide, we are going to focus on changing runlevels and boot targets on Linux System.

1. Using SysVinit Init System

A service manager based on the SysVinit standard will provide predefined sets of system states, called runlevels, and their corresponding service script files to be executed.

SyVinit Runlevels

Runlevels are numbered 0 to 6:

  • Runlevel 0: Shut down the system.
  • Runlevel 1, s, or S: Single-user mode used for system maintenance. (Similar to systemd rescue target.)
  • Runlevel 2: Multi-user mode without networking services enabled.
  • Runlevel 3: Multi-user mode with networking services enabled.
  • Runlevel 4: Custom.
  • Runlevel 5: Multi-user mode with GUI available.
  • Runlevel 6: Reboot the system

The program responsible for managing runlevels and associated daemons/resources is /sbin/init. During system initialization, the init program identifies the requested runlevel, defined by a kernel parameter or in the /etc/inittab file, and loads the associated scripts listed there for the given runlevel.

Every runlevel may have many associated service files, usually scripts in the /etc/init.d/ directory.

Syntax of the /etc/inittab:

id:runlevels:action:process

id: is a generic name up to four characters in length used to identify the entry.

runlevels: entry is a list of runlevel numbers for which a specified action should be executed. 

action: defines how init will execute the process indicated by the term process. 

The actions are;

boot: The process will be executed during system initialization. The field runlevels is ignored.

bootwait: The process will be executed during system initialization and init will wait until it finishes to continue. The field runlevels is ignored.

sysinit:The process will be executed after system initialization, regardless of runlevel. The field runlevels is ignored.

wait: The process will be executed for the given runlevels and init will wait until it finishes to continue.

respawn: The process will be restarted if it is terminated.

ctrlaltdel: The process will be executed when the init process receives the SIGINT signal, triggered when the key sequence of Ctrl+Alt+Del is pressed.

Changing Runlevels

To check the system’s current and former runlevel, use the runlevel command;

$ runlevel
N 5

In the above output, the first number or letter displayed indicates the previous runlevel (N indicates that the system is newly booted), and the second number (5) indicates the current runlevel.

In SysVinit, when changing runlevels we use two utilities to accomplish the task;

  • init
  • telinit

Type init 3 or telinit 3 to change the runlevel to 3.

$ sudo init 3

Now, use the runlevel command to check the current runlevel;

$ runlevel
5 3

The above output, we find that the current runlevel is 3 while the previous runlevel is 5.

In SysVinit, you can shutdown the system in the command line by typing init 0 or telinit 0 as long as you have the superuser privileges. Also you can reboot the system by typing init 6 or telinit 6.

2. Using Systemd Init System

When a particular hardware component is attached to the system, services can now be started when the system boots. The systemd approach introduced a major paradigm shift in how Linux systems manage services. Currently, systemd is the most widely used set of tools to manage system resources and services, which are referred to as units by systemd.

There are currently 12 different systemd unit types:

  • automount
  • device
  • mount
  • path
  • scope
  • service
  • slice
  • snapshot
  • socket
  • swap
  • target
  • timer

The systemctl utility is the main gateway to managing systemd and system services. Its basic syntax is as follows:

systemctl [OPTIONS…] COMMAND [NAME…]

Systemd Targets

A target unit is a grouping of other units, managed as a single unit.

graphical.target: Provides multiple users access to the system via local terminals and/or through the network. Graphical user interface (GUI) access is offered.
multi-user.target: Provides multiple users access to the system via local terminals and/or through the network. No GUI access is offered.
runleveln.target: Provides backward compatibility to SysVinit systems, where n is set to 1–5 for the desired SysV runlevel equivalence.

To get the default systemd target unit file, employ the following command;

$ systemctl get-default
graphical.target

In the above output, graphical.target is the default system target.

Other useful systemctl utility commands deal with setting and jumping between the system’s target. They are as follows:

  • set-default
  • isolate

To change the default systemd target unit file, use the following command;

$ sudo systemctl set-default multi-user.target
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-unit-files ===
Authentication is required to manage system service or unit files.
 1.  frank_bett,,, (frank)
Password: 
==== AUTHENTICATION COMPLETE ===
Created symlink /etc/systemd/system/default.target → /lib/systemd/system/multi-user.target.

Now, we can check the default systemd target unit file;

$ systemctl get-default
multi-user.target

we obtain that the default systemd target unit file is multi-user.target as we have set to default.

Jumping to a different target unit

The isolate command is handy for jumping between system targets. When this command is used along with a target name for an argument, all services and processes not enabled in the listed target are stopped.

Jumping to a different target unit i.e. multi-user.target;

$ sudo systemctl isolate multi-user.target

Rescue and Emergency targets

Rescue Target

When you jump your system to the rescue target, the system mounts all the local filesystems, only the root user is allowed to log into the system, networking services are turned off, and only a few other services are started.

Jumping to a emergency target unit;

$ sudo systemctl isolate emergency

Emergency Target

When your system goes into emergency mode, the system mounts only the root filesystem, and it mounts it as read-only. When you run systemctl is-system-running command it will return the maintenance status.

$ systemctl is-system-running
maintenance

Stopping the System

Besides the various SysVinit and systemd commands you can use to shut down or reboot a system.

There are a few additional utilities you can employ to enact these tasks:

  • halt: Stops all processes and shuts down the CPU.
  • poweroff: Stops all processes, shuts down the CPU, and sends signals to the hardware to power down.
  • reboot: Stops all processes, shuts down the CPU, and then restarts the system.
  • shutdown: Stops all processes, shuts down the CPU, and sends signals to the hardware to power down.

On most modern systemd initialization systems, the halt, poweroff, and reboot commands are symbolically linked to the systemctl utility. For example;

  • sudo systemctl halt: Will halt the system.
  • sudo systemctl poweroff: Will poweroff the system.
  • sudo systemctl reboot: Will reboot the system.

The utility with the most flexibility in rebooting or powering off your system is the shutdown command.

Syntax:

shutdown [option] time [message]

The [OPTIONS] include switches;

  • halt the system (-H)
  • power off the system (-P),
  • reboot the system (-r),

Use option -c to cancel the shutdown i.e shutdown -c

The time parameter include the following;

  • hh:mm: This format specifies the execution time as hour and minutes.
  • +m: This format specifies how many minutes to wait before execution.
  • now or +0: This format determines immediate execution.

The [message] parameter lets you modify the shutdown command message sent to
any logged-in users using wall command.

Conclusion

This marks the end of our guide on Changing Runlevels and Boot Targets on Linux System. Stay tuned for our next LPIC 101 guides.

You can also check;

LPIC 101 – Managing Shared Libraries in Linux Systems

LPIC 101 – Managing Processes in Linux Systems

LPIC 101 – Managing Software Packages on Linux