Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the startup process of Linux system?

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you what the Linux system startup process is like, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

In the BIOS phase, the behavior of the computer is basically written to death, and there is not much that can be done; generally, there are four steps: power on, BIOS, master boot record, and operating system. So we generally think that loading the kernel is the first step in the linux startup process.

The first step, load the kernel

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

Detailed explanation of Linux Startup process Linux Startup process

Let's take a look. Under the / boot directory, there are something like this:

$ls / boot config-3.2.0-3-amd64 config-3.2.0-4-amd64 grub initrd.img-3.2.0-3-amd64 initrd.img-3.2.0-4-amd64 System.map-3.2.0-3-amd64 System.map-3.2.0-4-amd64 vmlinuz-3.2.0-3-amd64 vmlinuz-3.2.0-4-amd64

Second, start the initialization process

After the kernel file is loaded, you start running the first program / sbin/init, which initializes the system environment.

Since init is the first program to run, its process number (pid) is 1. All other processes are derived from it and are its child processes.

The third step is to determine the running level.

Many programs need to be booted up. They are called "service" in Windows and "daemon" in Linux.

One of the major tasks of the init process is to run these boot-up programs. However, different programs need to be started in different situations, such as when used as a server, you need to start Apache, but not as a desktop. Linux allows different boot loaders to be assigned to different occasions, which is called "runlevel". That is, at startup, you determine which programs to run according to the run level.

Detailed explanation of Linux Startup process Linux Startup process

Linux presets seven operating levels (0-6). Generally speaking, 0 is shutdown, 1 is single-user mode (that is, maintenance mode), and 6 is restart. Runlevel 2-5, which varies from release to release, is the same multi-user mode (that is, normal mode) for Debian. The init process first reads the file / etc/inittab, which is the run-level settings file. If you open it, you can see that the first line looks like this:

Id:2:initdefault:

The value of initdefault is 2, indicating that the system starts with a runlevel of 2. If you need to specify a different level, you can modify this value manually.

So, what programs are there in run level 2, and how does the system know which programs should be loaded at each level? The answer is that each run level is under the / etc directory, with a corresponding subdirectory that specifies the program to load.

/ etc/rc0.d / etc/rc1.d / etc/rc2.d / etc/rc3.d / etc/rc4.d / etc/rc5.d / etc/rc6.d

The "rc" in the directory name above indicates run command (running the program), and the last d represents directory (directory). Let's see which programs are specified in the / etc/rc2.d directory.

$ls / etc/rc2.d README S01motd S13rpcbind S14nfs-common S16binfmt-support S16rsyslog S16sudo S17apache2 S18acpid...

As you can see, except for the first file, README, the other file names are in the form of "letter S + two digits + program name". The letter S stands for Start, which means startup (the running parameter of the startup script is start), and if this position is the letter K, it represents Kill (off), that is, if you switch from another run level, you need to close the program (the running parameter of the startup script is stop). The next two digits represent the processing order, and the smaller the number, the earlier the processing, so the first program to start is motd, followed by rpcbing, nfs... When the number is the same, it starts in alphabetical order of the program name, so rsyslog starts before sudo.

All the files in this directory (except README) are the programs to be loaded at startup.

The fourth step is to load the boot program

As mentioned earlier, each of the seven preset "run levels" has a directory for programs that need to be booted up. It is not hard to imagine that if multiple "run levels" need to start the same program, then the startup script of the program will have a copy in each directory. This can cause administrative problems: if you want to modify the startup script, don't you have to change every directory?

The solution for Linux is that the programs listed in the seven / etc/rcN.d directories are all set to link files and point to another directory / etc/init.d, where the real startup scripts are all placed. The init process loads the startup programs one by one, which is actually running the startup scripts in this directory.

Detailed explanation of Linux Startup process Linux Startup process

Here is what the link file really points to.

$ls-l / etc/rc2.d README S01motd->.. / init.d/motd S13rpcbind->.. / init.d/rpcbind S14nfs-common->.. / init.d/nfs-common S16binfmt-support->.. / init.d/binfmt-support S16rsyslog->.. / init.d/rsyslog S16sudo->.. / init.d/sudo S17apache2->.. / init.d/apache2 S18acpid- >.. / init.d/acpid...

Another benefit of this is that if you want to shut down or restart a process manually, go directly to the directory / etc/init.d and look for the startup script. For example, to restart the Apache server, run the following command:

$sudo / etc/init.d/apache2 restart

The last letter d of the directory name / etc/init.d, which means directory, indicates that this is a directory to distinguish it from the program / etc/init.

Step 5, user login

After the boot program is loaded, let the user log in.

Detailed explanation of Linux Startup process Linux Startup process

Generally speaking, there are three ways for users to log in:

(1) login on the command line

(2) ssh login

(3) logging in with graphical interface

In these three cases, there are their own ways to authenticate users.

(1) Command line login: the init process invokes the getty program (meaning get teletype) and asks the user to enter a user name and password. When the input is complete, call the login program to check the password (linux will run one more identity checker / etc/pam.d/login). If the password is correct, read the user-specified shell from the file / etc/passwd, and then start the shell.

(2) ssh login: at this point, the system calls the sshd program (linux will run / etc/pam.d/ssh again), replacing getty and login, and then starting shell.

(3) graphical interface login: the init process calls the display manager, and the corresponding display manager of the Gnome graphical interface is gdm (GNOME Display Manager), and then the user enters the user name and password. If the password is correct, read / etc/gdm3/Xsession and start the user's session.

Step 6: enter login shell

The so-called shell is simply a command-line interface that allows users to talk directly to the operating system. The shell that a user opens when he logs in is called login shell.

Detailed explanation of Linux Startup process Linux Startup process

The default shell for linux is Bash, which reads a series of configuration files. There are also differences in the treatment of the three cases in the previous step.

(1) Command line login: first read in / etc/profile, which is a valid configuration for all users, and then look for the following three files in turn, which is the configuration for the current user.

~ / .bash_profile ~ / .bash_login ~ / .profile

It should be noted that as long as one of these three files exists, it will no longer be read into the following files. For example, if ~ / .bash_profile exists, the last two files will not be read in.

(2) ssh login: exactly the same as in the first case.

(3) graphical login: only / etc/profile and ~ / .profile are loaded. In other words, ~ / .bash_profile will not run with or without it.

Step 7: open non-login shell

To be honest, after the previous step, the startup process of Linux is over, and the user can already see the command line prompt or graphical interface. However, for the sake of the integrity of the content, this step must be introduced again.

After users enter the operating system, they will often manually open a shell. This shell is called non-login shell, which means that unlike the shell that appears when logging in, it does not read configuration files such as / etc/profile and .profile.

Detailed explanation of Linux Startup process Linux Startup process

The importance of non-login shell lies not only in that it is the shell that users come into contact with most, but also in that it reads into the user's own bash profile ~ / .bashrc. Most of the time, our customization of bash is written in this file.

You might ask, if you don't enter non-login shell, .bashrc won't run, so bash won't be able to customize it. In fact, Debian has already considered this problem, please open the file ~ / .profile, you can see the following code:

If [- n "$BASH_VERSION"]; then if [- f "$HOME/.bashrc"]; then. "$HOME/.bashrc" fi fi

The above code first determines whether the variable $BASH_VERSION has a value, then determines whether a .bashrc file exists in the home directory, and runs it if it does. The dot at the beginning of the third line, shorthand for the source command, indicates that it is possible to run a file and write it as "source ~ / .bashrc".

Therefore, as long as you run the ~ / .profile file, the ~ / .bashrc file will run in conjunction with it. But as mentioned in the first case in the previous section, if the ~ / .profile file exists, the ~ / .profile file may not be run. To solve this problem, just write the following code into .bash _ profile.

If [- f ~ / .profile]; then. ~ / .profile fi

In this way, in either case, .bashrc will be executed, and the user's settings can be safely written to this file.

The reason why setting up Bash is so tedious is due to historical reasons. In the early days, the computer was slow and it took a long time to load the configuration file, so the authors of Bash had to divide the configuration file into several parts and load it in stages. The common settings of the system are placed in / etc/profile, the user's personal settings that need to be inherited by all child processes are placed in .profile, and the settings that do not need to be inherited are placed in .bashrc.

By the way, in addition to Linux, the shell used by Mac OS X is also Bash. However, it only loads .bash _ profile and then calls .bashrc in .bash _ profile, whether it's a ssh login or launching a shell window in a graphical interface.

These are all the contents of the article "what is the startup process of the Linux system?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report