Skip to content

Changing Runlevels on Linux with System V

Learn how to change execution levels - Runlevels - on Linux using the systemv init commands

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 Upstream (used in Ubuntu - not anymore!).

System V Init Daemon

The System V Init Daemon was inherited from UNIX, which was named System V init Daemon (System Five) or simply init.

Its basic function is to load the services during the process of system load, such as network support, multi-user, Apache, MySQL, and other machine services.

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

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

From there on, all other processes executed 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 init using a method called FORK and load 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:

# Pastry

Runlevels on Linux

On Linux, there are seven predefined execution levels from 0 to 6. Os Services that each runlevel can execute will depend on the Linux distribution and of the configuration that the system administrator made.

TABLE - Linux Runlevels

\# Description
0 Level zero defines an elegant shutdown and fast of the system.
1, s Level one is used to maintain the system, also called single-user. Only the essential thing is executed.
2 Multi-user mode with sharing NFS files disabled.
3 Modo multi-user with all services enabled, but without a graphical interface to log in to the system.
4 Not used.
5 Modo multi-user with all services enabled, X11 graphical interface and login in graphic mode.
6 Level six is similar with level zero, but reboot the machine.
By default, distributions use level 0 for a sequence elegant shutdown and level 6 for the reboot. What about runlevels 3 5 are used for normal system load.

Level 1 also known as single-user is used for system maintenance, such as recover a problem data partition.

The system administrator can change the level at any time executed using 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 execution level may change the processes running and even shutting down the system.

What Processes Will Be Executed by Each Runlevel?

So far we’ve learned that the init command can alter running processes by changing the system’s runlevels.

But the definition of which processes will be executed by each execution level 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 load the system. Let’s look at these files and directories in detail:

The rc.sysinit file is executed by the init process during system load and enables functions essentials. For example, mount disk partitions;

The rc.local file is executed by the rc.sysinit script. He is used by administrators to modify services under the load of system. This file has a preference for this task since it is not changed during a system upgrade process;

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 called sshd (Secure Shell Daemon). This script and other scripts can accept commands such as start and stop as parameters. Other services may accept other commands such as reload, restart, status, etc.

Service levels are defined through directories /etc/rc .d, where it can vary from 0 to 6, corresponding to runlevels.

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

These symbolic links follow the following pattern:

[K|S] nn [scriptname]

Where:

The letter K, for kill, indicates that the script is for terminating a particular service;

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

The integer numbers nn indicate the sequence in which the scripts are executed, where the lowest number is executed first.

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

The sequence of numbers ensures that one service that depends on another is only executed after the dependency is satisfied;

The name of the script for the link symbolic is not necessary for proper functioning, but to facilitate human reading.

For example:

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

For include a particular service in any desired runlevel, you must copy the Upload script to the /etc/rc.d/init.d directory and create a symbolic link for the script at 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 Standard Runlevel

The file that defines what runlevel the system will assume during the load it 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 Put runlevel 0 or 6 in this file otherwise the system will never enter production.

You can determine at which runlevel the system is running using the runlevel command.

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

The runlevel command does not change the level of execution, but only informs. To change use the init command.

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

Did you like it? Share :-)