YouTip LogoYouTip

Linux System Boot

When Linux boots, we see a lot of boot information. The boot process of a Linux system is not as complicated as one might imagine. It can be divided into 5 stages: * Kernel bootstrapping. * Running init. * System initialization. * Establishing terminals. * User login. > 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 Bootstrapping After the computer is powered on, the BIOS performs a Power-On Self-Test (POST) and boots from the boot device specified in the BIOS settings (usually the hard disk). After the operating system takes over the hardware, it first loads the kernel file from the /boot directory. !(#) * * * ## Running init The init process is the starting point for all processes in the system. You can think of it as the ancestor of all system processes; without this process, no other process in the system would start. The init program first needs to read the configuration file /etc/inittab. !(#) ### Runlevels Many programs need to start at boot. In Windows, they are called "services," while in Linux, they are called "daemons." One of the main tasks of the init process is to run these startup programs. However, different scenarios require different programs to start. For example, when used as a server, Apache needs to start, but when used as a desktop, it does not. Linux allows assigning different startup programs for different scenarios, which is called "runlevel." This means that at boot, the programs to run are determined based on the "runlevel." !(#) A Linux system has 7 runlevels: * Runlevel 0: System halt state. The default runlevel cannot be set to 0, otherwise the system cannot start 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, you enter the console command-line mode. * Runlevel 4: Unused, reserved. * Runlevel 5: X11 console. After login, you enter the graphical GUI mode. * Runlevel 6: System normal shutdown and restart. The default runlevel cannot be set to 6, otherwise the system cannot start normally. * * * ## System Initialization In the init configuration file, there is a line: si::sysinit:/etc/rc.d/rc.sysinit. This calls and executes /etc/rc.d/rc.sysinit, which is a bash shell script. Its main purpose is to complete some system initialization work. rc.sysinit is an important script that must run first for every runlevel. Its main tasks include: activating swap partitions, checking disks, loading hardware modules, and other tasks that need to be executed first. l5:5:wait:/etc/rc.d/rc 5 This line indicates running /etc/rc.d/rc with 5 as the parameter. /etc/rc.d/rc is a Shell script that accepts 5 as a parameter to execute all rc startup scripts in the /etc/rc.d/rc5.d/ directory. These startup scripts in the /etc/rc.d/rc5.d/ directory are actually link files, not the actual rc startup scripts. The real rc startup scripts are located in the /etc/rc.d/init.d/ directory. These rc startup scripts have similar usage; they generally accept parameters like start, stop, restart, status, etc. The rc startup scripts in /etc/rc.d/rc5.d/ are usually link files starting with K or S. For startup scripts starting with S, they will be run with the start parameter. If a corresponding script starting with K is found and is already in a running state (indicated by files under /var/lock/subsys/), it will first stop these already running daemons with the stop parameter, and then restart them. This is done to ensure that when init changes the runlevel, all related daemons will be restarted. As for which daemons will run at each runlevel, users can set them themselves using 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 various daemons have 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 6:2345:respawn:/sbin/mingetty tty6 From the above, we can see that in runlevels 2, 3, 4, and 5, the mingetty program will run in respawn mode. The mingetty program can open a terminal and set its mode. It also displays a text login interface, which is the login screen we often see. This interface prompts the user to enter a username, and the entered username is passed as a parameter to the login program to verify the user's identity. * * * ## User Login Generally, there are three ways for users to log in: * (1) Command-line login * (2) SSH login * (3) Graphical interface login !(#) For users in the graphical mode with runlevel 5, their login is through a graphical login interface. After successful login, they can directly enter window managers like KDE or Gnome. However, this article mainly discusses text-mode login: when we see the mingetty login interface, we can enter our username and password to log in to the system. Linux's account verification program is login, which receives the username passed from mingetty as the username 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 registered in /etc/securetty are allowed for root user login. 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 Modes Linux provides six command-line terminals by default for us to log in. By default, we log in to 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 enter the graphical interface by default. At this time, you can press Ctrl + Alt + F1 ~ F6 to enter one of the command-line windows. When you are in a command-line window and want to return to the graphical interface, just press Ctrl + Alt + F7. If you are using a VMware virtual machine, the shortcut keys for switching command-line windows are Alt + Space + F1~F6. If you are in the graphical interface, press Alt + Shift + Ctrl + F1~F6 to switch to a command-line window. !(#) * * * ## Linux Shutdown In the Linux field, it is mostly used on servers, and shutdown operations are rarely encountered. After all, a service running on a server is endless, and shutdown is only done in special circumstances when necessary. The correct shutdown process is: sync > shutdown > reboot > halt The shutdown command is: shutdown. You can run man shutdown to see the help documentation. For example, you can run the following commands to shut down: sync synchronizes data from memory to the hard disk. shutdown is the shutdown command. You can run man shutdown to see the help documentation. For example, you can run the following commands to shut down: shutdown –h 10 'This server will shutdown after 10 mins' This command tells everyone that the computer will shut down in 10 minutes and will display on the current screen of the logged-in user. shutdown –h now shuts down immediately. shutdown –h 20:25 The system will shut down today at 20:25. shutdown –h +10 Shuts down in ten minutes. shutdown –r now The system restarts immediately. shutdown –r +10 The system restarts in ten minutes. reboot is equivalent to shutdown –r now. halt shuts down the system, equivalent to shutdown –h now and poweroff. Finally, to summarize, whether restarting or shutting down the system, you must first run the **sync** command to write data from memory to the disk. The commands for shutting down are **shutdown –h now halt poweroff** and **init 0**, and the commands for restarting the system are **shutdown –r now reboot init 6**. * * * ## References: * [http://www.ruanyifeng.com/blog/2013/08/linux_boot_process.html](http://www.ruanyifeng.com/blog/2013/08/linux_boot_process.html)
← Linux System ContentsLinux Install β†’