The INIT process number is 1 (PS-Aux | less). From this point, we can see that the INIT process is the starting point of all processes in the system. After Linux completes the kernel boot, start to run the INIT program.
The INIT program needs to read the configuration file/etc/inittab. Inittab is an unexecutable text file consisting of several lines of commands.
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 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:
Code:
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.
The runlevel field specifies the runlevel level. You can specify multiple runlevel levels, or do not specify a specific value for the runlevel field.
The action field defines the status in which the process should run:
Code:
Boot runs when the system starts, ignoring runlevel
Bootwait runs when the system starts and init waits for the process to complete. Ignore runlevel
Ctrlaltdel run when CTRL + ALT + DEL are pressed simultaneously, and the SIGINT signal is sent to init. Ignore runlevel
Initdefault: do not execute this process. It is used to set the default runlevel.
Kbrequest runs when init receives a signal from the keyboard. The keyboard combination is required to comply with keyboardsigral (see/usr/share/doc/KBD-* Documents on the keyboard combination)
Off indicates that the process is not running.
Once every runlevel runs once
OnDemand runs when the system specifies a specific running level A, B, and C.
Powerfail runs when init receives the sigpwr Signal
Powerokwait runs when the sigpwd signal is received and the power status in the/etc/file contains OK
Powerwait runs when it receives the sigpwd signal and init waits for the process to end
Respawn restarts the process at any time
Sysinit runs before running the boot or bootwait Process
Wait running process waiting for Input Running Mode
The process field contains the process executed by init. The process adopts the same format as running the process on 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.
Special purpose records
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. */
# 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
### When the running level is 5, run the/etc/rc. d/RC Script with 5 as the parameter, and init will wait for it to return (wait)
L5: 5: Wait:/etc/rc. d/RC 5
L6: 6: Wait:/etc/rc. d/RC 6
### Allow restarting the system by CTRL-ALT-DELETE during startup
# 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"
### Run the/sbin/mingetty program with ttyx as the parameter at Level 2, 3, 4, and 5, and enable the ttyx terminal for user login,
### If the process exits, run the mingetty Program (respawn) again)
# 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 the xdm program at level 5, provide the xdm graphical login interface, and re-Execute (respawn) upon exit)
# Run xdm in runlevel 5
X: 5: respawn:/etc/X11/preofdm-nodaemon
The preceding inittab file is used as an example to describe the inittab format. The row starting with # Is a comment row. Each row has the following format except the comment row:
ID: runlevel: Action: Process
The detailed explanations of the above items are as follows:
1. ID
Id refers to the entry identifier, which is a string. For other login program items such as Getty or mingetty, the ID must be the same as the TTY number; otherwise, the Getty program will not work properly.
2. runlevel
Runlevel is the identifier of the running level of init. It is generally 0-6 and S or S. The running levels 0, 1, and 6 are retained by the system. Among them, 0 is used as the shutdown action, 1 is used as the restart to the single-user mode, and 6 is used as the restart; S and S share the same meaning, indicating the single-user mode, the inittab file is not required, so it does not appear in inittab. In fact, when you enter single-user mode, init runs/sbin/sulogin directly on the console (/dev/console. In general system implementation, 2, 3, 4, and 5 are used. In the RedHat system, 2 indicates that the multi-user mode is not supported by NFS, 3 indicates full multi-user mode (also the most common level), 4 is reserved for user customization, and 5 indicates xdm graphical login mode. 7-9 levels are also available, which are not defined by traditional UNIX systems. Runlevel can be multiple parallel values to match multiple running levels. For most actions, runlevel is executed only when it matches the current running level.
3. Action
Action describes the running mode of the subsequent process. Optional values of action include initdefault, sysinit, boot, and bootwait:
Initdefault is a special action value used to identify the default startup level. When init is activated by the core, it reads the initdefault item in inittab and obtains the runlevel, as the current running level. If there is no inittab file or there is no initdefault item, init will request to enter runlevel on the console.
Actions such as sysinit, boot, and bootwait will run unconditionally when the system is started, ignoring runlevel.
Other actions (excluding initdefault) are related to a certain runlevel. The definitions of each action are described in detail in the man manual of inittab.
4. Process
Process is the specific execution program. The program can be followed by parameters.
Part 3: system initialization
There is such a line in the init configuration file:
Si: sysinit:/etc/rc. d/rc. sysinit
It calls and executes/etc/rc. d/rc. sysinit, while RC. sysinit is a bash shell script, which mainly
There is such a line in the init configuration file:
Si: sysinit:/etc/rc. d/rc. sysinit
It calls/etc/rc. d/RC. sysinit, while RC. sysinit is a bash shell script mainly used to complete system initialization, RC. sysinit is an important script to run at every running level. It mainly includes activating swap partitions, checking disks, loading hardware modules, and other tasks that require priority.
RC. sysinit has more than 850 lines, but each single function is relatively simple and has annotations. It is recommended that interested users read the file on their own machines, to learn more about system initialization. This file is long, so it is not listed in this article, and will not be described in detail.
After the RC. sysinit program is executed, the system returns init to continue the next step.
Part 4: Start the running daemon
After RC. sysinit is executed, init will be returned to continue other actions. Normally, the/etc/rc. d/RC program will be executed next. Taking running level 5 as an example, init will execute the following line in the configuration file inittab:
L5: 5: Wait:/etc/rc. d/RC 5
This line indicates running/etc/rc with 5 as the parameter. d/RC,/etc/rc. d/RC is a shell script that accepts 5 as the parameter and executes/etc/rc. d/rc5.d/All RC startup scripts in the directory,/etc/rc. in the D/rc5.d/directory, these startup scripts are actually some link files, rather than the real RC startup scripts. The real RC startup scripts are actually stored in/etc/rc. d/init. d/directory. These RC boot scripts have similar usage. They generally accept parameters such as start, stop, restart, and status.
The rc startup script in/etc/rc. d/rc5.d/is usually a link file starting with K or S. For a STARTUP script starting with S, it will run with the start parameter. If the corresponding script is found to have a K-header link, and it is already in the running state (marked by a file under/var/lock/subsys ), stop the started daemon with the Stop parameter and run the daemon again. This ensures that all related daemon will be restarted when init changes the running level.
You can use chkconfig or "system services" in setup to set which daemon will run at each running level. Common daemon processes include:
AMD: automatically installs the NFS daemon.
Apmd: Advanced Power Management daemon
Arpwatch: records logs and constructs an ethernet address and IP address pair database that is visible on the LAN interface.
Autofs: automatically installs the management process automount, which is related to NFS and relies on NIS.
Crond: the daemon of scheduled tasks in Linux
Named: DNS Server
Netfs: Install NFS, Samba, and Netware Network File Systems
Network: Activate the script program with configured network interfaces
NFS: Enable the NFS service
Portmap: RPC Portmap manager, which manages RPC-based connections
Sendmail: Sendmail
SMB: Samba file sharing/printing service
Syslog: a script that enables Syslog and klogd system logs to wait for processes during system boot.
XFS: X Window server, which provides a font set for local and remote X Servers
Xinetd: supports core daemon processes of multiple network services and manages services such as wuftp, sshd, and telnet.
These daemon processes are also started, the RC program is executed, and the init is returned to continue the next step.
Part 5: Establish a terminal
After the RC is executed, init is returned. At this time, the basic system environment has been set and various daemon processes have been started. Init will then open six terminals so that users can log on to the system. You can switch between the six terminals by pressing Alt + FN (N corresponds to 1-6. The following six lines in the inittab define six terminals:
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
From the above, we can see that the mingetty program will be run in the respawn mode in the 2, 3, 4, and 5 running levels. The mingetty program can open the terminal and set the mode. At the same time, it will display a text logon interface, which is a logon interface we often see. In this logon interface, a user is prompted to enter the user name, the user input will be passed as a parameter to the login program to verify the user's identity.
Part 6: log on to the system and start the system
For graphical users with a running level of 5, their logon is through a graphical logon interface. After successful logon, you can directly go to the KDE, gnome, and other Window managers. This article focuses on text-based Logon:
When we see the mingetty logon interface, we can enter the user name and password to log on to the system.
In Linux, the Account Verification Program is login. login receives the username sent from mingetty as the username parameter. Login analyzes the user name. If the user name is not root and the/etc/nologin file exists, login outputs the content of the nologin file and then exits. This is usually used to prevent non-Root User Logon during system maintenance. Only terminals registered in/etc/securetty allow the root user to log on. If this file does not exist, the root user can log on to any terminal. The/etc/usertty file is used to add access restrictions to users. If this file does not exist, there are no other restrictions.
After the username is analyzed, login searches for/etc/passwd and/etc/shadow to verify the password and set other information about the account, such as what is the main directory and what shell is used. If no main directory is specified, the root directory is used by default. If no shell is specified,/bin/bash is used by default.
After the login program is successful, the last logon information (recorded in/var/log/lastlog) is output to the corresponding terminal ), check whether the user has any new emails (in the directory of the corresponding user name of/usr/spool/mail ). Then set various environment variables: For Bash, the system first looks for the/etc/profile script file and runs it. Then, if the user's main directory exists. execute the bash_profile file. Other configuration files may be called in these files. After all the configuration files are executed, various environment variables are also set, A familiar command line prompt will appear, and the entire startup process ends.
InittabThis document describes the processes (such as/etc/init. d/boot,/etc/init. d/RC, and Getty) started during system boot and normal operations ).
Init
(8) DiscussionRunlevels)Every running level has a set of processes started by itself. The effective running level is0-6AddOnDemandEntryA,BAndC.InittabEach entry in the document has the following format:
ID:Runlevels:Action:Process
Rows starting with '#' are ignored.
ID
InittabThe unique identifier of the entry in the document, which must be 1-4 characters long (if the sysvinit program is compiled by a library with the version number less than 5.2.18 or a. Out, it must be 2 characters long ).
Note: For Getty or other registration processes,IDMust be the TTY Suffix of the response terminal line, such1ResponseTty1Otherwise, the registration process cannot work normally.
Runlevels
Lists the running levels of a specified action.
Action
Describes the action to take.
Process
The process to be executed. If the process domain starts with a '+,InitThis process is not recorded in the utmp and wtmp documents. This is because Getty needs to host utmp/wtmp accounting by itself, and this is also a historical vulnerability.
RunlevelsThe domain can contain multiple characters that indicate different running levels, for example123Indicates that the process must be started when the running level is 1, 2, and 3.OnDemandEntryRunlevelsThe domain can containA,B, OrC. UsedSysinit,Boot, AndBootwaitEntryRunlevelsThe domain is ignored.
When the running level is changed, the processes that are not given in the new operation level are killed. Use the sigterm signal first, and then the sigkill.
ActionThe actions that the domain can use include:
Respawn
The process is restarted immediately after it is terminated (for example, Getty ).
Wait
Start this process as long as you enter the specified running level, andInitWait until the process ends.
Once
This process is started once at the specified running level.
Boot
This process is executed during system boot.RunlevelsThe domain is ignored.
Bootwait
This process is executed during system boot.InitWait until the process ends (such as/etc/rc ).RunlevelsThe domain is ignored.
Off
Do nothing.
OnDemand
Before enteringOnDemandDuring runningOnDemandIn any case, the running level (OnDemandThe running level is 'A', 'B', and 'C ').
Initdefault
InitdefaultThe entry indicates the running level entered after the system boot is complete. If such an entry does not exist,InitYou will be asked on the console about the running level to be entered.ProcessThe domain is ignored.
Sysinit
This process is executed during system boot.BootOrBootwaitRun the command before the entry.RunlevelsThe domain is ignored.
Powerwait
This process is executed when the power supply is insufficient. The INIT process is usually notified when the UPS is connected to the computer,InitWait until the process ends before continuing other work.
Powerfail
SimilarPowerwait,InitWait until the process is completed.
Powerokwait
InInitExecute this process immediately after receiving the notification that the power supply has been restored.
Powerfailnow
This process is inInitExecuted when the UPS power supply is quickly exhausted and the external power supply fails (invalid) (assuming the ups and monitoring process can detect such a situation ).
Ctrlaltdel
InInitThis process is executed when the SIGINT signal is received. This means that someone pressesCTRL-ALT-DELKey combination, typically, may want to execute something similarShutdownThen enter the single-user mode or reboot the machine.
Kbrequest
This process is inInitRun the command when receiving a special combination of key signals from the console keyboard.
This document has not been completed for this feature; more information can be found in the kbd-x.xx package (kbd-0.94 is the latest when writing this document ). of course, you may want to map key combinations for some "keyboardsignal" actions. For example, to map (Alt-up arrow), you can use the following methods in the keyboard ing document:
Alt key code 103 = keyboardsignal
Example
This is an example document similar to the old Linux inittab document:
# Inittab for Linux
ID: 1: initdefault:
RC: bootwait:/etc/rc
1: 1: respawn:/etc/Getty 9600 tty1
2: 1: respawn:/etc/Getty 9600 tty2
3: 1: respawn:/etc/Getty 9600 tty3
4: 1: respawn:/etc/Getty 9600 tty4
This document is executed during boot/Etc/rcAnd start the Getty process on ty1-tty4.
A more detailedInittabThere will be different running levels (refer to the comments ):
# Enter the default running level
ID: 2: initdefault:
# Complete system initialization before performing other tasks.
Si: sysinit:/etc/rc. d/bcheckrc
# The running level 0 suspends the system, 6 restarts, and 1 single user mode.
L0: 0: Wait:/etc/rc. d/rc. Halt
L1: 1: Wait:/etc/rc. d/rc. Single
L2: 2345: Wait:/etc/rc. d/rc. multi
L6: 6: Wait:/etc/rc. d/rc. Reboot
# What to do when "3 keys" are pressed.
CA: ctrlaltdel:/sbin/shutdown-T5-RF now
# Running level 2 and 3: generate the Getty process on the console. If the running level is 3, generate Getty on the modem.
1: 23: respawn:/sbin/Getty tty1 VC Linux
2: 23: respawn:/sbin/Getty tty2 VC Linux
3: 23: respawn:/sbin/Getty tty3 VC Linux
4: 23: respawn:/sbin/Getty tty4 VC Linux
S2: 3: respawn:/sbin/uugetty ttys2 m19200