Guidance process and service control

Keywords: Linux Operation & Maintenance server

Linux operating system boot process

Overview of the boot process


1. Post
After the server host is powered on, the CPU, memory, graphics card, keyboard and other devices will be preliminarily tested according to the settings in the BIOS of the motherboard. After the test is successful, the system control will be handed over according to the preset startup sequence, and most of the time will be handed over to the local hard disk.
Summary: check the hardware device and detect the first device that can boot the system, such as hard disk or optical drive

2. MBR guidance
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 setting of MBR (master boot record) in the first sector of the hard disk; Or directly call the Startup 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
GRUB (Unified boot loader) is the most widely used multi system boot program for Linux operating system. After the system control is transferred to GRUB, the Startup 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 GRUB2 boot boot.
Summary: GRUB boot program obtains the settings and path locations of kernel and image file system by reading GRUB configuration file / boot/grub2/grub.cfg

4. Load Linux kernel
Linux kernel is a precompiled special binary file between various hardware resources and system programs, which is responsible for resource allocation and scheduling. After the kernel takes control of the system, it will fully control the running process of the whole Linux operating system.
In Centos 7 system, the default kernel file is located in "/ boot / vmlinuz-3.10.0-514. E17. X86_64".
Summary: load the kernel and image 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 memory for operation (the running program is called a process). The init process is responsible for completing the initialization of the whole system, and finally waits for the user to log in.
Summary: load the hardware driver, and the kernel loads the init process into memory to run

System initialization process

init process

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

Systemd

●Systemd yes Linux Part of the operating system-species init Software
●CentOS7 New in Systemd(Parallel startup) startup mode to replace the traditional SysVinit(Serial service and shell Script startup)
●CentOS7 First run in- individual init Process is/lib/systemd/systemd

System D unit type

Unit typeExtensionexplain
Service.serviceDescribe a system service
Socket.socketDescribes a socket for interprocess communication
Device.deviceDescribes a device file recognized by the kernel
Mount.mountDescribes the mount point of a file system
Automount.automountDescribes the auto mount point of a file system
Swap.swapDescribes a memory swap device or swap file
Path.pathDescribes a file or directory in a file system
Timer.timerDescribe a timer (used to implement cron like scheduling tasks)
Snapshot.snapshotUsed to save the state of a systemd
Scope.scopeUse the bus interface of systemd to create external processes programmatically
Slice.sliceDescribes a group of management system processes organized through a hierarchy residing in Cgroup
Target.targetDescribe a - set of systemd units

The system D target corresponding to the run level

Run leveltarget of system Dexplain
0targetShutdown state, when using this level, the host will be shut down
1rescue.targetSingle user mode, which can log in to the system without password authentication. It is mostly used for system maintenance
2multi-user.targetUser defined / domain specific runlevel. Default equivalent to 3
3multi-user.targetFull multi-user mode of character interface, at which most server hosts run
4multi-user.targetUser defined / domain specific runlevel. Default equivalent to 3
5graphical.targetThe multi-user mode of graphical interface provides a graphical desktop operating environment
6reboot.targetRestart, the host will be restarted when using this level

Level switching

init 0			systemctl isolate poweroff. target		systemctl poweroff		poweroff	Shut down
init 1			systemctl isolate rescue . target											Switch single user mode
init 3			systemctl isolate multi -user. target										Switch character interface multi-user mode
init 5			systemctl isolate graphical. target											Switch GUI multi-user mode
init 6			systemctl isolate reboot. target		systemctl				reboot		restart

Troubleshooting startup faults

Fix the MBR (first sector of hard disk, 512 bytes) sector failure

Cause of failure

  • Damage caused by viruses, Trojans, etc
  • Incorrect partition operation, disk read / write error

Fault phenomenon

  • The pilot cannot be found. Start interrupt
  • Unable to load the operating system, black screen after boot

Solution ideas

  • Backup files shall be prepared in advance
  • Use the installation CD to guide you into first aid mode
  • Restore from backup file

1. Backup MBR sector data to other disks (/ dev/sdb1)
The MBR is located at the -- physical sector of the first hard disk (/ dev/sda), with a total of 512 bytes

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


2. Simulate the destruction of MBR boot sector

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


3. The boot interface enters the first aid mode to recover MBR sector data from the backup file:

First load the CD image and restart the operating system,
When the installation wizard interface appears, select"Troubleshoot ing"option,

Reselection“ Rescue a Centos Linux system" Option to enter first aid mode

Select "1" Continue And press Enter Key continue

Press again Enter Key will enter the belt“ sh-4.2#Bash Shell environment for the prompt
sh-4.2# mkdir /backupdi r
sh-4.2# mount /dev/ sdb1 /backupdir					#Mount a partition with backup files

sh-4.2# dd if=/backupdir/mbr .bak of=/dev/sda		#Restore backup data

sh-4.2# exit										#Execute the exit command to exit the temporary Shell environment, and the system will restart automatically

Fix GRUB (446 bytes) boot failure

Cause of failure

MBR Medium GRUB arch|The pilot program is damaged
grub.conf Missing file, incorrect boot configuration

Fault phenomenon

System introduction|Lead stagnation, display“ grub>" Prompt

Solution ideas

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

Fix GRUB boot failure

Methods: guide the interface to enter the first aid mode and rebuild the GRUB menu configuration file

rm -rf /boot/grub2/grub.cfg

#Enter the first aid mode, load the CD image, and switch to the system root environment


Note: when restarting, click the mouse back to the simulator when reading the note, and then press esc to enter the "Boot" menu page



sh-4.2# chroot /mnt/ sysimage

#Reinstall the GRUB boot program into the MRB sector of the first hard disk (/ dev/sda). If there are multiple partitions, you can omit them

bash-4.2# grub2- install /dev/ sda 		 Note: if there are multiple partitions, the system may not enter (use with caution)

#Rebuild the configuration file for the GRUB menu

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

#Exit the chront environment and restart

bash-4.2# exit
sh-4.2# reboot



f10 restart

Forget root password

Cause of failure

  • Forget the root user password

Fault phenomenon

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

Solution ideas

  • Enter the first aid mode and reset the password

Example

  • Enter first aid mode to change password

System service control and switching operation level

System service control

System service control

systemctl Control type service name

control type

start: start-up
stop: stop it 
restart: Restart
reload: Reload
status: View service status

Run level of Linux system
View operation mode

runlevel Command: view current run level and last run level

systemctl Tools: viewing default run levels

Temporarily switch operation level

init Command: init Command parameters are numbers corresponding to the run level
systemctl Tools: systemctl The command parameters are specific target

Set persistent run level

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


start: it is also self starting
Is active: startup does not start automatically

systemctl enable --now firewalld		#Start the service and start it automatically
systemctl status firewalld				#Turn off the service and cancel the startup

Note: shut down and restart after the above command settings are completed

Optimize startup process

ntsysv tool

  • Provide an interactive, visual window
  • Can run in character terminal
  • Facilitate centralized management of multiple services
  • Used to control whether the service is started or not


    Note: press the up and down keys to select the cursor; Use the space bar to select on or off (* means on); tab key to switch options

systemctl / chkconfig tool

  • Interactive and visual windows are not provided
  • Managing a single service is more efficient

Expand knowledge

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 language used by the current system

localectl [status]

Posted by KnottyAlder on Sat, 20 Nov 2021 03:46:23 -0800