Linux system startup process
When Linux starts, we will see a lot of startup information.
The startup process of the Linux system is not as complicated as everyone imagines. The process can be divided into 5 stages:
Kernel booting.
Run init.
system initialization.
Create a terminal.
User logs into the system.
Kernel boot
When the computer is powered on, it first performs a BIOS power-on self-test and starts according to the startup device (usually the hard disk) set in the BIOS. .
After the operating system takes over the hardware, it first reads the kernel file 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 would exist. Neither will start.
The init program first needs to read the configuration file /etc/inittab.
Run level
Many programs need to be started at boot. They are called "services" in Windows and "daemons" in Linux.
One of the major tasks of the init process is to run these programs that are started at boot.
However, different programs need to be started in different situations. For example, when used as a server, Apache needs to be started, but when used as a desktop, it is not required.
Linux allows allocating different boot programs for different occasions, which is called "runlevel" (runlevel). That is to say, based on the "run level" at startup, which programs are determined to be run.
Linux system has 7 runlevels (runlevel):
Run level 0: system shutdown state, the system default run level cannot be set to 0, otherwise Unable to start normally
Run Level 1: Single-user working state, root authority, used for system maintenance, remote login is prohibited
Run Level 2 : Multi-user state (without NFS)
Running level 3: Complete multi-user state (with NFS), enter the console command line mode after logging in
Run level 4: The system is not used, reserved
Run level 5: X11 console, enter the graphical GUI mode after logging in
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
Configuration in init There is this line in the 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, which is mainly To complete some system initialization work, rc.sysinit is an important script that must be run first at each run level.
The main tasks it completes are: activating the swap partition, checking the disk, loading hardware modules and other tasks that need to be performed first.
l5:5:wait:/etc/rc.d/rc 5
This line means running /etc/rc.d/rc with 5 as the parameter. /etc/rc.d/rc is a Shell script that accepts 5 as the parameter to execute /etc/rc. All rc startup scripts in the d/rc5.d/ directory and these startup scripts in the /etc/rc.d/rc5.d/ directory are actually connection files, not real rc startup scripts. The rc startup script is actually placed in the /etc/rc.d/init.d/ directory.
These rc startup scripts have similar usage. They generally accept start, stop, restart, status and other parameters.
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 a connection starting with K, and it is already in the running state (using the file under /var/lock/subsys/ as a sign), it will first stop with stop as the parameter. These daemons have been started and then re-run.
This is done to ensure that when init changes the run level, all related daemon processes will be restarted.
As for which daemon processes will be run in each run level, users can set it themselves through chkconfig or "System Services" in setup.
Establish a terminal
After rc is executed, return to init. At this time, the basic system environment has been set up and various daemon processes have been started.
init will then open 6 terminals to allow users to 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
As can be seen from the above, the mingetty program will be run in respawn mode in run levels 2, 3, 4, and 5. The mingetty program can Open terminal and setup mode.
At the same time, it will display a text login interface. This interface is the login interface we often see. In this login interface, the user will be prompted to enter a user name, and the user entered by the user will be passed to the login program as a parameter. 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 run level For 5 graphic users, their login is through a graphical login interface. After successful login, you can directly enter window managers such as KDE and Gnome.
This article mainly talks about text login: when we see the mingetty login interface, we can enter the username and password to log in to the system.
Linux’s account verification program is login. Login will receive the username sent by 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 contents of the nologin file and then exit.
This is usually used to prevent non-root users from logging in during system maintenance. Only terminals registered in /etc/securetty are allowed to log in as the root user. If this file does not exist, root can log in on any terminal./etc/usertty file is used to impose additional access restrictions on users. If this file does not exist, there are no other restrictions.
##How to switch between graphics mode and text mode
Linux default provides six command window terminals for us to log in. By default, we log in to the first window, which is tty1. The six windows are tty1, tty2...tty6. You can press Ctrl + Alt + F1 ~ F6 to switch them. If you have installed a graphical interface, you will enter the graphical interface by default. 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 return to the graphical interface, just press Ctrl + Alt + F7 to come back. 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, please press Alt + Shift + Ctrl + F1~F6 to switch to the command window. .Linux shutdown
In the Linux field, it is mostly used on servers, and shutdown is rarely encountered. operation. After all, there is no end to running a service on the server. Unless under special circumstances, it will be shut down as a last resort. The correct shutdown process is: sysnc > shutdown > reboot > halt The shutdown command is: shutdown, you can use man shutdown to read the help document. For example, you can run the following command to shut down:shutdown Shutdown command, you can man shutdown to read 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 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 Shut down immediately
Shutdown –h 20:25 The system will shut down at 20:25 today
Shutdown –h +10 Shut down in ten minutes
Shutdown –r now The system will restart immediately
Shutdown –r +10 The system will restart after ten minutes
reboot means restarting, which is equivalent to shutdown –r now
halt System, equivalent to shutdown –h now and poweroff
Finally, to summarize, whether you restart the system or shut down the system, you must first run the sync command to write the data in the memory to the disk.
The shutdown commands include shutdown –h now halt poweroff and init 0, and the commands to restart the system include shutdown –r now reboot init 6.