Demonstration of boot process and service control and troubleshooting in Linux (super detailed illustration)

1. CentOS boot process management

System booting is the beginning of the operation of the operating system. Before the user can log in to the system normally, the Linux boot process completes a series of initialization tasks, and loads the necessary programs and command terminals to prepare for the user to log in.

1.1 The boot process

1.1.1 Overview of the boot process

The boot process of the Linux operating system generally includes the following stages: power-on self-check, MBR boot, GRUB menu, Linux kernel loading, and init process initialization.
1)
After the server is powered on, the CPU, memory, graphics card, keyboard and other devices will be preliminarily tested according to the settings in the BIOS (Basic Input/Ourput System) of the motherboard. After the test is successful, the system will be transferred to the system control according to the preset startup sequence. Right, most of the time will be transferred to the local hard disk.
Summary : The first device capable of booting the system is detected, such as a hard disk or CD-ROM drive.
2) MBR boot.
When starting the system from the local hard disk, first follow the MBR (Master Boot Record) in the first sector of the hard disk. Set, transfer the system control right to the partition containing the boot file of the operating system; or directly call the boot menu (such as GRUB) according to the boot information in the MBR record.
Summary : Run the boot GRUB boot program placed in the MBR sector
3) GRUB menu
For the Linux operating system, GRUB (Unified Boot Loader) is the most widely used multi-system boot program. After the system control is passed to GRUB, the boot menu will be displayed for the user to choose, and the Linux kernel file will be loaded according to the selected option (or the default value), and then the system control will be transferred to the kernel.
CentOS 7 uses the GRUB2 boot loader.
Summary : The GRUB boot program reads the GRUB configuration file /boot/grub2/grub.cfg to obtain the settings and path locations of the kernel and mirror file system.
4. Load the Linux kernel
The Linux kernel is a pre-compiled special binary file, between various hardware resources and system programs, responsible for resource allocation and scheduling. After the kernel takes over the control of the system, it will fully control the running process of the entire Linux operating system.
In CentOS 7 system, the default kernel file is located at "/boot/vmlinuz-3.10.0-514.e17.x86_64".
Summary : Load the kernel and mirror file system into memory.
5. Initialize the init process in
order to complete the further system boot process , Linux kernel first loads the "/sbin/init" program in the system into the memory to run (the running program is called a process), the init process is responsible for completing the initialization of the entire system, and finally waits for the user to log in.
Summary : Load the hardware driver, the kernel loads the init process into the memory to run

1.1.2 System initialization process init and Systemd

Init process
The process (running program) in the Linux system is marked with numbers, and the identity of each process is marked with PID. In the process of booting the Linux system, "/sbin/init" is the first program loaded by the kernel, so the init process is the first process in the system and the corresponding PID number is always 1. The
init process is the first process in the system, It is the parent process of all processes and provides services to users together.
Systemd
Systemd is a kind of init software of Linux operating system

CentOS7 uses a new Systemd startup method to replace the traditional SysVinit

The first init process running in CentOS7 is /lib/systemd/systemd

Traditional SysVinit relies on the serial execution of Shell scripts to start services, resulting in low efficiency and slow system startup

systemd can start more service processes in parallel, and has the ability to start services on demand, so that fewer processes are started, thereby increasing the system startup speed.
The init process is the first process in the system and is the parent process of all processes
x
Systemd unit type

Unit type extension name Description
Service .service Describe a system service
Socket .socket Describe a socket for inter-process communication
Device .device Describe a device file recognized by the kernel
Mount .mount Describe the mount point of a file system
Automount .automount Describe the automatic mount point of a file system
Swap .swap Describe a memory swap device or file
Path .path Describe a file or directory in a file system
Timer .timer Describe a timer
Target .target Describe a set of systemd units

Systemd target corresponding to the run level

Run level Systemd的target Description
0 target Shutdown state, the host will be shut down when this level is used
1 rescue.target Single user mode, you can log in to the system without password verification, mostly used for system maintenance
2 multi-user.target User-defined/domain-specific run level, which is equal to 3 by default, but cannot be connected to the Internet
3 multi-user.target Full multi-user mode with character interface, most server hosts run at this level
4 multi-user.target User-defined/domain specific run level. The default is equivalent to 3
5 graphical.target The multi-user mode of the graphical interface provides a graphical desktop operating environment
6 reboot.target Restart, the host will be restarted when using this level

1.2 Troubleshoot startup faults

1. MBR sector failure analysis

  • 1) Cause of failure

Damage caused by viruses, Trojan horses, etc.
Incorrect partition operations, disk read and write errors

  • 2) Failure phenomenon

The boot program
cannot be found, the startup is interrupted , the operating system cannot be loaded, and the screen goes black after boot

  • 3) Solutions

Make a backup file
in advance, boot
from the installation CD, enter the emergency mode , and restore from the backup file.

2. Simulate MBR sector failure

MBR is located at the first physical sector of a hard disk (/dev/sda), a total of 512 bytes.
Because it is an experiment, it is necessary to simulate a failure in advance.
Add a new hard disk and mount it for use as a backup disk.
Insert picture description here
Format
Insert picture description here

1. Back up MBR sector data to other disks (/dev/sdb1)

mkdir /backup
mount /dev/sdb1 /backup
dd if=/dev/sda of=/backup/mbr.bak bs=512 count=1

Insert picture description here
Insert picture description here

2. Simulate MBR sector failure

dd if=/dev/zero of=/dev/sda bs=512 count=1

Insert picture description here
Then you can try to restart the virtual machine and find that you can no longer directly enter the system at this time
Insert picture description here

3. The boot interface enters emergency mode and restores MBR sector data from the backup file

先加载好光盘镜像,重启操作系统,
当出现安装向导界面时,选择“Troubleshooting”选项,
再选择“Rescue a CentOS Linux system”选项,进入急救模式
选择“1”选择 Continue并按 Enter 键继续
再次按 Enter 键后将进入带“sh-4.2#”提示符的 Bash Shell 环境
sh-4.2# mkdir /backupdir
sh-4.2# mount /dev/sdb1 /backupdir 				#挂载带有备份文件的分区
sh-4.2# dd if=/backupdir/mbr.bak of=/dev/sda	#恢复备份数据
sh-4.2# exit									#执行exit命令退出临时Shell 环境,系统将会自动重启

Insert picture description here
Insert picture description here
Insert picture description here
You can check that the backupdir path already exists in the mbr.bak file. After
Insert picture description here
exiting, you can log in normally
Insert picture description here

4. Repair GRUB boot failure

Cause of failure

  • The GRUB boot program in MBR is corrupted

  • grub.conf file is missing, the boot configuration error
    Symptom

  • The system boot stalls, and the prompt'grub>' is displayed.
    Solutions

  • Try to manually enter the boot command

  • Enter emergency mode, rewrite or restore grub.conf from backup

  • Rebuild grub program to MBR sector
    failure recovery

/boot/grub/目录下的包是用于启动菜单的背景图片及样式
/boot/grub2/grub.cfg			GRUB配置文件

Method 1 : This method is not recommended, because it cannot be copied and pasted. All commands are manually inputted one character by one character, which is cumbersome and error-prone

grub> insmod xfs					加载指定的模块到内核
grub> linux16 /vmlinuz-3.10.0-693.el7.x86_64 root=UUID=8fd74986-ae66-4ffd-b7d8-a19f2eca7b6f ro rhgb quiet LANG=zh_CN.UTF-8	            内核的名字及位置等信息
grub> initrd16 /initramfs-3.10.0-693.el7.x86_64.img			镜像系统文件
grub> boot							引导boot

Method 2: Enter the emergency mode and restore the GRUB boot program

  • MBR is located at the first physical sector of the first hard disk (/dev/sda), a total of 512 bytes, the first 446 bytes are the master boot record, and the partition table is stored in the 477-510 bytes in the MBR sector Medium, so the first 446 bytes are backed up here, and the previous partition table is retained.
mkdir /bak
mount /dev/sdb1 /bak
dd if=/dev/sda of=/bak/grub.bak bs=446 count=1
  • Simulates the destruction of the GRUB boot program in the MBR, but does not destroy the partition table
dd if=/dev/zero of=/dev/sda bs=446 count=1  
  • The boot interface enters emergency mode, and the GRUB boot program is restored from the backup file
sh-4.2# mkdir /backupdir
sh-4.2# mount /dev/sdb1 /backupdir 	
sh-4.2# dd if=/backupdir/grub.bak of=/dev/sda	
sh-4.2# exit

The operation command is the same as above to repair the MBR. Only need to change the 512 bytes to 446 bytes, no longer demonstrate
Method 3: The boot interface enters the emergency mode and rebuild the GRUB menu configuration file

  • This method generally deals with the situation that the /boot/grub2/grub.cfg file is deleted or lost by mistake or the boot configuration is incorrect.
    1) To simulate a failure experiment, delete the /boot/grub2/grub. cfg file first
rm -rf /boot/grub2/grub.cfg

Insert picture description here

48.png)
appears grub> cannot start normally
Insert picture description here

2) Enter the emergency mode, load the CD image, switch to the system root environment,
enter the emergency mode, press the ESC key when loading the interface to enter the emergency mode
Insert picture description here

Insert picture description here
Sometimes the progress bar is too fast to read, too late to click the esc button, you can use the following methods to achieve:
Shut down first,
Insert picture description here
turn on the power and enter the firmware
Insert picture description here
. Move the arrow keys to the Boot ± key to move the CD-ROM Drive to the first line.
Insert picture description here
Press F10 to save and
Insert picture description here
enter the emergency mode.
Insert picture description here

sh-4.2# chroot /mnt/sysimage

3) Re-install the GRUB boot program to the MBR sector of the first hard disk (/dev/sda)

bash-4.2# grub2-install /dev/sda

4) Rebuild the configuration file of the GRUB menu

bash-4.2# grub2-mkconfig -o /boot/grub2/grub.cfg

5) Exit the chroot environment and restart
Insert picture description here

bash-4.2# exit
sh-4.2# reboot

Can start normally
Insert picture description here

5. Forget the password of the root user

1) the cause
forgetting the root password
2) failure phenomenon
can not require root permissions management operation
if there is no other available accounts, you can not log into the system
3) Solutions
enters emergency mode, reset passwords
4) forgotten the root password
to enter Emergency mode, load the system image, switch to the system root environment

sh-4.2# chroot /mnt/sysimage     //进入急救模式,加载系统镜像,切换到系统根环境
bash-4.1# passwd root      //重设 root 用户密码
**按照步骤重设即可**

The steps to enter the emergency mode are the same as ↑ and will not be demonstrated

2. System service control and optimization of the startup process

1. Command format

systemctl 控制类型 服务名称

2. Common control types:

  • start: start
  • stop: stop
  • restart: restart
  • reload: reload
  • status: View service status

3. Run level

  1. View run level
  • runlevel command (view the run level before the switch and the current run level, N means there is no switch before)
  • systemctl tool
  • systemctl get-default (View the default run level of the system)
  1. Temporary/permanent switch run level
    Temporary switch
  • init command
  • systemctl tool
    permanent switch

Two commands:

ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target      
systemctl set-default multi-user.target    

systemctl get-default    查看系统默认的运行级别

4. Optimize the startup process

ntsysv tool

  • Provide an interactive, visual window
  • Can be run in a character terminal
  • Convenient for centralized management of multiple services
  • Used to control whether the service starts automatically after booting
    ■ systemct tool
  • No interactive, visual window is provided
  • Manage a single service more efficiently

5. Start and control of system services

■ntsysv system service management tool

ntsysv
ntsysv --level 级别列表
  • Press the space bar to mark the service option
  • Press ↑, ↓ to switch service options
  • Press the tab key to switch the cursor
    Insert picture description here

6. Start and control of system services

(1) View the startup status of the system service
systemctl is-enabled service name
2, service name setting the startup status of the system service
systemctl enable service name//enable boot auto-start
systemctl disable service name//disable boot auto-start

7. Permanently modify the host name and view the status of the host name

hostnamectl set-hostname newname          永久修改主机名
hostnamectl status                        查看主机名的状态

8. Set the system language to Chinese

localetcl set-locale LANG=zh_CN.utf8      设置系统语言为中文
localectl [status]                        查看当前系统使用的语言

9. View the system startup time

systemd-analyze

Guess you like

Origin blog.csdn.net/zhangyuebk/article/details/113730617