What You Need to Know About Run Levels in Linux
Tags: devops, linux, runlevels
Published: 2023-04-24 20:59:41


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4o6593tmuiflaptgfsu2.png)

Graphical user interface (GUI) and command-line interface (CLI). Oh, snap! I realize I’ve just blurted out those terms without context, let me add context. The GUI and CLI are the two options a Linux operating system (OS) boots to when turned on. When the system finishes booting up, the applications it executes are determined by Run levels. Run levels also determine the state of the system after booting.

In essence, run levels in Linux can be considered as the different modes the OS runs in. These modes/run levels have their list of processes or the services they provide and these are either turned on or off. As you continue to work with your computer, and depending on the kind of services the OS is accessing, the run level may change.

For instance, if you are running your Linux machine with a GUI, the run level for this is different from when you are running the CLI on your system. The reason for this is simple. The GUI will need access to services that the command line does not need.
_

In this article, we will talk about run levels in Linux, the different types of run levels, changing run levels in a system, list services that are enabled at a run level, and some Init scripts.

**Table of Contents**

- What is Run Level?
- Linux System Boot Process
- Types of Run Levels
- Changing Run Levels
- Services That Are Enabled at a Run Level
- init scripts
- Examples of Run Level Commands
- Security Issues That Affect Run Levels in Linux
- Conclusion
- Resources

---

## What is Run Level?

A run level is an operating state of init on a Unix or Linux-based operating system. It determines what system services are operating and which programs can be executed after the OS boots up. In essence, the state of the machine after boot is defined by the run level.

System administrators use run levels to define system-related issues like which subsystems are working if the network is operational if a command/file/program is running etc. The run level command 

```
/sbin/runlevel
```

can be used to find out the current or previous run level of the OS. These run levels are usually identified by numbers (0–6). Each of these levels controls a system configuration different from the other levels and they allow access to the combination of different processes.

To view the current run level for a system, you can use the command:

```
$ runlevel
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lryxve49g5araceocwu8.png)

The command below can also be used:

```
$ who -r
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x5gl28e5zkcflzjkpppn.png)

## Linux System Boot Process

When a Linux system boots up, the first thing that happens is that the init process starts. To execute the start scripts that are appropriate for the default run level, the init first determines the system’s default run level. It is this process that is responsible for the initialization of the hardware, bringing up the network, and starting the GUI.

## Types of Run Levels

There are seven run levels in Linux and they are numbered from zero to six. According to the operating system or Linux distribution, these run levels may be used differently. As a result, it is not very easy to come up with a clearly defined list of the run levels and what they do. You should check the specific distribution that you use, and how the run levels work on it.

However, the standard LINUX kernel supports the seven different run levels listed below:

- Run Level 0 — In this run level, the system can safely be shut down

- Run Level 1 — This is a single-user mode and it is used for maintenance or administrative activities. This mode can also be referred to as run level S(S represents single-user).

- Run Level 2 — This is a multi-user mode and it has no Network File System (NFS).

- Run Level 3 — This is a multi-user mode under the command line interface. It is the run level you use if your system doesn’t boot into a graphical user interface.

- Run Level 4 — This run level is user-definable. What this means is that the user can customize the level to fit their purposes.

- Run Level 5 — This run level works the same as run level 3, but it also starts a display manager. This is the standard run level for most Linux-based systems that boot up into a GUI.

- Run Level 6 — This run level reboots the system.

It is important to note from the above listed that each run level has a different purpose.

- Run levels 0,1 and 6 are the same.

- Depending on the Linux distribution being used, Run levels 2–5 are different

- When the system is booted, only one level executes

- Finally, run levels are executed in a sequence. This means that either levels 2 or 3 or 4 are executed, and not 2 then 3 then 4.

## Changing Run Levels

Using the init or telinit command, we can alter the run level. The telinit command can be used to invoke the init program, which is used to change the run level.

For instance, to reboot a system, you can use the:

```
$ init 6
```
or

```
$ telinit 6
```
To change the run level, we first have to get the current run level by using the systemctl command:

```
$ systemctl get-default
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4usqk1xvio9gkg7mbzi9.png)

Graphical.target represents run level 5. Other targets include

poweroff.target (run level 0), rescue.target (run level 1), multi-user.target (run level 3), and *reboot.target (run level 6).

Now we can change the run level from the current level it is at to run level 3. This run level supports multi-user and on it, we can operate in the CLI with networking enabled.

```
$ sudo systemctl isolate multi-user.target
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/edhbzkc3ndcqe1iej817.png)

We are directed to a login screen on a command-line interface after entering the password.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/017o69j0mdum7167rohe.png)

We can confirm the run level by using the command:

```
$ who -r
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ij4gtr9q60dv0y25252i.png)

To go back to the graphical interface, we can use the command:

```
$ sudo systemctl isolate graphical.target
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q0oudih8i0xppc2gn1sy.png)

**Note**: It is important to note that the changing of run levels is a task for the super user. This is why for the successful execution of the changing run levels process above, it is important to be logged in as the super user. Or you can use the sudo command.

## Services That Are Enabled at a Run Level

Until recently, to list the services that would be enabled at different run levels, “chkconfig — — list” was the command used. However, if your OS is up to date, using that command may give you an error or push you over to systemd.

Now if we want to see what services will be enabled when we boot our system into the run level 5 (graphical mode), we can use this command:

```
$ systemctl list-dependencies graphical.target
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/14ioemn16yhjlxmh0xpi.png)

We can replace “graphical.target” with the name of a target service we want to see to see the services that run by default on other run levels.

## init scripts

init is the program that is responsible for launching other processes during the boot-up of a system. Linux system’s default state is configured in scripts of config files and these are located in the /etc/init.d/ directory:

```
$ ls /etc/init.d/
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5pqablzl8vgdtwd3t44n.png)

Run levels are found in the /etc/ directory, and each run level has its directory here:

```
$ ls /etc/ | grep rc
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2x704twr19qbg6dnc9l6.png)

In each subdirectory, configuration files for a run level are kept. For example, to list all the configuration scripts under run level 0, we can use

```
$ ls /etc/rc0.d/
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pnni3a008j65ibtlwgdu.png)

## Examples of Run Level Commands

1. To see the current run level use the:
```
$ runlevel
```
2. To suppress the output:
```
$ runlevel -q
$ runlevel - quiet
```
3. To run in verbose mode:

```
$ runlevel -v
$ runlevel - verbose
```
4. To get the version info:

```
$ runlevel - version
```
5. To get help:

```
$ runlevel - help
```
6. To check the runlevel manual:

```
$ man runlevel
```

## Security Issues That Affect Run Levels in Linux

The main point of run levels in Linux is to give the system administrator granular control over the system. Such control improves security because the administrator is then sure that no other services are running without permission. A very important practice when using run levels is to only use the run level you need. This means that there is no point in starting the graphical interface (run level 5) when you only want to use the terminal. Doing this — having different run levels on — introduces multiple services, some of which you may forget to secure because they run in the background.

## Conclusion

It is important to understand run levels and how they work. Knowledge of this is useful especially when accessing the system, fixing a damaged config file, or in the case of a forgotten password. In any of these situations, we will boot into a single-user mode. And as mentioned earlier in the article, run level configurations vary across the different Linux distributions.

## Resources

You may find the following resources helpful:

- [Run Levels](https://www.techtarget.com/searchdatacenter/definition/runlevel)

- [Linux System Boot Process](https://www.freecodecamp.org/news/the-linux-booting-process-6-steps-described-in-detail/)

- [Changing Run Levels](https://www.tecmint.com/change-runlevels-targets-in-systemd/)

- [Run Levels Security Configuration](https://security.stackexchange.com/questions/150564/linux-runlevel-configuration-security)