Brief analysis of startup process of Linux system

Source: Internet
Author: User
Tags windows support ssh iptables rsyslog

Contact with Linux system operations for several years, often asked about the Linux system startup process problems, just today is free to comb this process:
In general, all operating system start-up processes are basically:

In general, the Linux system startup process can be summarized in the following simple steps:
1) boot BIOS self-test, load hard disk.
2) Read the MBR and take the MBR boot.
3) Grub boot menu (boot Loader).
4) load kernel kernel.
5) Start the INIT process and set the RunLevel according to the Inittab file
6) Init process, execute Rc.sysinit file.
7) Start the kernel module and perform different levels of scripting.
8) Execute/etc/rc.d/rc.local
9) Start the Mingetty and enter the system login screen.

When installing a Linux system, if you want to set the startup entry to open, you can:
Power on to the BIOS reminder screen, press F11 (Dell Server practice) into the BIOS settings boot MENU, and then set the startup item: Hard drive HD boot, CD/DVD boot, or USB boot.

The following is a detailed explanation of the Linux operating system startup process:

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

12345678910 [[email protected] ~]# ll /boot/total 21668-rw-r--r--. 1 root root   105195 Nov 22  2013 config-2.6.32-431.el6.x86_64drwxr-xr-x. 3 root root     1024 Aug 22 16:31 efidrwxr-xr-x. 2 root root     1024 Aug 22 16:32 grub-rw-------. 1 root root 15217153 Aug 22 16:32 initramfs-2.6.32-431.el6.x86_64.imgdrwx------. 2 root root    12288 Aug 22 16:24 lost+found-rw-r--r--. 1 root root   193758 Nov 22  2013 symvers-2.6.32-431.el6.x86_64.gz-rw-r--r--. 1 root root  2518236 Nov 22  2013 System.map-2.6.32-431.el6.x86_64-rwxr-xr-x. 1 root root  4128368 Nov 22  2013 vmlinuz-2.6.32-431.el6.x86_64

Start the initialization process
After the kernel file is loaded, it starts running the first program/sbin/init, which is initialized with the system environment.

Since Init is the first program to run, its process number (PID) is 1. All other processes derive from it, all of which are its child processes.

Determine Run level
Many programs need to boot up. They are called "services" in Windows, and in Linux they are called "daemons" (daemon).
One of the major tasks of the Init process is to run these boot-up programs. However, different applications need to start different programs, such as when serving as a server, the need to start Apache, as a desktop is not required. Linux allows different start-up programs to be assigned for different occasions, which is called "RunLevel" (runlevel). That is, at startup, depending on the runlevel, determine which programs to run.

Linux presets seven types of init runlevel (0-6):
0: Shutdown mode (equivalent to Poweroff)
1: Single-user mode (crack root password)
2: Multi-user mode with no network support
3: Multi-user mode with network support (i.e. text mode, most commonly used mode in work)
4: reserved, not used
5: X-windows support multi-user mode (i.e. Desktop graphics mode) with network support
6: Reboot the system, that is, restart (equivalent to reboot)

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 is this:
[Email protected] ~]# Cat/etc/inittab
....
Id:3:initdefault:

The value of Initdefault is 3, indicating that the operating level at system startup is 3. If you need to specify a different level, you can modify this value manually.
So what are the programs that run level 3, and how does the system know which programs should be loaded at each level?
The answer is that each run level under the/etc directory has 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 above directory name represents the Run command (running the program) and the last D indicates directory. Let's look at what programs are specified in the/ETC/RC3.D directory.

12345678910111213141516171819202122232425262728293031323334 [[email protected] ~]# ll /etc/rc3.d/total 0lrwxrwxrwx. 1 root root 19 Aug 22 16:30 K10saslauthd -> ../init.d/saslauthdlrwxrwxrwx. 1 root root 18 Aug 22 16:47 K15svnserve -> ../init.d/svnservelrwxrwxrwx. 1 root root 15 Aug 23 16:21 K25squid -> ../init.d/squidlrwxrwxrwx. 1 root root 19 Dec 23 13:14 K45memcached -> ../init.d/memcachedlrwxrwxrwx. 1 root root 20 Aug 22 16:30 K50netconsole -> ../init.d/netconsolelrwxrwxrwx. 1 root root 13 Dec 21 17:45 K60nfs -> ../init.d/nfslrwxrwxrwx. 1 root root 20 Dec 21 17:45 K69rpcsvcgssd -> ../init.d/rpcsvcgssdlrwxrwxrwx. 1 root root 17 Nov 24 14:45 K75ntpdate -> ../init.d/ntpdatelrwxrwxrwx. 1 root root 20 Aug 22 16:31 K87multipathd -> ../init.d/multipathdlrwxrwxrwx. 1 root root 21 Aug 22 16:30 K87restorecond -> ../init.d/restorecondlrwxrwxrwx. 1 root root 15 Aug 22 16:30 K89rdisc -> ../init.d/rdisclrwxrwxrwx. 1 root root 22 Aug 22 16:31 S02lvm2-monitor -> ../init.d/lvm2-monitorlrwxrwxrwx. 1 root root 16 Aug 22 16:31 S07iscsid -> ../init.d/iscsidlrwxrwxrwx. 1 root root 19 Aug 22 16:30 S08ip6tables -> ../init.d/ip6tableslrwxrwxrwx. 1 root root 18 Aug 22 16:30 S08iptables -> ../init.d/iptableslrwxrwxrwx. 1 root root 17 Aug 22 16:30 S10network -> ../init.d/networklrwxrwxrwx. 1 root root 16 Aug 22 16:31 S11auditd -> ../init.d/auditdlrwxrwxrwx. 1 root root 17 Aug 22 16:30 S12rsyslog -> ../init.d/rsysloglrwxrwxrwx. 1 root root 15 Dec 21 17:45 S13iscsi -> ../init.d/iscsilrwxrwxrwx. 1 root root 17 Dec 21 17:45 S13rpcbind -> ../init.d/rpcbindlrwxrwxrwx. 1 root root 17 Dec 21 17:45 S14nfslock -> ../init.d/nfslocklrwxrwxrwx. 1 root root 19 Aug 22 16:31 S15mdmonitor -> ../init.d/mdmonitorlrwxrwxrwx. 1 root root 17 Dec 21 17:45 S19rpcgssd -> ../init.d/rpcgssdlrwxrwxrwx. 1 root root 26 Aug 22 16:31 S25blk-availability -> ../init.d/blk-availabilitylrwxrwxrwx. 1 root root 15 Aug 22 16:30 S25netfs -> ../init.d/netfslrwxrwxrwx. 1 root root 19 Aug 22 16:30 S26udev-post -> ../init.d/udev-postlrwxrwxrwx. 1 root root 18 Oct 25 11:49 S50onealert -> ../init.d/onealertlrwxrwxrwx. 1 root root 14 Aug 22 16:31 S55sshd -> ../init.d/sshdlrwxrwxrwx. 1 root root 16 Oct 26 09:47 S56xinetd -> ../init.d/xinetdlrwxrwxrwx. 1 root root 17 Aug 22 16:30 S80postfix -> ../init.d/postfixlrwxrwxrwx. 1 root root 15 Aug 22 16:30 S90crond -> ../init.d/crondlrwxrwxrwx. 1 root root 11 Aug 22 16:30 S99local -> ../rc.local

can see:
The letter S means start, that is, the starting point (the startup script has a start parameter)
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 startup script has a run parameter of stop).
The next two digits indicate the processing order, the smaller the number, the earlier processing, so the first program to start is MOTD, then rpcbing, NFS ... When the numbers are the same, they start in alphabetical order of the program name, so the Rsyslog will start before sudo.
All files in this directory (except for the readme) are the programs to be loaded at startup. If you want to add or remove some programs, it is not recommended to manually modify the/ETC/RCN.D directory, preferably with some special commands to manage (see here and here).

Load Boot startup program
As mentioned earlier, the seven preset "RunLevel" each have a directory that stores the programs that need to start up. It is not difficult to think that if more than one "runlevel" needs to start the same program, then the program's startup script will have a copy in each directory. This can be a management problem: if you want to modify the startup script, do you want to change every directory?
The Linux solution, is the seven/ETC/RCN.D directories listed in the program, are set as a link file, pointing to another directory/ETC/INIT.D, the real startup scripts are unified in this directory. The init process loads the start-up program, which is actually running the startup script in this directory.

Here's what the link file really points to

12 [[email protected] ~]# ls -l /etc/rc3.dlrwxrwxrwx. 1 root root 10 Aug 22 16:30 /etc/rc3.d -> rc.d/rc3.d

Another benefit of this is that if you want to manually shut down or restart a process, go directly to the directory/ETC/INIT.D to find the startup script.
For example, to restart the iptables server, run the following command:
[Email protected] ~]#/etc/init.d/iptables restart

/ETC/INIT.D This directory name, the last letter D, is the meaning of directory, which indicates that this is a directory that is used to differentiate/etc/init from the program.

User Login
After the boot loader has finished loading, the user will be logged in.

In general, there are three ways to log in: command line login, SSH login, GUI login. In all three cases, users are certified in their own way.
1) command line login: The init process calls the Getty program (meaning get teletype), allowing the user to enter a user name and password. Once the input is complete, call the login program and check the password (Debian will run an identity check/etc/pam.d/login). If the password is correct, read the user-specified shell from the file/etc/passwd and start the shell.
2) SSH Login: The system calls the SSHD program (Debian will run/etc/pam.d/ssh again), replacing Getty and Login, and then launching the shell.
3) GUI Login: The init process invokes the display manager, the Gnome graphical interface corresponds to the display manager for GDM (Gnome Display manager), then the user enters the user name and password. If the password is correct, read/etc/gdm3/xsession and start the user's session.

Enter Login Shell
The so-called shell, simply speaking, is the command-line interface, allowing users to talk directly to the operating system. The shell that is opened when the user logs on is called the login shell.

Linuix The default shell is bash, which reads into a series of configuration files. There are also differences in the three cases of the previous step, which are dealt with in this step.
1) command line login: first read into the/etc/profile, which is a valid configuration for all users, and then look for the following three files, which is the configuration for the current user.
~/.bash_profile
~/.bash_login
~/.profile
It is important to note that these three files are no longer read into the file as long as they exist. For example, if ~/.bash_profile exists, it will not be read into the next two files.
2) SSH login: Exactly the same as the first case.
3) GUI Login: Load/etc/profile and ~/.profile only. In other words, ~/.bash_profile will not run, whether or not it is.

Open Non-login Shell
To be honest, after the last step, the Linux startup process is finished, and the user can already see the command line prompt or the graphical interface. However, for the sake of completeness of the content, this step must be introduced again.
Once the user enters the operating system, a shell is often opened manually. This shell is called the Non-login shell, which means it differs from the shell that appears when logging in, 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 that the user most often touches, but also that it reads into the user's own bash profile ~/.BASHRC. Most of the time, our custom for bash is written in this file.
You might ask, if you don't get into the non-login shell, wouldn't it be. BASHRC won't run, so bash can't finish customizing? In fact, Debian has taken this into account, please open the file ~/.profile and 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, and then determines if there is a. bashrc file in the home directory and runs the file if it exists. The point at the beginning of the third line is the shorthand form of the source command, which means that it is also possible to run a file as "source ~/.BASHRC".
Therefore, as soon as you run the ~/.profile file, the ~/.BASHRC file runs. However, as mentioned in the first case in the previous section, if a ~/.bash_profile file exists, it is possible that the ~/.profile file will not run. It is easy to solve this problem by writing the following code. Bash_profile.
If [-f ~/.profile]; Then
. ~/.profile
Fi
 
This way, in either case, the. BASHRC will execute and the user's settings can be safely written to this file.
Bash's setup is so cumbersome that it's caused by historical reasons. In the early days, the computer was running slowly, and loading the configuration file took a long time, and the bash author had to split the configuration file into sections and load it in stages. The general settings of the system are placed in the/etc/profile, and the user's personal, settings that need to be inherited by all child processes are placed in the. Profile, which does not require the inherited settings to be placed in the. bashrc.
By the way, besides Linux, the shell used by Mac OS X is also bash. However, it only loads. Bash_profile, and then calls. BASHRC inside. Bash_profile. And, whether it's SSH login or launching the shell window in a graphical interface, this is the case.

Brief analysis of startup process of Linux system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.