fbpx Linux Runlevels: Changing Execution Level in Terminal | Certificação Linux

Linux Runlevels: Changing Execution Levels with System V and systemd

📅 03/06/2019 ⏱ 6 min ✍️ Uira Ribeiro
Linuxcommandshow-to
Linux Runlevels: Changing Execution Levels with System V and systemd
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

Learn how to change runlevels in Linux using the init commands from System V

Linux has three major special systems for loading services during the boot process and is also responsible for starting all processes: System V Init Daemon, Systemd and Upstart (used in Ubuntu - no longer!).

System V Init Daemon

The System V Init Daemon was inherited from UNIX, which received the name of System V Init Daemon (System Five) or simply init.

Its basic function is to load services during the system boot process, such as network support, multi-user mode, apache, mysql, and other machine services.

During the Linux boot process, the boot system (GRUB) loads the kernel image into memory.

As soon as the Kernel takes control of the machine, it loads a special program called init which always has PID 1.

From there, all other processes running on the machine are children of the init process, which has PID 1.

This is because, when the user executes a program, the kernel makes a copy of init through a method called FORK and loads the machine code of the desired program on top of the machine code of the init clone that was created.

You can see this relationship with the pstree command:

# pstree

Runlevels in Linux

In Linux there are seven predefined runlevels from 0 to 6. The services that each runlevel can execute depend on the Linux distribution and the configuration that the system administrator made.

TABLE - Linux Runlevels

# Description
0 Level zero defines a graceful and fast system shutdown.
1,s Level one is used for system maintenance, also called single-user mode. Only the essentials are executed.
2 Multi-user mode with NFS file sharing disabled.
3 Multi-user mode with all services enabled, but without a graphical interface for logging into the system.
4 Not used.
5 Multi-user mode with all services enabled, X11 graphical interface and graphical login.
6 Level six is similar to level zero, but reboots the machine.

By default, distributions use level 0 for a graceful shutdown sequence and level 6 for reboot. And runlevels 3 through 5 are used for normal system boot.

Level 1, also known as single-user mode, is used for system maintenance, such as, for example, recovering a data partition with problems.

The system administrator can at any time change the runlevel through the init command followed by the desired runlevel number.

# init 5

or

# telinit 6

The telinit command is a link to init.

It is important to know that changing the runlevel can alter the running processes and even shut down the system.

Which Processes Will Be Executed by Each Runlevel?

So far we have learned that the init command can change the running processes by changing the system runlevels.

But the definition of which processes will be executed by each runlevel is made by the /etc/rc.d directory hierarchy and shell scripts.

When Linux starts, a series of scripts in the /etc/rc.d directory is executed to boot the system. Let's look at these files and directories in detail:

The rc.sysinit file is executed by the init process during system boot and enables essential functions. For example, mounting disk partitions;

The rc.local file is executed by the rc.sysinit script. It is used by administrators to modify services during system boot. This file is preferred for this task since it is not changed during a system upgrade;

The rc file is used for transitions between runlevels;

The /etc/rc.d/init.d directory contains the various scripts that load all operating system services.

For example, the SSH secure shell service has a script for loading the service named sshd (Secure Shell Daemon). This script and other scripts can accept commands like start and stop as parameters. Other services can accept other commands like reload, restart, status, etc.

Service levels are defined through the /etc/rc<n>.d directories, where <n> can range from 0 to 6, corresponding to the runlevels.

For example /etc/rc0.d and /etc/rc1.d. Within each runlevel directory there are symbolic links to the service scripts in the /etc/rc.d/init.d files.

These symbolic links follow this pattern:

[K|S]nn[namescript]

Where:

The letter K, for kill, indicates that the script is to terminate a specific service;

The letter S, for start, indicates that the script is to load a specific service;

The integer numbers nn indicate the execution sequence of the scripts, where the lower number is executed first.

If there are services in the same runlevel with the same execution order number, the order will be indeterminate.

The number sequence ensures that a service that depends on another is only executed after the dependency is satisfied;

The name of the script for the symbolic link is not required for correct operation, but to facilitate human reading.

For example:

# /etc/rc.d/rc3.d/S23httpd -> /etc/rc.d/init.d/httpd

To include a certain service in any desired runlevel, you must copy the load script to the /etc/rc.d/init.d directory and create a symbolic link to the script in the desired runlevel following the nomenclature above.

See the example:

# cp /usr/local/mailman/mailman.sh /etc/rc.d/init.d

# ln –s /etc/rc.d/init.d/mailman.sh /etc/rc.d/rc3.d/S25mailman

Defining the Default Runlevel

The file that defines which runlevel the system will assume during boot is /etc/inittab. In this file, look for the line:

id:n:initdefault

Where n will define the runlevel by its number from 0 to 6. Never place runlevel 0 or 6 in this file or the system will never go into production.

You can determine which runlevel the system is running in through the runlevel command.

This command returns the previous runlevel and the current runlevel. If the runlevel has remained unchanged since system boot, the previous level will be shown as the letter N.

The runlevel command does not change the runlevel, but only reports it. To change it, use the init command.

Learn much more about Linux in our online course. You can enroll here with a 7-day free trial. If you already have an account, you can access it here.

Liked it? Share :-)

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp