Magazine

System Startup, Inittab, and Run Levels

Posted on the 06 May 2021 by Satish Kumar @satish_kumar86

When we power on the Linux system, shell scripts are run one after another and the Linux system is initialized. These scripts start various services, daemons, databases, and applications, as well as mount discs. Even during the shutting down of the system, certain shell scripts are executed so that important system data and information can be saved to the disk and the applications are properly shut down. These are called boot, startup, and shutdown scripts. These scripts are copied during installation of the Linux operating system in your computer. As a developer or administrator, understanding these scripts may help you in understanding and debugging the Linux system. If required, you can customize these scripts if the need arises.

The kernel startup and init process

In our computers, there is one EPROM chip called theBIOS, which issituatedon the motherboard or main board of our computers. When we power-on, theprocessorstarts executing a program from the BIOS. The program in the BIOS does a power-on-self-test, checking memory andotherperipherals. Then the BIOS program initializes the basic hardware required for PC operation, such as initializing the PCI bus, video devices, and similar.

Finally, the BIOS checks the boot device sequence and queries the first boot device. This BIOS program then reads the master boot record of the first boot device, which is normally a hard disk, USB device, or DVD. Once the BIOS reads the master boot record of the first boot device, then the boot loader is started. The boot loader reads the kernel binary and copies it to the RAM memory. The boot loader checks if the kernel binary is clean and not corrupt. If the integrity check is good then it uncompresses the kernel in the RAM. The bootloader then calls thestart_kernel()function, which is a part of kernel source code. Once thestart_kernel()function is called, the kernel is started.

The kernel then initializes its subsystems, such as process management, filesystem, device drivers, memory management, network management, and similar other modules of the kernel. Then, it mounts the root file system, and the kernel creates the first process calledinit. Thisinitprocess reads the/etc/inittabfile. Ininittab, the run level information is stored. As per this information, the operating system is initialized by theinitprocess.

The typical/etc/inittabcontent will be as follows:

$ cat /etc/inittab

Here is the output:

Output:

# inittab is no longer used when using systemd.## ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.## Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target## systemd uses 'targets' instead of runlevels. By default, there are two main targets:## multi-user.target: analogous to runlevel 3# graphical.target: analogous to runlevel 5## To view current default target, run:# systemctl get-default## To set a default target, run:# systemctl set-default TARGET.target#

In the preceding line, the number5after ID specifies that the system should be started in run level5. It means that the system should be started inX11, such as a graphical user interface. We will study more about run levels in the next section.

Nowadays, various Linux distributions, including CentOS, have replaced theinitprocess with thesystemddaemon program, which initializes Linux by starting processes and services in parallel instead of serial execution.

The process ID of thesystemdprocess is always1, since it is the first process created by the kernel.

systemdreads the file linked by/etc/systemd/system/default.targetto determine the default system target. The default system target is equivalent to the run level. Then, as per the desired run level, system initialization is continued.

Understanding run levels

There are seven run levels. The system will be started in run level 1 to 5. Run level 0 is used for shutting down the system. Run level 6 is used for rebooting the system. The graphical user interface is started in run level 5. The following is the summary of the different run levels:

Sr. No.

Run level number

Description

1

0

Halting the system

2

1

Single-user mode

3

2

Multi-user mode

4

3

Multi-user with network support

5

4

Not used

6

5

Graphical user interface with multi-user and networking support

7

6

Rebooting the system

We need to be in the root-user mode to use theinitcommand.

If we give the following command, then the system will shut down:

# init 0

To reboot the system, use the following command:

# init 6

If the system is running in the command-line mode, and you want to start your server in the graphical user mode, then use the following command:

# init 5

System initialization boot scripts

In the Linux system, the following folders will be present in the /etc/ folder:

Sr. No.

Folder name

Description

1

rc0.d/

The scripts called during shutting down

2

rc1.d/

The run level1scripts

3

rc2.d/

The run level2scripts

4

rc3.d/

The run level3scripts

5

rc4.d/

The run level4scripts

6

rc5.d/

The run level5scripts

7

rc6.d/

The run level6scripts

8

rcS.d/

The scripts called during boot-up, before every run level

9

rc.local

The final script called after run level initialization

Every run level folder will have script names starting with eitherSorK. When starting the system, the scripts with names starting withSare called one after another. When shutting down, all the script names starting withKare called one after another.

For example, if the system has to be started in run level5, then initially all the scripts from thercS.dfolder will be called, then all the scripts fromrc5.dwill be called. Finally, therc.localscript will be called.

The content of/etc/rc.localis as follows:

$ cat /etc/rc.local

Here is the output:

Output:

#!/bin/bash# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES## It is highly advisable to create own systemd services or udev rules# to run scripts during boot instead of using this file.## In contrast to previous versions due to parallel execution during boot# this script will NOT be run after all other services.## Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure# that this script will be executed during boot.touch /var/lock/subsys/localexit 0

We can add our customization commands before theexit 0line in the precedingrc.localscript.

Before any user is logged in, the previously mentioned scripts will be called. After this, user login initialization will be started. This is explained in the following sections.


You Might Also Like :

Back to Featured Articles on Logo Paperblog

These articles might interest you :