Graphic and text explain the Linux boot process and service control, Xiaobai can also know it in seconds!

One, Linux operating system boot process

1.1 Classification of Linux operating system

  • Kernel
    hardware-oriented level
  • Shell
    for user level

1.2 Overview of the boot process

Insert picture description here

1.3 System process initialization

1.3.1 init process

  • Load and run the /sbin/init program by the linux kernel
  • The init process is the first process in the system
  • The PID (Process ID) of the init process is always 1

1.3.2 systemd components

  • systemd is an init component of the linux operating system
  • CentOS7 uses a brand-new System startup method to replace the traditional sysVinit
  • The first init process running in CentOS7 is /lib/systemd/system

1.4 Systemd unit format

Unit type extension name Description
Service .service Describe a system service
Socket .socket Describe a socket for 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 kernel swap device or swap file
Path .path Describe a file or directory of a file system
Timer .timer Describe a timer (used to implement cron scheduling tasks)
Snapshot .snapshot Used to save the state of a system
Scope .scope Use systemd's bus interface to programmatically create external processes
Slice .slice Use a set of management system processes organized by hierarchy in Cgroup
Target .target Describe a set of systemd units

1.5 Systemd target corresponding to run level

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

2. Linux fault problem solving

2.1 Repair MBR sector failure

2.1.1 Cause of failure

  • Damage caused by viruses, Trojan horses, etc.
  • Incorrect partition operation, misoperation of disk read and write

2.1.2 Symptoms

  • Could not find boot reboot, boot interrupted
  • Unable to load operating system, black screen after boot
    Insert picture description here

2.1.3 Solutions

  • Backup files should be made in advance
  • Installation CD boot into emergency mode
  • Restore from backup file

2.1.4 Fault manufacturing and repair

MBR sector destruction

dd if=/dev/sda of=/data/mbr.bak bs=1024k count=1   ## 备份文件
dd if=/dev/zero of=/dev/sda bs=1024k count=1  ## 破坏已备份的源文件
init 6    ## 重启

MBR sector repair

mkdir /data1   
mount /dev/sdb1 /data1   ## 制作新的挂载点
dd if=/data1/mbr.bak of=/dev/sda bs=1024k count=1  ## 将备份文件拷到sda中
reboot

2.1.5 Attention to MBR failure manufacturing and repair

  • The MBR sector exists in the first hard disk space, so if you want to destroy the MBR data, you only need to destroy the data in the first hard disk space.
  • When backing up the first hard disk, note that the backup disk space cannot be less than the size of the first hard disk.

2.2 Repair GRUB boot failure

2.2.1 Causes of failure

  • The GRUB boot program in MBR is corrupted
  • grub.conf is lost and the boot configuration is wrong

2.2.2 Simulate manufacturing failure

cd /boot/grub2/    ## 进入grub程序
rm -rf grub.cfg      ## 删除grub.cfg 模拟故障
init 6    重启系统

2.2.3 Symptoms

System boot stalls, and the situation shown in the figure
Insert picture description here

2.2.4 Repair the fault

  • Try to manually boot the command
  • Enter emergency mode, rewrite or restore grub.cfg from backup
  • Rebuild the grub program to the MBR sector

Repair fault command

Because of the loss of the hard disk configuration, we are going to use a CD to replace the hard disk backup to restore the grub.cfg configuration file, so when you see the above failure phenomenon, restart and press F2 to enter the mode, select the boot mode, and hold the CD "shift+" Set it as the first option, then press F10 to save the setting and proceed with the following command operations.
Insert picture description here

chroot /mnt/sysimage/   ## 引导急救模式,加载系统镜像
grub2-install /dev/sda     ## 重新建立加载sda分区
grub2-mkconfig -o /boot/grub2/grub.cfg    ## 重新构建grub菜单配置文件
exit     ## 退出bash环境
reboot     ## 重启

After the end, because we set the CD as the first option above, we should use the same method to reset the hard disk to the first option to save.

2.3 Forgotten root user password

2.3.1 Cause of failure

Forgotten the password of the root user

2.3.2 Symptoms

  • Cannot perform pipeline operations that require root privileges
  • If there is no other available account, you will not be able to log in to the system

2.3.3 Solutions

Enter emergency mode, reset password

Repair command

chroot /mnt/sysimage 
passed root
new:
retry:
exit
reboot

Three, system service control

3.1 System service control

  • systemctl control type service name
  • service Service name control type
    The effect of the two is the same, but the order of the commands is different.
    When systemctl and service manage services, there must be this service under /lib/systemd/system. The service file or the configuration file of the service in /etc/init.d/.

3.1.1 Control type

control commands effect
start start up
stop stop
restart Restart
reload Reload
status Check service status

The difference between restart and reload is that the former needs to close the service and then start the service, the intermediate service will be disconnected, and the latter will not interrupt the service.

3.2 Run level

3.2.1 View run level

  • The runlevel command
    can only view the switch run level and the current run level
  • systemctl tool
    View the default run level

3.2.2 Temporarily switch run level

  • The init command
    init command parameter is the parameter corresponding to the run level
  • systemctl tool
    command parameters are specific parameters

3.2.3 Set permanent run level

method one:

systemctl set-default multi-user.target

Way two:

rm -rf /etc/systemd/system /default.target
ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target

The functions of the two commands are the same and must be restarted to take effect

Another: View the current run level

 systemctl get-default

3.3 Optimize the startup process

3.3.1 ntsysv tool

  • Provide an interactive, visual window
  • Can be run in a character terminal
  • Convenient for centralized management of multiple services

ntsysv system service management tool

  • ntsysv
  • ntsysv --level level list

3.3.2 systemctl tool

  • No interactive, visual window is provided
  • High efficiency in managing a single service

View the startup status of system services

systemctl is-enable 服务名称

Set the startup status of system services

systemctl enable/disable 服务名称

Guess you like

Origin blog.csdn.net/weixin_50344820/article/details/109160711