1. What is INIT?
Init is an indispensable program in Linux. Is a user-level process started by the kernel.
After the kernel is started (it has been loaded into the memory, started to run, and initialized to all the device drivers and data structures, start a user-level program init to start other user-level processes or services. Therefore, init is always the first process (its PID is always 1 ).
In the past, the kernel used init to find it. The correct position (for Linux) is/sbin/init. if the kernel cannot find init, it will try to run/bin/sh. If the run fails, the system will also fail to start.
Ii. Operation Level
Runlevel 0 enables init to shut down all processes and terminate the system.
Runlevel 1 is used to switch the system to single-user mode. in single-user mode, only system administrators can access the system. In this mode, files that cannot be changed when a user is logged on can be processed, you can change runlevel number 1 to S.
Runlevel 2 allows the system to access multiple users, but does not support file sharing. This mode is rarely used.
Runlevel 3 is the most common running mode. It is mainly used to provide real multi-user mode and is also the default mode for most servers.
Runlevel 4 is generally not used by the system. You can design your own system status and apply it to runlevel 4. Although it is rarely used, you can use this system to implement some specific login requests.
Runlevel 5 is a dedicated X Window terminal that initializes the system. This is not a good choice for a powerful Linux system. However, if you need this, you can enable this solution at runlevel.
Runlevel 6 is used to shut down all running processes and restart the system.
These levels are specified in the/etc/inittab file. This file is the main file found by the INIT program. The first service to run is the file stored in the/etc/rc. d directory. In most Linux releases, the startup script is located in/etc/rc. d/init. d. These scripts are connected to the/etc/rc. d/rcN. d directory by using LN commands. (Here N is the running level 0-6)
3. Run-level Configuration
All rows starting with # In the inittab file are comment rows. The comment line helps you understand the inittab file. The values in the inittab file are in the following format:
Label: runlevel: Action: Process
1, label is 1 ~ A four-character tag that identifies the input value. Some systems only support 2 Characters of tags. For this reason, most people limit the number of tag characters to less than 2. The tag can be a string composed of any characters, but in fact, some specific labels are commonly used. The labels used in Red Hat Linux are:
ID is used to define the default init running level
Si is the process of system initialization.
In LN, N ranges from 1 ~ 6. indicates the runlevel level that can be used by the process.
UD is the upgrading process
CA indicates the process that runs when CTRL + ALT + DEL is pressed.
PF indicates the process that runs when the ups instance is powered off.
PR is the process to run when the ups sends a signal of power recovery before the system is actually shut down.
X is the process to run when the system is transferred to the X terminal.
2. The runlevel field specifies the runlevel level. You can specify multiple runlevel levels, or do not specify a specific value for the runlevel field. (In addition, the sysinit, boot, and bootwait processes ignore this setting value .)
3. Action: indicates how init should run the process field command when it enters the corresponding runlevels. The valid action values are as follows:
Respawn: indicates that init should monitor this process, even after which it should be restarted.
Wait: init should run the process once and wait until it is finished before proceeding to the next step.
Once: init needs to run this process once.
Boot: run as the system starts, so the runlevel value is invalid.
Bootwait: run as the system starts, and init should wait until it ends.
Off: it makes no sense.
Initdefault: the default running level after the system is started. As processes at the corresponding running level are activated, the process field specified by the system is meaningless. If this record does not exist in the inittab file, you can query the running level on the console when the system starts.
Sysinit: the command to be run when the system starts. For example, this command will clear/tmp. You can view the/etc/rc. d/rc. sysinit script to understand the operations it has run.
Powerwait: Allows init to shut down the system when the power supply is disconnected. Of course, the premise is that u p S and monitoring u p S and notifying init that the power supply has been cut off. This option is not listed by default in RH Linux.
Powerfail: Same as powerwait, but init does not wait until the running process ends. This option is not listed by default in RH Linux.
Powerokwait: The operation that init performs when the power monitoring software reports "power recovery.
Powerfailnow: If the UPS power supply is about to run out, the init operation is different from powerwait/powerfail.
Ctrlaltdel: Allows init to restart the system when you press CTRL + ALT + DEL on the console keyboard. Note: If the system is deployed in a public place, the system administrator can configure the CTRL + ALT + DEL combination key as another action, such as ignore. I set it to print out a swear word ^ o ^. kbrequest: the action taken when the specified keyboard combination key is pressed, which is not complete yet.
OnDemand: A process marked with an OnDemand runlevel will be executed whenever the specified OnDemand runlevel is called. however, no runlevel change will occur (OnDemand runlevels are 'A', 'B', and 'C. Shame !)
4. The process field contains the process executed by init. The process adopts the same format as running the process under the command line. Therefore, the Process field starts with the name of the process, it is followed by the parameter to be passed to the process during running. For example,/sbin/shutdown-T3-R now, the process is executed when CTRL + ALT + DEL is pressed. You can also enter it in the command line to restart the system.
Iv. Example
Take a closer look at the example file and learn how to apply the syntax format of inittab. Most of the content of this file can be ignored, because more than half of the content is annotated, and the remaining content is mainly used to implement some special functions:
The value of ID indicates that the default runlevel is 3.
The UD value can wake up the/sbin/update process. To ensure the integrity of the disk, the whole I/O buffer is cleared before I/O operations are performed on the disk.
The PF, PR, and Ca values are called only for specific interruptions.
If the system is a dedicated X terminal, only the input value of X is required.
The getty process provides services for virtual terminal devices. For example:
3: 2345: respawn:/sbin/mingetty tty3
The value of the tag field is 3, which is the digital Suffix of the device tty3. tty3 is associated with the corresponding process. The runlevel that the Getty process can start is 2, 3, 4, and 5, when the process is terminated, init immediately restarts it. The name of the startup process is/sbin/mingetty, which is the smallest version of Getty supported by the virtual terminal and provides the process for tty3 to start the virtual device.
Si: sysinit:/etc/rc. d/rc. sysinit
This value tells the INIT program to run/etc/rc. d/RC. the sysinit script file is used to initialize the system. The script file is similar to all started scripts. It is only an executable file containing Linux shell commands, note that the entered string must include the full path of the script. Different Versions of Linux store this script in different locations, but you don't have to remember these locations. You only need to view the/etc/inittab file, this file contains the exact location of the startup script file.
A complete inittab File
Reference content :#
# Inittab this file describes how the INIT process shocould set up
# The system in a certain run-level.
#
# Author: Miquel van smoorenburg <miquels@drinkel.nl.mugnet.org>
# Modified for RHS Linux by Marc Ewing and Donnie Barnes
#
# Default runlevel. The runlevels used by RHS are:
#0-halt (do not set initdefault to this)
#1-Single User Mode
#2-multiuser, without NFS (the same as 3, if you do not have networking)
#3-full multiuser Mode
#4-unused
#5-X11
#6-Reboot (do not set initdefault to this)
#
ID: 3: initdefault:
# System initialization.
Si: sysinit:/etc/rc. d/rc. sysinit
L0: 0: Wait:/etc/rc. d/RC 0
L1: 1: Wait:/etc/rc. d/RC 1
L2: 2: Wait:/etc/rc. d/RC 2
L3: 3: Wait:/etc/rc. d/RC 3
L4: 4: Wait:/etc/rc. d/RC 4
L5: 5: Wait:/etc/rc. d/RC 5
L6: 6: Wait:/etc/rc. d/RC 6
# Things to run in every runlevel
UD: Once:/sbin/update
# Trap CTRL-ALT-DELETE
CA: ctrlaltdel:/sbin/shutdown-T3-R now
# When our ups tells us power has failed, assume we have a few minutes
# Of power left. Schedule a shutdown for 2 minutes from now.
# This does, of course, assume you have powerd installed and your
# Ups connected and working correctly.
PF: powerfail:/sbin/shutdown-f-h + 2 "power failure; system shutting down"
# If power was restored before the shutdown kicked in, cancel it.
PR: 12345: powerokwait:/sbin/shutdown-c "power restored; shutdown cancelled"
# If power was restored before the shutdown kicked in, cancel it.
PR: 12345: powerokwait:/sbin/shutdown-c "power restored; shutdown cancelled"
# Run Gettys in standard runlevels
1: 2345: respawn:/sbin/mingetty tty1
2: 2345: respawn:/sbin/mingetty tty2
3: 2345: respawn:/sbin/mingetty tty3
4: 2345: respawn:/sbin/mingetty tty4
5: 2345: respawn:/sbin/mingetty tty5
6: 2345: respawn:/sbin/mingetty tty6
# Run xdm in runlevel 5
X: 5: respawn:/etc/X11/preofdm-nodaemon