What is Runlevel in Linux?

Learn via video courses
Topics Covered

Overview

Run level in linux, which controls the operating state of a system, is a crucial component of Linux operating systems. They are essential for controlling how your Linux system behaves at certain points in its functioning. We'll go into detail about run levels in this article, including what they are, how they operate, how to monitor and adjust them, and other pertinent details.

What are Run Levels in Linux?

For each task in Linux, a distinct mode called runlevel is used. Runlevels come in seven different varieties and are numbered from 0 to 6. Runlevels 0, 1, and 6 in Linux each have a particular function. However, you can carry out any purpose determined by the distribution provider at runlevels 2, 3, and 4. The system's runlevel is determined by the /etc/inittab file. As an admin, you can manually change between runlevels.

The following are some of the primary justifications for runlevel definition:

  • figuring out the system's boot configuration, such as whether a GUI is present.
  • describing the program or application that the system will need to run when it boots.

LINUX Kernel Supports These Seven Different Runlevels

Seven distinct run levels, each geared to particular tasks and conditions, are commonly defined by Linux systems. The system's operation in single-user mode, multi-user mode with networking, or even complete shutdown is determined by these run levels.

The seven regular run levels in linux are as follows:

Level 0 (Single-user Mode):

Rescue mode, also referred to as single-user mode, is a run level in linux that has limited functionality and is frequently used for system upkeep and troubleshooting. Only critical services are started in this mode.

Level 1 (Multi-user Mode without Networking):

Without network capabilities, such as internet connectivity, the system is set to handle numerous users in the multi-user mode.

Level 2 (Multi-user Mode with Networking):

For the majority of Linux systems, this is the default run level in linux. Along with multi-user services, networking services are started.

Level 3 (Unused):

Run level 3 is usually not used and is left up to the user's decision.

Level 4 (Graphical Multi-user Mode):

It is multi-user mode with a graphical user interface (GUI). Also referred to as full multi-user mode, run level 4 places the system in a GUI environment where many users can sign in and utilize graphical tools to interact with the system.

Level 5 (Reboot):

The system is rebooted using this run level in linux.

Level 6 (Shutdown):

Run level 6 is used to shut down the system during shutdown. All services are stopped, and the system is turned down.

How to Check Runlevel of Our Linux System?

Using the runlevel command, you may determine your Linux system's current run level in linux. Activate a terminal and type:

Suppose the output of the above code is N 5. The first number in the report that follows denotes the previous runlevel that was active on your system. Its current runlevel is represented by the second number. Your system was not previously at a different runlevel, as indicated by the letter N in this case. And 5 indicates that the current runlevel of your system is 5.

Switching Between Runlevels

In Linux, switching between run levels entails altering your system's operational state, which has an impact on the running services and processes. You can modify the behavior of your system to fit a variety of scenarios because each run level corresponds to a particular configuration of services. The following describes how to change run levels:

Using the "init" or "telinit" Command

Get a terminal open:

You must utilize the terminal on your Linux system to change run levels.

Verify Current Run Level:

It's a good idea to verify the current run level before switching run levels. To do this, type the following command onto your keyboard:

The previous and current run levels will be shown.

Change Run Level:

The init or telinit command must be used in combination with the appropriate run level number to change the run level in linux. For instance, to enter the multi-user mode without a graphical user interface at run level 3, type:

Substitute the required run level number for 3 here. To make sure you have the rights necessary to modify run levels, use the sudo command.

Confirm the Switch:

Your system will begin the process of switching to the new run level after you enter the command. Depending on the service, this can entail initiating or halting it. Your terminal will show a notification stating that the run level has changed once the transition is finished.

Check for Change:

You can use the runlevel command once more to confirm that the run level in linux has successfully been changed:

The modified run level should now appear in the output.

Runlevel Files Location

The /etc directory houses the directories for each runlevel. Each runlevel uses a unique set of scripts to execute its environment. We'll look at where these scripts are kept next.

The /etc directory contains a lot of configuration files and subdirectories. These directories include runlevel files that are arranged from rc0.d to rc6.d. Each subdirectory is intended to house the configuration files for a particular runlevel. The subdirectories are shown by the following command:

runlevel files location in linux

There is also a list of the processes that are active for each runlevel. For instance, runlevel 4 displays a list of every active process. Let's examine the documents in the /etc/rc4.d directory that houses the runlevel 4 files:

examine the documents in the /etc/rc4.d directory

Changing the Runlevel

In Linux, switching the run level in linux entails moving your system between various operational states, each of which is associated with a different collection of services and processes. Depending on your Linux distribution and the init system it employs (SysV init or systemd), changing the run level can involve a different procedure.

Changing Run Level in SysV Init

  • Excute runlevel command.
  • The init or telinit command must be used in combination with the appropriate run level number to change the run level. For instance, use sudo init 3 to enter run level 3 (multi-user mode without a graphical interface). Substitute the required run level number for 3 here.
  • Your system will start the process of changing to the new run level when you enter the command. Depending on the service, this can entail initiating or halting it. Your terminal will show a notification stating that the run level has changed once the transition is finished.

FAQs

Q. How can I switch run levels in Linux?

A. To switch run levels in Linux, you can use either the init or telinit command for SysV init systems or the systemctl command for systems that use systemd. You specify the desired run level, and the system transitions accordingly.

Q. How do run levels relate to systemd targets?

A. In systemd-based systems, run levels are often referred to as targets. These targets determine the state of the system and services. They provide more granularity and flexibility compared to traditional run levels.

Q. Are run levels the same across all Linux distributions?

A. The concept of run levels is consistent across Linux distributions, but the implementation may vary. Some distributions may use the traditional SysV init system, while others adopt systemd or other init systems.

Conclusion

The following is a summary of important Linux run-level information:

  • Different operational states of a Linux system are defined by run levels, each of which has a unique configuration of services and processes.
  • A range of operating levels, from single-user mode for maintenance to graphical multi-user mode for everyday use, accommodate to different needs.
  • To change between run levels, services must be started or stopped by the desired configuration.
  • Linux normally offers seven standard run levels, including single-user, multi-user with networking, multi-user without networking, graphical multi-user, reboot, and shutdown.
  • Run level changes are made via the init, telinit, and systemctl commands, each of which has a unique syntax based on the init system.
  • While the idea of run levels is the same across all distributions, the specifics of implementation may change depending on the init system used.
  • Operations for controlled system shutdown and reboot are triggered by run levels 0 and 6.