runlevel
What is a runlevel?
A runlevel is an operating state on a Unix and Unix-based operating system (OS) that is preset on the Linux-based system. Runlevels are numbered from zero to six and play several important roles in the operations, maintenance and management of Linux and Unix systems.
A runlevel is a preset operating state, defined as a single integer that could range from zero to six, in a Unix or Linux-based OS. In those systems, runlevels determine which programs can execute after the OS boots up. Each runlevel defines the state of the machine after boot, denotes a specific system configuration and defines which services are operating on the machine after the OS is up and running.
Runlevels also play an important role in the management of Unix and Linux systems. In general, system administrators (sys admins) can use runlevels to find which subsystems are running, working and not working. In particular, runlevel 1 is used for system maintenance and repair. Runlevels 3 and 5 are used to run the system in multiuser mode with networking -- and check that it is running properly in this mode -- and runlevels 0 and 6 are used to shut down and reboot the system, respectively.
How many runlevels are there?
In Unix and Linux, there are seven runlevels, numbered from zero to six. Each basic level has a different purpose. Runlevels 1 through 6 are generally delegated to single-user mode, multiuser mode with and without network services started, system shutdown and system reboot. The setup of these configurations differs between different Linux distributions and Unix versions.
Runlevels 0, 1 and 6 are always the same. Runlevels 2, 3, 4 and 5 are different depending upon the Linux distribution in use. Only one runlevel is executed when the system is booted. Runlevels are not implemented sequentially. For example, either runlevel 4, 5 or 6 is executed, not 4 then 5 then 6. Runlevels 3 and 5 are similar with one key difference: Runlevel 5 supports a graphical user interface (GUI), whereas runlevel 3 does not.
In addition, each runlevel has its own systemd-target. Runlevels relate to specific systemd-targets. Systemd-targets are methods of starting up Linux-based systems. They are written-out commands that differ from a runlevel command that consists only of a number.
The purpose and systemd-target for the seven runlevels are described in this table.
Importance of runlevels
The system's runlevel can indicate whether its network is operational. Also, booting a system into different runlevels can help sys admins find the solutions to certain problems. For example, booting the system in runlevel 1 (single-user mode) can eliminate problems like the machine failing to boot due to a damaged configuration file or blocking an authorized user from logging in to the system due to a corrupted /etc/passwd file or forgotten password. Overall, runlevels enables sys admins to better control system behaviors and to resolve problems due to undesirable behaviors.
Setting and finding the system runlevel
Sys admins set the default runlevel of a system according to their needs. In most Linux and Unix systems, if an admin doesn't set the default runlevel, the system automatically boots to runlevel 3 or runlevel 5.
In these systems, the init command initializes various processes. To do this, it finds the system's default runlevel and accordingly runs the start scripts corresponding to that runlevel. These scripts may be designed to start the network, initialize system hardware and so on. Users can also modify the preset runlevels or even create new ones if needed. However, these users should have in-depth knowledge of and experience in Linux system administration. Otherwise, they may face problems during system startup or operations.
The start scripts for each runlevel vary and can be found under the respective rc subdirectory under etc/rc.d. These subdirectories are named rc0.d, rc1.d, rc2.d, rc3.d, rc4.d, rc5.d and rc6.d. Thus, the start script for runlevel 1 is in /etc/rc.d/rc1.d, for runlevel 2 in /etc/rc.d/rc2.d and so on.
Administrators also use the runlevel command to find out the machine's current runlevel to assess a system. Use the runlevel command /sbin/runlevel to find the current and previous runlevel of an OS. Another way to show the runlevel is to run the who command: who –r.
Changing the system runlevel
The system runlevel can be changed using the init command. Thus, to start multiuser mode with networking, the runlevel must be runlevel 3. If the system is not already on this runlevel, it can be changed with the following command: sudo init 3.
There's usually no need to reboot the system to switch between runlevels. However, to run the system in GUI mode (runlevel 5), rebooting may be required, along with installation of GUI packages.