English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Linux System Boot Process

Many startup messages will be displayed when Linux starts up.

The startup process of the Linux system is not as complex as everyone imagines, and the process can be divided into5Phases:

  • Kernel boot.

  • Run init.

  • System initialization.

  • Establish terminal .

  • User logs in to the system.

Type of init program:

  • SysV: init, CentOS 5Before, configuration file: /etc/inittab.

  • Upstart: init, CentOS 6, configuration file: /etc/inittab, /etc/init/*.conf。

  • Systemd: systemd, CentOS 7, configuration file: /usr/lib/systemd/system、 /etc/systemd/system。

Kernel boot

When the computer is powered on, the BIOS power-on self-test is first performed, starting according to the boot device set in BIOS (usually the hard disk).

After the operating system takes over the hardware, it first reads /Kernel files under the boot directory

Run init

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

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

Run level

Many programs need to be started up. They are called "services" (service) in Windows, and "daemons" (daemon) in Linux.

One of the major tasks of the init process is to run these boot programs.

However, different occasions require different programs to be started, for example, when used as a server, Apache needs to be started, but it is not needed when used as a desktop.

Linux allows different boot programs to be allocated for different occasions, which is called "run level" (runlevel). That is to say, when starting up, programs to be run are determined according to the "run level".

Linux systems have7Run levels (runlevel):

  • Run level 0: System shutdown state, the default run level of the system cannot be set to 0, otherwise it cannot be started normally

  • Run level1: Single-user working state, root privileges, used for system maintenance, remote login is prohibited

  • Run level2: Multi-user state (without NFS)

  • Run level3: Full multi-user state (with NFS), log in after entering the console command-line mode

  • Run level4: The system is not used, reserved

  • Run level5: X11Console, log in after entering the graphical GUI mode

  • Run level6: The system is normally closed and restarted, the default run level cannot be set to6If not, it cannot be started normally

System initialization

There is a line like this in the init configuration file: si::sysinit:/etc/rc.d/rc.sysinit calls and executes/etc/rc.d/rc.sysinit, while rc.sysinit is a bash shell script, which mainly completes some system initialization tasks. rc.sysinit is an important script that needs to be run first at each run level.

It mainly completes the following tasks: activates the swap partition, checks the disk, loads hardware modules, and other tasks that need to be executed first.

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

This line indicates that it runs with5runs with/etc/rc.d/rc,/etc/rc.d/rc is a Shell script that accepts5as parameters to execute/etc/rc.d/rc5.d/directory under all the rc startup scripts,/etc/rc.d/rc5.d/The startup scripts in the directory are actually some link files, not real rc startup scripts. The real rc startup scripts are actually placed in/etc/rc.d/init.d/directory.

These rc startup scripts have similar usage, and they generally accept parameters such as start, stop, restart, status, etc.

/etc/rc.d/rc5.d/The rc startup scripts are usually files starting with K or S, for startup scripts starting with S, they will be run with the start parameter.

And if there is a corresponding script with a K prefix connection and it is already running (with/var/lock/subsys/If the files under the directory are used as a marker), then these started daemons will be stopped first with the stop parameter, and then re-run.

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

As to which daemons will run in each run level, users can set them themselves through chkconfig or "System Services" in setup.

Establishing a terminal

After the rc execution is completed, it will return to init. At this time, the basic system environment has been set up, and various daemons have also been started.

init will open6terminals for user login. In the following6This line defines6terminals:

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 in2,3,4,5All levels will run the mingetty program in the respawn mode, and the mingetty program can open terminals and set modes.

It will also display a text login interface, which is the login interface we often see. In this login interface, it will prompt the user to enter a username, and the username entered by the user will be passed as a parameter to the login program to verify the user's identity.

User login to the system

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

  • (1command line login

  • (2ssh login

  • (3) Graphical Interface Login

For run levels of5For graphical mode users, their login is through a graphical login interface. After successful login, they can directly enter KDE, Gnome, and other window managers.

This article mainly discusses the text mode login situation: when we see the mingetty login interface, we can enter the username and password to log in to the system.

The account authentication program of Linux is login, login will receive the username passed from mingetty as the username parameter.

Then login will analyze the username: if the username is not root and there is /etc/If the 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./etc/Only the terminals registered in securetty allow root users to log in, if this file does not exist, root users can log in from any terminal.

/etc/usertty file for additional access restrictions for users, if this file does not exist, there are no other restrictions.

After analyzing the username, login will search /etc/passwd and /etc/shadow to verify the password and set other account information, such as: what is the home directory, what shell to use. If the home directory is not specified, it will default to the root directory; if the shell is not specified, it will default to /bin/bash.

The way to switch between graphical and text modes

Linux provides six command window terminals for us to log in.

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

If you have installed a graphical interface, by default, it will enter the graphical interface, at this time you can press Ctrl + Alt + F1 ~ F6to enter one of the command window interfaces.

After entering the command window interface, to return to the graphical interface, just press Ctrl + Alt + F7 and it will return.

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

Linux Shutdown

In the Linux field, it is mostly used on servers and rarely encounter shutdown operations. After all, running a service on a server is endless, and shutdown would only be necessary in special circumstances.

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

The shutdown command is: shutdown, you can type 'man shutdown' to view the help document.

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

sync synchronize data from memory to hard disk.
shutdown shutdown command, you can man shutdown to see the help document. For example, you can run the following command to shut down:
shutdown –h 10 ‘This server will shutdown after 10 mins' this command tells us that the computer will10minutes later shut down and will be displayed on the current screen of the logged-in user.
shutdown –h now shut down immediately
shutdown –h 20:25 The system will shut down today20:25Shut down
shutdown –h +10 Shut down after ten minutes
shutdown –r now the system will restart immediately
shutdown –r +10 The system will restart after ten minutes
reboot is to restart, equivalent to shutdown –r now
halt closes the system, equivalent to shutdown –h now and poweroff

In conclusion, whether to restart the system or shut down the system, first run sync Command, which writes data from memory to disk.

There are commands to shut down the system shutdown –h now halt poweroff and init 0 , There are commands to restart the system shutdown –r now reboot init 6

Reference Articles: