Boot process and service control
The boot process of the Linux operating system
System booting is the beginning of the operation of the operating system. Before the user can log in to the system normally, the boot process of the Linux operating system will complete a series of initialization tasks and load the necessary programs and command terminals to prepare for the user to log in.
Process steps
The boot process of the Linux operating system generally includes the following stages:
POST, MBR boot, GRUB menu, loading of the Linux kernel, and initialization of the init process
1. POST
After the server host is turned on, the CPU, memory, graphics card, keyboard and other devices will be initially tested according to the settings in the motherboard BIOS. After the test is successful, the system control will be transferred according to the preset startup sequence, and 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 an optical drive
2. MBR boot
When starting the system from the local hard disk, first transfer the system control right to the partition containing the operating system boot files according to the MBR (Master Boot Record) setting in the first sector of the hard disk; or directly according to the boot in the MBR record The information calls the boot menu (such as GRUB).
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 bootloader 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 kernel and mirror file system settings and path locations
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.el7.x86_64".
Summary: Load the kernel and mirror file system into memory
5. init process initialization
In order to complete the further system boot process, the 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 proceed log in.
Summary: Load the hardware driver, the kernel loads the init process into the memory to run
System initialization process
init process
The /sbin/init program
is loaded and run by the Linux kernel. The init process is the first process in the system. The
PID (process tag) number of the init process is always 1
Systemd
Systemd is a kind of init software of the Linux operating system.
CentOS7 uses a new Systemd startup method, replacing the traditional SysVinit
. The first init process running in CentOS7 is /lib/systemd/systemd
Traditional sysvinit relies on 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 provide on-demand startup services, so that fewer processes can be started, thereby improving System startup speed
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 swap file |
Path | .path | Describe a file or directory in a file system |
Timer | .timer | Describe a timer (used to implement cron-like scheduling tasks) |
Snapshot | .snapshot | Used to save the state of a systemd |
Scope | .scope | Use systemd's bus interface to programmatically create external processes |
Slice | .slice | Describe a group of management system processes that reside in Cgroup through hierarchical organization |
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, can log in to the system without password verification, mostly used for system maintenance |
2 | multi-user.target | User-defined/domain specific run level. The default is equivalent to 3 |
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 |
Troubleshoot startup faults
The boot process of the Linux operating system involves MBR (Main Boot Record), GRUB boot menu, system initialization configuration file and other aspects. Any failure of any link may cause the system to start abnormally, so be sure to pay attention to the relevant File backup
cause of issue
Damage caused by viruses, Trojan horses, etc.
Incorrect partition operations, disk read and write errors
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
Solutions
Make a backup file
in advance, boot
from the installation CD, enter the emergency mode , and restore from the backup file.
Repair MBR sector faults on real machine operation
MBR is located at the first physical sector of the first hard disk (/dev/sda), a total of 512 bytes
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
2. Simulate destruction of MBR boot sector
dd if=/dev/zero of=/dev/sda bs=512 count=1
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 环境,系统将会自动重启
Fix GRUB boot failure
cause of issue
The GRUB boot program in MBR is damaged. The
grub.conf file is missing, and the boot configuration is incorrect.
Failure phenomenon
System booting stalls and the "grub>" prompt is displayed
Solutions
Try to manually enter the boot command to
enter the rescue mode, rewrite or restore grub.conf from the backup
to rebuild the grub program in the MBR sector
Repair GRUB boot failure real machine operation
The package in the /boot/grub/ directory is used for the background image and style of the boot menu
/boot/grub2/grub.cfg #GRUB配置文件
Three methods are listed below
Method 1: Manually enter the guide command (clumsy and cumbersome, not recommended)
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 466 bytes are the master boot record, the partition table is stored in the 477-510 bytes in the MBR sector in.
mkdir /bak
mount /dev/sdb1 /bak
dd if=/dev/sda of=/bak/grub.bak bs=446 count=1
#模拟对MRB中的GRUB引导程序的破坏,但并不破坏分区表
dd if=/dev/zero of=/dev/sda bs=446 count=1
#引导界面进入急救模式,从备份文件中恢复GRUB引导程序
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
Method 3: Boot the interface into emergency mode and rebuild the GRUB menu configuration file
rm -rf /boot/grub2/grub.cfg
#进入急救模式,加载系统镜像,切换到系统根环境
sh-4.2# chroot /mnt/sysimage
#重新将GRUB引导程序安装到第一块硬盘(/dev/sda)的MRB扇区
bash-4.2# grub2-install /dev/sda
#重新构建GRUB菜单的配置文件
bash-4.2# grub2-mkconfig -o /boot/grub2/grub.cfg
#退出chroot 环境,并重启
bash-4.2# exit
sh-4.2# reboot
Forgotten root user password
cause of issue
Forgotten the password of the root user
Failure phenomenon
Cannot perform management operations that require root privileges.
If there is no other account available, you will not be able to log in to the system
Solutions
Enter emergency mode, reset password
Forgot the password of the root user
#进入急救模式,加载系统镜像,切换到系统根环境
sh-4.2# chroot /mnt/sysimage
#重设 root 用户密码
bash-4.1# passwd root
Service control and switching run levels
System service control
System service control
systemctl 控制类型 服务名称
Control type
start: start
stop: stop
restart: restart
reload: reload
status: view service status
Run level of Linux system
View run level
runlevel command-----runlevel can only
view the default run level when switching the run level and the current run level systemctl
systemctl tool
Temporarily switch run level
The init command-----The command parameter of init is the number corresponding to the run level.
The command parameter of systemctl is the specific target.
systemctl tool
Set permanent run level
init 0 systemctl isolate poweroff.target systemctl poweroff
init 1 systemctl isolate rescue.target
init 3 systemctl isolate multi-user.target
init 5 systemctl isolate graphical.target
init 6 systemctl isolate reboot.target systemctl reboot
Set permanent operation level for real machine operation
ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
或者
systemctl set-default multi-user.target
查看系统默认的运行级别
systemctl get-default
Optimize the startup process
ntsysv tool
Provides an interactive, visual window that
can be run on a character terminal to
facilitate centralized management of multiple services
systemctl tool
Does not provide interactive, visual windows to
manage a single service more efficiently
Start and control of system services
ntsysv system service management tool
ntsysv
ntsysv --level 级别列表
After the interface shown in the figure above appears, the operations are as follows:
Press the space bar to mark the service option,
press the ↑, ↓ key to switch the service option,
press the Tab key to switch the cursor
View the startup status of system services
systemctl is-enabled 服务名称
Set the startup status of system services
systemctl enable/disable 服务名称
#开启/关闭开机自启动
Some other major commands
Permanently modify the host name
hostnamectl set-hostname newname
View the status of the hostname
hostnamectl status
Set the system language to Chinese
localectl set-locale LANG=zh_CN.utf8
View the language used by the current system
localectl [status]
View system startup time
systemd-analyze