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

Introduction to the startup process of Linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly explains "Introduction to Linux Startup Process". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "Introduction to Linux Startup Process" together.

Step 1: Load the kernel

After the operating system takes over the hardware, it first reads the kernel file under the/boot directory.

1 $ ls /boot

2

3 config-3.2.0-3-amd64

4 config-3.2.0-4-amd64

5 grub

6 initrd.img-3.2.0-3-amd64

7 initrd.img-3.2.0-4-amd64

8 System.map-3.2.0-3-amd64

9 System.map-3.2.0-4-amd64

10 vmlinuz-3.2.0-3-amd64

11 vmlinuz-3.2.0-4-amd64

Step 2: Start the initialization process

Once the kernel files are loaded, the first program,/sbin/init, runs, which initializes the system environment.

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

Step 3: Determine the operation level

Many programs need to be booted. They are called "services" in Windows and daemons in Linux.

One of the tasks of the init process is to run these boot-up programs. However, different occasions need to start different programs, such as when used as a server, you need to start Apache, used as a desktop does not need. Linux allows different boot programs to be assigned to different situations, which is called run level. That is, startup determines which programs to run based on the run level

Linux comes pre-configured with seven run-levels (0-6). In general, 0 is shutdown, 1 is single-user mode (i.e. maintenance mode), and 6 is restart. Run levels 2-5 vary from distro to distro, and for Debian, it's the same multi-user mode (i.e. normal mode).

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 reads like this:

1id:2:initdefault:

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

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 runlevel has a corresponding subdirectory under the/etc directory that specifies the program to load.

1 /etc/rc0.d

2 /etc/rc1.d

3 /etc/rc2.d

4 /etc/rc3.d

5 /etc/rc4.d

6 /etc/rc5.d

7 /etc/rc6.d

The "rc" in the directory name above stands for run command, and the final d stands for directory. Let's see what programs are specified in the/etc/rc2.d directory.

1 $ ls /etc/rc2.d

2

3 README

4 S01motd

5 S13rpcbind

6 S14nfs-common

7 S16binfmt-support

8 S16rsyslog

9 S16sudo

10 S17apache2

11 S18acpid

12 ...

As you can see, except for the first file README, the other file names are of the form "letter S+ two digits + program name." The letter S means Start (start script run parameter), if this position is the letter K, it means Kill (close), that is, if you switch from another run level, you need to close the program (start script run parameter stop). The following two digits indicate the processing order, the smaller the number, the earlier the processing, so the first program to start is motd, then rpcbing, nfs... When the numbers are the same, they are started in alphabetical order of the program name, so rsyslog will start before sudo.

All files in this directory (except README) are programs that are loaded at startup. If you want to add or remove certain programs, it is not recommended to manually modify the/etc/rcN.d directory. It is best to use some special commands to manage it.

Step 4: Load the boot program

As mentioned earlier, each of the seven preset "run levels" has a directory for programs that need to be booted. It's not hard to imagine that if multiple runlevels need to start the same program, there will be a copy of the startup script for that program in each directory. This creates administrative headaches: if you want to modify the startup script, doesn't every directory have to be changed again?

Linux's solution is to set the programs listed in the seven/etc/rcN.d directories as link files, pointing to another directory/etc/init.d, where the real startup scripts are unified. The init process loads the boot program one by one, essentially running the boot scripts in this directory.

Below is the link file's true point.

1 $ ls -l /etc/rc2.d

2

3 README

4 S01motd -> ../ init.d/motd

5 S13rpcbind -> ../ init.d/rpcbind

6 S14nfs-common -> ../ init.d/nfs-common

7 S16binfmt-support -> ../ init.d/binfmt-support

8 S16rsyslog -> ../ init.d/rsyslog

9 S16sudo -> ../ init.d/sudo

10 S17apache2 -> ../ init.d/apache2

11 S18acpid -> ../ init.d/acpid

12 ...

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

1 $ sudo /etc/init.d/apache2 restart

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

Step 5: User login

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

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

1 (1) Command line login

2

3 (2) SSH login

4

5 (3) Graphical interface login

All three have their own way of authenticating users.

Command-line login: The init process calls the getty program (get teletype), asking the user to enter a username and password. After typing, call login to check the password (Debian also runs an additional identity checker,/etc/pam.d/login). If the password is correct, read the shell specified by the user from the file/etc/passwd and launch the shell.

(2) ssh login: At this point, the system calls the sshd program (Debian also runs/etc/pam.d/ssh), replaces getty and login, and then launches the shell.

(3) GUI login: The init process calls the display manager, and the Gnome graphical interface corresponds to the display manager gdm (GNOME Display Manager), and then the user enters the username and password. If the password is correct, read/etc/gdm3/Xsession and start the user session.

Step 6: Enter login shell

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

Debian's default shell is Bash, which reads in a series of configuration files. The three cases in the previous step, in this step of processing, there are also differences.

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

1 ~/.bash_profile

2 ~/.bash_login

3 ~/.profile

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

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

(3) GUI login: Load only/etc/profile and ~/.profile. That is,~/.bash_profile doesn't work with or without it.

Step 7: Open the non-login shell.

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

After entering the operating system, users often manually open a shell. This shell is called a non-login shell, meaning that it is different from the shell that appears at login and does not read configuration files such as/etc/profile and.profile.

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

You might ask, if you don't go into the non-login shell, doesn't.bashrc stop running, and bash can't be customized? In fact, Debian has already considered this problem, please open the file ~/.profile, you can see the following code:

1 if [ -n "$BASH_VERSION" ]; then

2 if [ -f "$HOME/.bashrc" ]; then

3 . "$HOME/.bashrc"

4 fi

5 fi

The above code first determines whether the variable $BASH_VERSION has a value, then determines whether the.bashrc file exists in the home directory, and if it exists, runs the file. The dot at the beginning of the third line is a shorthand for the source command, which means running a file. It can also be written as "source ~/.bashrc".

Therefore, whenever you run the ~/.profile file, the ~/.bashrc file runs with it. However, as mentioned in the first case in the previous section, if the ~/.bash_profile file exists, it is possible that the ~/.profile file will not run. To solve this problem, simply write the following code to.bash_profile.

1 if [ -f ~/.profile ]; then

2 . ~/.profile

3 fi

In either case,.bashrc is executed and user settings can be safely written to the file.

The reason why Bash is so cumbersome is due to historical reasons. In the early days, computers were slow and configuration files took a long time to load, so the authors of Bash had to divide the configuration file into several parts and load it in stages. Common system settings are placed in/etc/profile, user settings that need to be inherited by all child processes are placed in.profile, and settings that do not need to be inherited are placed in.bashrc.

By the way, besides Linux, Mac OS X uses a shell called Bash. However, it only loads.bash_profile and then calls.bashrc inside.bash_profile. And this is true whether you log in with ssh or launch a shell window in a graphical interface.

Thank you for reading, the above is the "Linux startup process introduction" content, after the study of this article, I believe that we have a deeper understanding of the Linux startup process introduction this issue, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

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

Servers

Wechat

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

12
Report