[Liunx] Linux system startup process

We will see a lot of startup information when Linux starts.

The startup process of the Linux system is not as complicated as everyone thinks. The process can be divided into 5 stages:

  • The boot of the kernel.
  • Run init.
  • system initialization.
  • Establish a terminal.
  • The user logs into the system.

Type of init program:

  • SysV: init, before CentOS 5, configuration file: / etc / inittab.
  • Upstart: init, CentOS 6, Configuration files: / etc / inittab, /etc/init/*.conf.
  • Systemd: systemd, CentOS 7, configuration files: / usr / lib / systemd / system, / etc / systemd / system.

Kernel boot

When the computer is turned on, the BIOS self-test is the first to start according to the startup device (usually a hard disk) set in the BIOS.

After the operating system takes over the hardware, first read the kernel files in the / boot directory.


Run init

The init process is the starting point of all processes in the system. You can compare it to the ancestor of all processes in the system. Without this process, no process in the system will start.

The init program first needs to read the configuration file / etc / inittab.

 

Run level

Many programs need to be started. They are called "services" in Windows and "daemons" in Linux.

A major task of the init process is to run these startup programs.

However, different occasions require different programs to be started. For example, when it is used as a server, Apache needs to be started, and it is not necessary to use it as a desktop.

Linux allows different boot programs to be assigned to different occasions. This is called "runlevel". In other words, according to the "run level" at startup, determine which programs to run.

The Linux system has 7 runlevels (runlevel):

  • Operating level 0: The system is in a stopped state. The default operating level of the system cannot be set to 0, otherwise it cannot start normally
  • Run level 1: single-user working state, root authority, used for system maintenance, prohibit remote login
  • Run level 2: Multi-user state (no NFS)
  • Run level 3: Complete multi-user state (with NFS), enter the console command line mode after login
  • Run level 4: The system is unused and reserved
  • Run level 5: X11 console, enter graphical GUI mode after login
  • Run level 6: The system shuts down and restarts normally. The default run level cannot be set to 6, otherwise it cannot start normally

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, and rc.sysinit is a bash shell script , It is mainly to complete some system initialization work, rc.sysinit is an important script that must be run first for each run level.

The main tasks it completes: activate the swap partition, check the disk, load hardware modules, and other tasks that need to be performed first.

l5:5:wait:/etc/rc.d/rc 5

This line indicates that /etc/rc.d/rc is run with 5 as the parameter. /Etc/rc.d/rc is a shell script that accepts 5 as the parameter to execute the /etc/rc.d/rc5.d/ directory All the rc startup scripts under, the startup scripts in the /etc/rc.d/rc5.d/ directory are actually some connection files, rather than real rc startup scripts, the real rc startup scripts are actually all Place in the /etc/rc.d/init.d/ directory.

These rc startup scripts have similar usages. They generally accept parameters such as start, stop, restart, and status.

The rc startup script in /etc/rc.d/rc5.d/ is usually a connection file starting with K or S. For startup scripts starting with S, it will be run with the start parameter.

If it is found that there is a corresponding script and there is a connection beginning with K, and it is already in a running state (using the file under / var / lock / subsys / as a sign), then these stopped guards will be stopped with the stop parameter Process, and then run it again.

This is done to ensure that when init changes the runlevel, all related daemons will be restarted.

As for which daemons will be run in each runlevel, users can set their own through "System Services" in chkconfig or setup.


Establish Terminal

After rc is executed, return to init. At this time, the basic system environment has been set, and various daemons have been started.

Init will then open 6 terminals so that users can log in to the system. The following 6 lines in inittab define 6 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

It can be seen from the above that in the run levels of 2, 3, 4, and 5, the mingetty program will be run in respawn mode. The mingetty program can open the terminal and set the mode.

At the same time, it will display a text login interface, which is the login interface we often see. In this login interface, the user will be prompted to enter the user name, and the user entered by the user will be passed as a parameter to the login program to verify the user's identity.


User login system

Generally speaking, there are three ways for users to log in:

  • (1) Command line login
  • (2) ssh login
  • (3) Graphical interface login

For users in graphical mode with run level 5, their login is through a graphical login interface. After successful login, you can directly enter window managers such as KDE and Gnome.

And this article mainly talks about the situation of text login: when we see the login interface of mingetty, we can enter the user name and password to log in to the system.

The account verification program for Linux is login, and login will receive the username from Mingetty as the username parameter.

Then login will analyze the user name: if the user name is not root, and the / etc / nologin file exists, login will output the content of the nologin file, and then exit.

This is usually used to prevent non-root users from logging in during system maintenance. Only the terminals registered in / etc / securetty allow root users to log in. If this file does not exist, the root user can log in on any terminal.

The / etc / usertty file is used to impose additional access restrictions on users. If this file does not exist, there are no other restrictions.


Switching mode between graphic mode and text mode

Linux presets provide six command window terminals for us to log in.

By default, we log in is the first window, which is tty1. The six windows are tty1, tty2… tty6, you can press Ctrl + Alt + F1 ~ F6 to switch between them.

If you have installed a graphical interface, the default is to enter the graphical interface. At this time, you can press Ctrl + Alt + F1 ~ F6 to enter one of the command window interfaces.

When you enter the command window interface and then return to the graphical interface, just press Ctrl + Alt + F7 to return.

If you are using a vmware virtual machine, the shortcut key for switching the command window is Alt + Space + F1 ~ F6. If you are in the graphical interface, press Alt + Shift + Ctrl + F1 ~ F6 to switch to the command window.


Linux shutdown

Mostly used on the server in the Linux field, rarely encountered shutdown operation. After all, there is no end to running a service on the server, unless under special circumstances, it will be shut down.

The correct shutdown process is: sync> shutdown> reboot> halt

The shutdown command is: shutdown, you can man shutdown to see the help documentation.

For example, you can run the following command to shut down:

sync data from the synchronous memory to the hard disk. 

shutdown shutdown command, you can man shutdown look at the help files. For example, you can run the following command to shut down: shutdown h 10 ' This server will shutdown after 10 mins ' This command tells everyone that the computer will shut down after 10 minutes and will be displayed on the current screen of the logged-in user. shutdown h now immediately shutdown shutdown h 20 : 25 system will shut down today at 20 : 25 shutdown h + 10 shutdown after ten minutes r now system immediately restart shutdown    R + 10 system reboot after 10 minutes is reboot, which is equivalent to shutdown r now halt shuts down the system, which is equivalent to shutdown h now and poweroff 

Finally, to summarize, whether it is to restart the system or shut down the system, first run the sync command to write the data in the memory to the disk.

The shutdown command is shutdown –h now halt poweroff and init 0 , and the command to restart the system is shutdown –r now reboot init 6 .


Reference article:

Guess you like

Origin www.cnblogs.com/HGNET/p/12723948.html