Run levels in Linux

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

Run levels are modes of operation for computer operating systems. We discuss the different run levels in the Linux OS and demonstrate how to change the default run level.

Table of contents.

  1. Introduction.
  2. Type of run levels.
  3. Init scripts.
  4. Changing run levels.
  5. Summary.
  6. References.

Introduction.

A run level is an operational level describing the state of a system with respect to the services it is currently running. For example, when we boot a Linux OS, we have two options, boot into the graphical user interface or boot into a command-line terminal.

Run levels not only determine the applications to execute when the system boots up but also the state of the system after it is booted.
They are numbered from 0 to 6.

In this article, we discuss various run levels of the Linux OS.

To view the current run level for a system, we write:

$ runlevel

We can also use the who command as follows:

$ who -r

Above, we have number 5 - the default runlevel in most Linux distributions.

Types of run levels.

Each run level from 1 to 6 has its specified function. Run levels 0, 1, 6 behave in the same way while run levels 2, 3, 4, 5 behave differently.

  • 0: Shutting down the system.
  • 1: Operate in single-user mode.
  • 2: Operate in multi-user, no networking mode.
  • 3: Operate in multi-user, with networking mode.
  • 4: Is defined by the user.
  • 5: Operate in multi-user, with networking mode.
  • 6: Reboot or restart the system.

During run level 1 - single-user mode, non-root logins are disallowed and we also don't have networking but can change configuration files, passwords.

Run level 2 doesn't support multiple users, and there are no daemons for networking.

Run level 3 runs the system in the command-line interface and networking is enabled.

Run level 4 allows us to create new run levels. We can also modify the rest of the run levels.

Run level 5 is the default run level for most Linux distributions.

Init scripts.

The init program is responsible for spawning other processes during boot up. It has a PID of 1.

The default state that Linux systems run in is configured in scripts or config files located in the /etc/init.d/ directory:

$ ls /etc/init.d/

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

$ ls /etc/ | grep rc.

Each subdirectory keeps the configuration files for a runlevel.
For example, let's list all configuration scripts under runlevel0:

$ ls /etc/rc0.d/

Changing run levels.

There are two ways in which we can change run levels, either by using the init or the telinit command,

Both commands are used to execute run levels. For example, we can reboot a system as follows:

$ init 6

or

$ telinit 6

Now, let's change the run level. First, we get the current run level using the systemctl command:

$ systemctl get-default

THe graphical.target represents run level 5. Other targets are poweroff.target(rl-0), rescue.target(rl-1), multi-user.target(rl-3), and *reboot.target(rl-6)

We change the run level from the current runlevel to runlevel 3, which supports multi-user - on this run level will be operating in the command-line interface with networking enabled.

$ sudo systemctl isolate multi-user.target

After entering the password, we are taken to a login screen to a command-line interface.

We confirm the run level by writing:

$ who -r

To go back to the graphical interface, we write:

$ sudo systemctl isolate graphical.target

Summary.

Understanding run levels is useful. For example, in fixing a damaged config file, accessing the system, in case of forgotten login password. In this situation, we will boot into a single-user mode.

Run level configurations are different across various Linux distributions.

References.

Execute the commands $ man runlevel or $ runlevel --help.

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.