Linux init detailed explanation

  
 

init is one of the indispensable programs in Linux system operation.

The so-called init process, which is a user-level process started by the kernel.

After the kernel starts itself (has been loaded into memory, starts running, and all device drivers and data structures have been initialized, etc.), the boot process is completed by starting a user-level program init. Therefore, init is always the first process (its process number is always 1).

The kernel will look it up in a few places where init has been used in the past. Its correct location (for Linux systems) is /sbin/init. If the kernel can't find init, it will try to run /bin/sh. If the run fails, the system will fail to start.

First, what is INIT: init is one of the indispensable programs in the operation of Linux system.

The so-called init process, which is a user-level process started by the kernel.

After the kernel starts itself (has been loaded into memory, starts running, and all device drivers and data structures have been initialized, etc.), the boot process is completed by starting a user-level program init. Therefore, init is always the first process (its process number is always 1).

The kernel will look it up in a few places where init has been used in the past. Its correct location (for Linux systems) is /sbin/init. If the kernel can't find init, it will try to run /bin/sh. If the run fails, the system will fail to start.

Second, the run level

So, what is the run level?

To put it simply, the runlevel is the level of functionality currently running on the operating system
. This level ranges from 1 to 6 and has different functions.

Different runlevels are defined as follows: (Refer to /etc/inittab in Red Hat Linux)

# 0 - Downtime (do not set initdefault to 0)

# 1 - Single User Mode

# 2 - Multi-user, no NFS

# 3 - Full Multi-User Mode (Standard Run Level)

# 4 - Not used

# 5 - X11 (xwindow)

# 6 - Restart (do not set initdefault to 6)

These levels are in /etc/inittab Specified in the file. This file is the main file that the init program looks for. The first service to run is the file placed in the /etc/rc.d directory. In most Linux distributions, the startup scripts are located in /etc/rc.d/init.d. These scripts are connected to the /etc/rc.d/rcn.d directory with the ln command. (The n here is the run level 0-6)

Third, the run level configuration

The run level configuration is performed in the /etc/inittab line, as shown below:

12 : 2 : wait : /etc /init.d /rc 2

The first field is an arbitrarily specified label;

The second field indicates that this line applies to Run that level (here is 2);

The third field indicates that when entering the runlevel, init should run the command in the fourth field once, and init should wait for the command to end. The /etc/init.d/rc command runs the start and stop inputs to get any commands needed to run level 2.

The command in the fourth field performs everything when setting the run level. "Miscellaneous". It starts a service that is not already running, terminating a service that should not be run within the new run level. Depending on the version of Linux, the specific commands used are different, and the configuration of the run level is also different.

When init starts, it looks for a line of code in /etc/inittab, which specifies the default runlevel:

id : 2 : initdefault :

You can ask init to enter a non-default runlevel at boot time by specifying a “single” or "emergency" command line argument to the kernel. For example, the specification of kernel command line parameters can be performed by LILO. This way, you can choose single-user mode (ie run level 1).

The telinit command changes the run level while the system is running. When the runlevel changes, init runs the appropriate command from /etc/inittab.

Fourth, special configuration in /etc/inittab

In /etc/inittab, there are several special features that allow init to reactivate special events. These special features are marked with special keywords in the third field. For example:

1. powerwait

Allows init to shut down the system when the power is turned off. The premise is that there is U P S and software that monitors U P S and informs the init that the power has been cut.

2. ctrlaltdel

Allows init to restart the system when the user presses C t r l + A l t + D e l on the console keyboard. Note that if the system is placed in a public place, the system administrator can configure the C t r l + A l t + D e l key combination to perform other actions, such as ignoring.

3. sysinit

The command to be run when the system starts. For example, this command will clear /tmp.

The special keywords listed above are not yet complete. For other keywords and their usage details, please refer to your inittab manual page.

V. Booting in single-user mode

An important runlevel is single-user mode (runlevel 1), in which only one system administrator uses a particular machine, and Run system services as little as possible, including logins. Single-user mode is necessary for a few administrative tasks (such as running fsck on a /usr partition) because it requires unmounting the partition, but this is not possible unless all service systems have been killed.

A running system can enter single-user mode by using init to request run level 1. When the kernel starts, you can enter runlevel 1 by specifying the single or emergency keyword on the kernel command line. The kernel also specifies the command line for init. Init knows from the keyword that it should not use the default runlevel (the kernel command line input method is related to the way you boot the system).

Sometimes it is necessary to start in single-user mode so that users can manually run fsck (besides the scatter file before loading the partition, or at least before loading the scattered /usr partition) Any activity on the system may make it more fragmented, so you should run fsck as much as possible.

If the automated fsck fails at startup, the startup script init will automatically enter single-user mode. This is done to prevent the system from using a coherent file system that f s c k does not automatically fix. Inconsistent file systems are extremely rare and often result in inconsistent or experimental kernel release of the hard drive, but it is best to prevent it before it happens.

Due to security considerations, in single-user mode, a properly configured system will require the user to provide a root password before launching the shell script. Otherwise, it will simply enter the appropriate line of code for LILO and log in as root (of course, if /etc/passwd is not coherent due to file system issues, it is not suitable for this principle, in order to deal with this In the case, you are always ready to prepare a boot disk).

Different run levels have different uses and should be set according to their own situation.

For example, if the root password is lost, you can have the machine boot into a single-user state. At the startup lilo prompt, type:

init=/bin/sh rw Put the machine into run level 1 and hang the root file system as read and write. He skips all system authentication, allowing you to use the passwd program to change the root password and then boot to a new runlevel.

Copyright © Windows knowledge All Rights Reserved