Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The Linux system startup process


May 22, 2021 Linux


Table of contents


The Linux system startup process

We'll see a lot of startup information when linux starts.

The startup process for a Linux system is not as complex as you think, and it can be divided into five stages:

  • The boot of the kernel.
  • Run init.
  • System initialization.
  • Establish a terminal.
  • The user logs on to the system.

Kernel boot

When the computer is powered on, the first step is bioS POST, which starts with the startup device (usually a hard disk) set up in the BIOS.

After the operating system takes over the hardware, the kernel files in the /boot directory are read first.

The Linux system startup process

Run init

The init process is the starting point for all processes in the system, and you can liken it to the ancestor of all processes in the system, without which no process in the system would start.

The init program first needs to read the profile /etc/inittab.

The Linux system startup process

The run level

Many programs require power-on startup. They are called "service" in Windows and daemon in Linux.

One of the big tasks of the init process is to run these boot-started programs.

However, different programs need to be started in different situations, such as when used as a server, apache needs to be started, and use as a desktop is not required.

Linux allows different boot programs to be assigned for different situations, which is called "runlevel". That is, startup determines which programs to run based on the Run Level.

The Linux system startup process

Linux systems have 7 run levels:

  • Run level 0: System down state, the system default operating level can not be set to 0, otherwise it can not start normally
  • Run Level 1: Single-user working state, root permission, for system maintenance, no remote landing
  • Run Level 2: Multi-user state (no NFS)
  • Run Level 3: Full multi-user state (NFS), log on and enter console command line mode
  • Run Level 4: The system is not used and reserved
  • Run level 5:X11 console, log in and enter graphic GUI mode
  • Run Level 6: The system shuts down and restarts normally, and the default operating level cannot be set to 6, otherwise it cannot start normally

System initialization

In the init profile there is this line: si:::sysinit:/etc/rc.d/rc.sysinit It calls to execute/etc/rc.d/rc.sysinit, and rc.sysinit is a bash shell script that mainly does some system initialization, and rc.sysinit is the first important script to run at every run level.

It does the main tasks: activate the swap partition, check the disk, load the hardware modules, and some other priorities.

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

This line means that running at 5 arguments/etc/rc.d/rc,/etc/rc.d/rc is a Shell script that accepts 5 as arguments to execute/etc/rc.d/rc5.d/directory All rc startup scripts, /etc/rc.d/rc5.d/directory These startup scripts are actually some connection files, not the real rc startup scripts, the real rc startup scripts are actually placed under /etc/rc/rc.d/init.d/directory.

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

The rc startup script in /etc/rc.d/rc5.d/is usually a connection file that starts with K or S and runs with the startup script starting with S.

If the corresponding script is found to have a K-header connection and is already running (marked with /var/lock/subsys/under file), the daemon that has already been started will be stopped with stop as the stop parameter before running again.

This is done to ensure that all relevant daemons are restarted when init changes the run level.

As for which daemons will be run at each run level, users can come from the row settings through chkconfig or "System Services" in setup.

The Linux system startup process

Establish a terminal

After rc is executed, return to init. At this time, the basic system environment has been set up, a variety of daemons have been started.

Init then opens six terminals so that the user can log on to the system. The following six lines in inittab define six 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 can be seen in the 2, 3, 4, 5 running level will be respawn way to run the mingetty program, mingetty program can open the terminal, set the 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 will prompt the user to enter a user name, and the user input of the user will be passed as a parameter to the login program to verify the user's identity.


The user logs on to the system

In general, there are three ways users log on:

  • (1) The command line logs in
  • (2) ssh login
  • (3) Graphic interface login
The Linux system startup process

For graphical users running level 5, their login is through a graphical login interface. After successful login, you can go directly to KDE, Gnome and other window managers.

And this article is mainly about text-based login: when we see mingetty's login interface, we can enter a username and password to log on to the system.

Linux's account verification program is login, and login receives the username from Mingetty as a username parameter.

The login then analyzes the user name: if the user name is not root and there is a /etc/nologin file, the login outputs the contents of the nologin file and exits.

This is typically used to prevent non-root users from logging on during system maintenance. Only terminals registered in /etc/securetty allow root users to log in, and if this file does not exist, root can log on at any terminal.

The /etc/usertty file is used to provide additional access restrictions to the user, and if this file does not exist, there are no other restrictions.

After analyzing the user name, login searches/etc/passwd and/etc/shadow to verify the password and other information about setting up the account, such as what the home directory is and what shell is used. If you do not specify a home directory, it will default to the root directory, and if you do not specify shell, it will default to /bin/bash.


How graphics mode and text mode are switched

Linux presets provide six command window terminals for us to log in.

By default, we log in to the first window, which is tty1, which is tty1, tty2 ... tty6, you can switch them by pressing Ctrl, Alt, F1, F6.

If you have a graphical interface installed and you enter it by default, you can press Ctrl s Alt s F1 s F6 to enter one of the command window interfaces.

When you enter the command window interface and then return to the graphical interface, just press Ctrl and Alt and F7 and you'll be back.

If you're using a vmware virtual machine, the command window switches shortcuts to Alt and Space and F1 to F6. I f you're in the graphical interface, switch to the command window by pressing Alt-Shift-Ctrl-F1-F6.

The Linux system startup process

Linux shuts down

Most of the linux domain is used on servers and rarely shuts down. After all, running a service on the server is never-ending, and it's only possible to shut down unless there's a special case.

The correct shutdown process is: sync and shutdown and reboot and halt

The shutdown instruction is: shutdown, and you can take a look at the help documentation.

For example, you can run the following command shutdown:

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, whether you restart or shut down the system, you first run the sync command and write the data in memory to disk.

The shutdown commands are shutdown -h now halt poweroff and init 0, and the command to restart the system has shutdown-r now, reboot, and init 6.