Boot process and service control

This chapter is structured in the
   Linux operating system boot process to
   eliminate startup faults,
   service control and switch run levels to
   optimize the startup process

Overview of the boot process of the Linux operating system
   


The boot process of the Linux operating system
 1. Power-on self-check. After the
   server is turned on, the CPU, memory, graphics card, keyboard and other devices will be preliminarily 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. , Most of the time, it will be handed over to the local hard disk.
   Summary: The first device that can boot 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 file according to the MBR (Master Boot Record) setting in the first sector of the hard disk; or directly according to the MBR record The boot information in the call 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, which is between various hardware resources and system programs, and is responsible for resource allocation and scheduling. After the kernel took control of the system, will have complete control over the entire process of running the Linux operating system
   Centos 7 system, the default kernel file is located in "/boot/vmlinuz-3.10.0-514.e17.x86_64"
   Summary: kernel and image File system loaded into memory

 5. Initialization of the init process
   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), and the init process is responsible for completing the initialization of the entire system , And finally wait for the user to log in.
   Summary: Load the hardware driver, the kernel loads the init process into the memory to run

System initialization process The
   init process

     is loaded and run by the Linux kernel. /sbin/init program The
     init process is the first process in the system and is the parent process of all processes. The
     init process PID (process tag) number is always 1

   Systemd
     Systemd is an init software of the Linux operating system.
     CentOS7 adopts 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 start services on demand, so that fewer processes can be started, thereby improving System startup speed

   Systemd unit type

   Systemd target corresponding to the run level

Repair MBR sector failure
   cause of the malfunction
     destroy viruses, Trojans, etc. caused by
     incorrect operation of partition, disk read and write misuse

   Trouble phenomenon: The
     boot program
     cannot be found, the operating system cannot be loaded after the startup is interrupted , and the screen is black after booting

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

   MBR is located at the first physical sector of the first hard disk (/dev/sda), a total of 512 bytes
 . 1. Backup 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, restore MBR sector data from the backup file,
    first load the CD image, restart the operating system,
    when the installation wizard interface appears, select the "Troubleshooting" option
    and then select the "Rescue a CentOS Linux system" option, Enter the emergency mode,
    select "1", select Continue and press Enter to continue,
    press Enter again, and you will enter the Bash shell environment with "sh-4.2#" prompt
   sh-4.2# mkdir /backupdir
   sh-4.2# mount /dev/sdb1 /backupdir
   #Mount the partition with backup files sh-4.2# dd if=/backupdir/mbr.bak of=/dev/sda #Restore the backup data
   sh-4.2# exit #Execute the exit command to exit the temporary shell environment, the system will Will automatically restart

Repair GRUB boot failure
   failure cause

     The GRUB boot program in the MBR is damaged. The
     grup.com file is missing, and the boot configuration is incorrect.

   Symptom The
     system boot stalls and the prompt "grup>" is displayed

   Solution
     Try to manually enter the boot command to
     enter the emergency mode, rewrite or restore grup.conf from the backup
     to rebuild the grup program in the MBR sector

   The package in the /boot/grub/ directory is used for the background image and style of the boot menu
   /boot/grub2/grub.cfg #GRUB configuration file

   Method 1: Manually enter the boot command (clumsy and cumbersome, not recommended)
     grub> insmod xfs
     #Load the specified module into the kernel grub> linux16.......UTF-8
     #Information such as the name and location of the kernel                            grub> initrd16 /initramfs-3.10.0-693.e17.x86_64.img #Mirror system file
     grub> boot #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 In the 447-510 bytes in the MBR sector,
   mkdir /bak
   mount /dev/sdb1 /bak
   dd if=/dev/sda of=/bak/grub.bak bs=446 count=1

   #Simulate the destruction of the GRUB boot program in MBR, but does not destroy the partition table
   dd if=/dev/zero of=/dev/sda bs=446 count=1

   #The boot interface enters the emergency mode and restores the GRUB boot program 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

   Method 3: Boot the interface into emergency mode, rebuild the GRUB menu configuration file
   rm -rf /boot/grub2/grub.cfg #Enter
   emergency mode, load the CD image, switch to the system root environment
   sh-4.2# chroot /mnt/sysimage

   #Reinstall the GRUB boot program to the MBR sector of the first hard disk (/dev/sda)
   bash-4.2# grub2-install /dev/sda

   #Rebuild the configuration file of the GRUB
   menubash-4.2# grub2-mkconfig -o /boot/grub2/grub.cfg

   #Exit the chroot environment and restart
   bash-4.2# exit
   sh-4.2# reboot

Forgotten root user password
   Failure reasons

     Forgot root user password

   Symptom
     : Management operations that require root privileges cannot be performed.
     If there is no other available account, the system cannot be logged in.

   Solution ideas,
     enter the emergency mode, reset the password

   #Enter the emergency mode, load the system image, switch to the system root environment
   sh-4.2# chroot /mnt/sysimage #Reset the
   root user password
   bash-4.1# passwd root

System service control
   systemctl control type service name

   Control type
     start: start
     stop: stop
     restart: restart
     reload: reload
     status: view service status
       ······

The run level of the Linux system
   view run level
     runlevel command
     systemctl command

   Temporarily switch the run level
     init command
     systemctl command

   Set the permanent run level
   ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
 or
   systemctl set-default multi-user.target

   systemctl get-default #View the
   current default level

Optimize the startup process. The
   ntsysv tool
     provides an interactive and visual window that
     can be run in a character terminal to
     facilitate centralized management of multiple services
     to control whether the service starts automatically.

   The systemctl tool
     does not provide interactive, visual window
     management for a single service, which is more efficient

Start and control
   system services ntsysv system service management tool
     ntsysv
     ntsysv --level level list


     Press the space 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 service name

   Set the self-starting state of the system service
     systemctl enable service name
     #enable automatic startup systemctl disable service name #turn off automatic startup

Permanently modify the host name
   hostnamectl set-hostname newname
View the status of the host name
   hostnamectl status

Set the system language to Chinese
   localectl set-locale LANG=zh_CN.utf8
View the current system language
   localectl [status]

View the system startup time
   systemd-analyze

Guess you like

Origin blog.csdn.net/weixin_53496478/article/details/113540714