Linux System Boot Process
When Linux starts, we see a lot of startup messages.
The Linux boot process is not as complicated as everyone imagines; it can be divided into 5 stages:
- Booting the kernel.
- Running init.
- System initialization.
- Setting up terminals.
- User login.
Types of init programs:
Kernel Booting
When the computer is powered on, the BIOS first performs a power-on self-test, then boots according to the boot device set in the BIOS (usually the hard disk).
After the operating system takes over the hardware, it first reads the kernel files in the /boot directory.

Running init
The init process is the starting point of all processes in the system; you can think of it as the ancestor of all system processes. Without this process, no process in the system would start.
The init program first needs to read the configuration file /etc/inittab.

Runlevel
Many programs need to start at boot. 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 situations require different programs to be started: for example, when used as a server, Apache needs to be started, but not when used as a desktop.
Linux allows different startup programs to be assigned to different situations; this is called the "runlevel". In other words, at boot time the programs to run are determined according to the "runlevel".

Linux systems have 7 runlevels:
- Runlevel 0: System halted; the default runlevel cannot be set to 0, otherwise the system cannot boot normally
- Runlevel 1: Single-user mode with root privileges, used for system maintenance; remote login is disabled
- Runlevel 2: Multi-user mode (without NFS)
- Runlevel 3: Full multi-user mode (with NFS); after login, enters console command-line mode
- Runlevel 4: Unused by the system, reserved
- Runlevel 5: X11 console; after login, enters graphical GUI mode
- Runlevel 6: System shuts down and reboots normally; the default runlevel cannot be set to 6, otherwise the system cannot boot normally
System initialization
In init's configuration file there is a line like this: si::sysinit:/etc/rc.d/rc.sysinit. It calls and executes /etc/rc.d/rc.sysinit, and rc.sysinit is a bash shell script whose main job is to perform some system initialization work. rc.sysinit is an important script that must be run first at every runlevel.
Its main tasks are: activating the swap partition, checking disks, loading hardware modules, and other tasks that need priority execution.
l5:5:wait:/etc/rc.d/rc 5
This line means running /etc/rc.d/rc with 5 as an argument. /etc/rc.d/rc is a Shell script that accepts 5 as a parameter and executes all the rc startup scripts in the /etc/rc.d/rc5.d/ directory. The startup scripts in /etc/rc.d/rc5.d/ are actually link files rather than real rc startup scripts; the real rc startup scripts are placed in the /etc/rc.d/init.d/ directory.
These rc startup scripts have similar usage; they generally accept arguments such as start, stop, restart, and status.
The rc startup scripts in /etc/rc.d/rc5.d/ are usually link files starting with K or S; those starting with S are run with the start argument.
If a corresponding script also has a link starting with K and is already running (indicated by a file under /var/lock/subsys/), these already-started daemons are first stopped with the stop argument and then run again.
This is to ensure that when init changes the runlevel, all related daemons are restarted.
As for which daemons run at each runlevel, users can set this themselves through chkconfig or "System Services" in setup.

Setting Up Terminals
After rc finishes executing, control returns to init. At this point the basic system environment has been set up and all kinds of daemons have been started.
init then opens 6 terminals for users to log in to the system. The following 6 lines in inittab define the 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
From the above you can see that at runlevels 2, 3, 4, and 5 the mingetty program is run in respawn mode; the mingetty program can open terminals and set modes.
At the same time it displays a text login interface, which is the login screen we often see. This interface prompts the user for a username, and the username entered is passed as an argument to the login program to verify the user's identity.
User Login
Generally speaking, there are three ways for a user to log in:
- (1) Command-line login
- (2) ssh login
- (3) Graphical interface login

For graphical users at runlevel 5, login is through a graphical login interface. After a successful login, they can go directly into window managers such as KDE or Gnome.
This article mainly discusses text-mode login: when we see the mingetty login interface, we can enter a username and password to log in to the system.
Linux's account verification program is login; login receives the username passed by mingetty as the username argument.
Then login analyzes the username: if the username is not root and the /etc/nologin file exists, login outputs the contents of the nologin file and then exits.
This is usually used during system maintenance to prevent non-root users from logging in. Only terminals registered in /etc/securetty allow the root user to log in; if this file does not exist, the root user can log in from 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 Between Graphical and Text Mode
Linux provides six command-window terminals by default for us to log in.
By default we log in to the first window, tty1. These six windows are tty1, tty2 ... tty6, and you can press Ctrl + Alt + F1 ~ F6 to switch between them.
If you have installed a graphical interface, the system enters graphical mode by default; at this point you can press Ctrl + Alt + F1 ~ F6 to enter one of the command-window interfaces.
After entering a command-window interface, you can return to the graphical interface simply by pressing Ctrl + Alt + F7.
If you are using a vmware virtual machine, the shortcut for switching command windows is Alt + Space + F1~F6. If you are in the graphical interface, press Alt + Shift + Ctrl + F1~F6 to switch to a command window.

Linux Shutdown
In the Linux world, systems are mostly used as servers and shutdown is rarely performed. After all, a service running on a server is endless; only under special circumstances is shutdown unavoidable.
The correct shutdown procedure is: sync > shutdown > reboot > halt
The shutdown command is: shutdown. You can use man shutdown to see the help documentation.
For example, you can run the following command to shut down:
sync 将数据由内存同步到硬盘中。shutdown 关机指令,你可以man shutdown 来看一下帮助文档。例如你可以运行如下命令关机:
shutdown –h 10 ‘This server will shutdown after 10 mins’ 这个命令告诉大家,计算机将在10分钟后关机,并且会显示在登陆用户的当前屏幕中。
shutdown –h now 立马关机
shutdown –h 20:25 系统会在今天20:25关机
shutdown –h +10 十分钟后关机
shutdown –r now 系统立马重启
shutdown –r +10 系统十分钟后重启
reboot 就是重启,等同于 shutdown –r now
halt 关闭系统,等同于shutdown –h now 和 poweroff
Finally, to sum up: whether rebooting or shutting down the system, you must first run sync to write the data in memory to disk.
The shutdown commands are shutdown –h now halt poweroff and init 0 , and the commands to reboot the system are shutdown –r now reboot init 6.