Linux System Boot Process
When Linux boots, we see a lot of boot messages.
The boot process of a Linux system is not as complicated as one might imagine; it can be divided into five stages:
- Booting the kernel.
- Running init.
- System initialization.
- Establishing terminals.
- User login system.
>
Types of init programs:
- 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 powered on, the BIOS performs a power-on self-test and starts according to the boot device set in the BIOS (usually the hard drive).
After the operating system takes control of the hardware, it reads the kernel file from the /boot directory.
Running Init
The init process is the starting point for all system processes. You can think of it as the ancestor of all processes in the system. Without this process, no other processes in the system would start.
The init program first needs to read the configuration file /etc/inittab.
Run Levels
Many programs need to start 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 start at boot.
However, different scenarios require different programs to be started, such as needing Apache when used as a server, but not when used as a desktop.
Linux allows different boot programs to be assigned for different scenarios, which is called "runlevel". That is, at boot time, depending on the "runlevel", it determines which programs to run.
Linux has 7 runlevels:
- Runlevel 0: System halt state, the default runlevel cannot be set to 0, otherwise it cannot start normally.
- Runlevel 1: Single-user mode, root privileges, used for system maintenance, prohibits remote login.
- Runlevel 2: Multi-user mode (no NFS).
- Runlevel 3: Full multi-user mode (with NFS), login into console command line mode.
- Runlevel 4: System not used, reserved.
- Runlevel 5: X11 console, login into graphical GUI mode.
- Runlevel 6: System normal shutdown and reboot, the default runlevel cannot be set to 6, otherwise it cannot start normally.
System Initialization
In the init configuration file, there is a line: si::sysinit:/etc/rc.d/rc.sysinit, which calls and executes /etc/rc.d/rc.sysinit. This is a bash shell script that primarily completes some system initialization tasks. rc.sysinit is an important script that runs first in every runlevel.
It mainly completes tasks such as activating the swap partition, checking disks, loading hardware modules, and other tasks that need to be executed with priority.
l5:5:wait:/etc/rc.d/rc 5
This line indicates running /etc/rc.d/rc with 5 as the argument. /etc/rc.d/rc is a Shell script that takes 5 as an argument to execute all rc startup scripts in the /etc/rc.d/rc5.d/ directory. These startup scripts in /etc/rc.d/rc5.d/ are actually link files, not the real rc startup scripts, which are actually located in the /etc/rc.d/init.d/ directory.
These rc startup scripts typically can accept parameters like start, stop, restart, status.
The rc startup scripts in /etc/rc.d/rc5.d/ are usually link files starting with K or S. For scripts starting with S, they will be run with the start parameter.
If there are corresponding scripts starting with K and they are already running (indicated by files in /var/lock/subsys/), they will first be stopped with the stop parameter and then re-run.
This ensures that when init changes runlevels, all related daemons will be restarted.
As for which daemons will run in each runlevel, users can set it themselves via chkconfig or "System Services" in setup.
Establishing Terminals
After rc finishes, it returns to init. At this point, the basic system environment is set up and all daemons are started.
Init will then open 6 terminals for users to log in. The following 6 lines in inittab define these 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
From the above, it can be seen that the mingetty program will be run in respawn mode at runlevels 2, 3, 4, and 5. The mingetty program can open terminals and set modes.
It also displays a text login interface, which is the login screen we often see. This interface prompts the user to enter a username, which is then 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:
- (1) Command line login
- (2) SSH login
- (3) Graphical interface login
For users at runlevel 5 with a graphical interface, their login is through a graphical login screen. After successful login, they can directly enter window managers like KDE or Gnome.
This article mainly discusses text-based login: When we see the mingetty login interface, we can enter our username and password to log into the system.
The Linux account verification program is login, which receives the username passed from mingetty as a parameter.
Then login analyzes the username: If the username 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 listed in /etc/securetty are allowed for root user login. If this file does not exist, the root user can log in on any terminal.
The /etc/usertty file is used for additional access restrictions on users. If this file does not exist, there are no other restrictions.
Switching Between Graphical and Text Modes
Linux provides six command window terminals for us to log into by default.
The default is the first window, which is tty1. These six windows are tty1, tty2... tty6. You can switch between them by pressing Ctrl + Alt + F1 ~ F6.
If you have installed a graphical interface, you will default to entering the graphical interface. You can then press Ctrl + Alt + F1 ~ F6 to enter one of the command window interfaces.
To return to the graphical interface from the command window interface, simply press Ctrl + Alt + F7.
If you are using a VMware virtual machine, the shortcut keys for switching command windows are Alt + Space + F1~F6. If you are in the graphical interface, press Alt + Shift + Ctrl + F1~F6 to switch to the command window.
Shutting Down Linux
In the Linux domain, shutdown operations are rarely encountered on servers. Server services are typically perpetual unless special circumstances require a shutdown.
The correct shutdown procedure is: sync > shutdown > reboot > halt
The shutdown command is: shutdown. You can view the help documentation with man shutdown.
For example, you can run the following commands to shut down:
sync: Synchronizes data from memory to the hard drive.
shutdown: The shutdown command. You can view the help documentation with man shutdown. For example, you can run the following commands to shut down:
shutdown –h 10 'This server will shutdown after 10 mins': This command informs that the computer will shut down in 10 minutes and displays this message on the current screen of logged-in users.
shutdown –h now: Shuts down immediately.
shutdown –h 20:25: The system will shut down at 20:25 today.
shutdown –h +10: Shuts down in ten minutes.
shutdown –r now: Reboots immediately.
shutdown –r +10: Reboots in ten minutes.
reboot: Reboots, equivalent to shutdown –r now.
halt: Shuts down the system, equivalent to shutdown –h now and poweroff.
To summarize, whether rebooting or shutting down the system, the first step is to run the sync command to write data from memory to the disk.
Shutdown commands include shutdown –h now, halt, poweroff, and init 0. Reboot commands include shutdown –r now, reboot, and init 6.
Reference Articles:
-http://www.ruanyifeng.com/blog/2013/08/linux_boot_process.html