System startup, inittab, and run levels

May 6, 2021

Comments

Adsense

Adsense

Adsense

Adsense

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 the BIOS, which is situated on the motherboard or main board of our computers. When we power-on, the processor starts executing a program from the BIOS. The program in the BIOS does a power-on-self-test, checking memory and other peripherals. 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 the start_kernel() function, which is a part of kernel source code. Once the start_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 called init. This init process reads the /etc/inittab file. In inittab, the run level information is stored. As per this information, the operating system is initialized by the init process.

The typical /etc/inittab content 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 number 5 after ID specifies that the system should be started in run level 5. It means that the system should be started in X11, 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 the init process with the systemd daemon program, which initializes Linux by starting processes and services in parallel instead of serial execution.

The process ID of the systemd process is always 1, since it is the first process created by the kernel.

systemd reads the file linked by /etc/systemd/system/default.target to 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 the init command.

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 level 1 scripts

3

rc2.d/

The run level 2 scripts

4

rc3.d/

The run level 3 scripts

5

rc4.d/

The run level 4 scripts

6

rc5.d/

The run level 5 scripts

7

rc6.d/

The run level 6 scripts

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 either S or K. When starting the system, the scripts with names starting with S are called one after another. When shutting down, all the script names starting with K are called one after another.

For example, if the system has to be started in run level 5, then initially all the scripts from the rcS.d folder will be called, then all the scripts from rc5.d will be called. Finally, the rc.local script will be called.

The content of /etc/rc.local is 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 the exit 0 line in the preceding rc.local script.

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.

Satish Kumar

Satish Kumar

I am Satish Kumar, Founder of LinuxConcept. Linux and F.O.S.S enthusiast, love to work on open source platform and technologies.

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Related Posts

Backup of files in Linux using command line

In IT or our day-to-day computer industry activities, taking backup is one of the most important activities. Previously, offices were required to keep important paper in a safe place; but if a fire breaks out, then everything is finished. In the digital world, taking...

Using awk in Linux

awk is a program that has its own programming language for performing data-processing and generating reports. The GNU version of awk is gawk. awk processes data, which can be received from a standard input, input file, or as the output of any other command or process....

0 Comments

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

2 × four =

News & Updates

Join Our Newsletter

Adsense

Adsense

Adsense

Adsense