What are run levels in Linux

What are runlevels in Linux?

When a Linux system is booted, its default setting runlevel is used and the start scripts assigned to this runlevel are executed. You can also switch between runlevels. For example, there is a runlevel that is intended for recovery and maintenance operations.

Traditionally Linux System V-style init scripts - while new init systems may obviate traditional runlevels, they haven't done so yet. For example, Ubuntu's Upstart system still uses traditional System V style scripts.

What is a runlevel?

When a Linux system boots, it starts the inside processes. init is responsible for starting the other processes on the system. For example, when you start your Linux computer, the kernel starts init and init runs the startup scripts to initialize your hardware, access the network and start your graphical desktop.

However, there is not just one set of startup scripts init will run. There are several run levels with their own start scripts. For example, one runlevel can activate the network and start the graphical desktop, while another runlevel leaves the network deactivated and skips the graphical desktop. This means that with a single command you can switch from graphical desktop mode to text console mode without a network, without having to manually start and stop various services.

In particular, init executes the scripts it finds in a specific directory that corresponds to the runlevel. For example, if you enter runlevel 3 on Ubuntu, init will run the scripts located in the /etc/rc3.d directory.

At least this will work with a traditional System V init system - Linux distributions are starting to replace the old System V init system. While Ubuntus Upstart currently maintains compatibility with SysV init scripts, this is likely to change in the future.

The runlevels

Some runlevels are standard on Linux distributions, while some runlevels vary from distribution to distribution.

The following runlevels are standard:

  • 0 - Stop (Shuts down the system.)
  • 1 - Single user mode (The system starts in superuser mode without starting daemons or networks. Ideal for starting in a recovery or diagnostic environment.)
  • 6 - Reboot

Runlevels 2-5 vary depending on the distribution. For example, under Ubuntu and Debian, runlevels 2-5 are identical and offer a full multi-user mode with network and graphical login. Under Fedora and Red Hat, runlevel 2 offers multi-user mode without network (console login only), runlevel 3 offers multi-user mode with network (console login only), runlevel 4 is not used, and runlevel 5 offers multi-user mode with networking and graphical login.

Change to another runlevel

Use the following command to switch to another runlevel while the system is already running:

sudo telinit #

Replace # with the number of the runlevel you want to switch to. Omit sudo and run the command as root if you're running a distribution that doesn't use sudo.

Direct boot to a specific runlevel

You can select a runlevel for booting via the bootloader, e.g. B. Grub. At the beginning of the startup process, press a key to access Grub, select your startup entry and press e to edit it.

You can add single until the end of the Linux Line for entering the single-user runlevel (runlevel 1). (After that, press Ctrl + x to boot.) This is the same as the Recovery Mode option in Grub.

Traditionally, you could specify a number as a kernel parameter and boot to that runlevel - for example with 3 Instead of single boot to runlevel 3. However, this doesn't seem to work on the latest versions of Ubuntu - Upstart doesn't seem to allow this to happen. How you change the default runlevel also depends on your distribution.

While Ubuntu's Upstart daemon is still emulating the SystemV init system, much of this information will change in the future. For example, Upstart is event-based - it can stop and start services when events occur (for example, a service can start when a hardware device is connected to the system and stop when the device is removed.) Fedora also has its own successor too init, systemd.