Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Theory: boot process and service control (solve the problem of forgetting root, solving the problem of not booting properly)

2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Foreword:

Learn the boot process, eliminate startup failures, service control and switch operation levels, optimize the startup process; focus on repairing MBR sectors, gurb menus, root password reset, optimizing boot

One: boot process overview sequence number step explanation 1 power-on self-test (BIOS) 1. Power-on self-test (with the exception of the server, when the server is powered on, the fan will turn around first), self-test whether the hardware is intact 2. Load the driver 2MBR boot to find the MBR in the system disk to enable the 3GRUB menu selection system to enable (systems such as dual systems, emergency systems, etc.) 4 load the kernel (kernel) kernel is the core part of the system, that is, load the kernel to run the system 5init process initialization PID=1, initialization includes emptying all the previously cached data, restoring the default, and then loading other programs again

Startup type (priority from usr/bindr-xr-xr-x. 5 root root 4096 October 23 13:41 bootdrwxr-xr-x. 20 root root 3300 November 13 10:00 devdrwxr-xr-x. 138 root root 8192 November 13 13:36 etcdrwxr-xr-x. 5 root root 45 November 13 08:53 homelrwxrwxrwx. 1 root root 7 October 23 13:35 lib-> usr/liblrwxrwxrwx. 1 root root 9 October 23 13:35 lib64-> usr/lib64drwxr-xr-x. 2 root root 6 November 5 2016 mediadrwxr-xr-x. 2 root root 6 November 5 2016 mntdrwxr-xr-x. 8 root root 150 November 12 17:33 optdr-xr-xr-x. 203 root root 0 November 13 10:00 proc-rw-r--r--. 1 root root 4 November 13 14:06 ps.txtdr-xr-x---. 14 root root 4096 November 13 10:01 rootdrwxr-xr-x. 42 root root 1260 November 13 13:36 run'lrwxrwxrwx. 1 root root 8 October 23 13:35 sbin-> usr/sbindrwxr-xr-x. 2 root root 6 November 5 2016 srvdr-xr-xr-x. 13 root root 0 November 13 10:00 sysdrwxrwxrwt. 27 root root 4096 November 13 14:04 tmpdrwxr-xr-x. 13 root root 155 October 23 13:35 usrdrwxr-xr-x. 22 root root 4096 November 5 23:09 var III: Systemd unit type

Location of unit type key information Service file: / lib/systemd/system/*./serviceSocket socket, that is, a carrier and medium of communication protocol, written by developers, for example, when installing myscl, you need to use Device device file block device (responsible for storage) and character device (responsible for input and output) Mount mount point Automount automatically mounts Swap swap space, essential components Path path (PATH is a variable, use echo $PATH) Timer timer For example, when cron sets a periodic schedule, you need to call the timer Snapshot to save state, that is, the snapshot function Scope external process Slice management process Target target service 4: run the Systemd target corresponding to the init level (centos 7)

Fifth: repair MBR sector failure 5.1 failure caused by virus, incorrect partition operation, disk read and write misoperation 5.2 failure phenomenon can not find the boot program, boot interrupt can not load the operating system, black screen 5.3 solution ideas are prepared in advance after boot, and form a habit Make backup files in advance to boot into emergency mode and restore from backup files in the form of an installation CD. 5.4.Experimental: simulate MBR sector failure, and then repair it.

Train of thought:

Fix MBR (the other is GPT) sector failure

512 bytes per sector

File meta-information (attribute, location) and system boot file are stored in MBR; file data information (specific content) is stored in other locations on disk.

Steps:

1: back up MBR sector data first

1) add a hard disk to backup sdb, partition, format, mount

[root@localhost ~] # init 6 'restart' [root@localhost ~] # fdisk / dev/sdb 'partition sdb backup disk' Welcome to fdisk (util-linux 2.23.2). The changes remain in memory until you decide to write them to disk. Think twice before using the write command. Device does not contain a recognized partition table uses the disk identifier 0x99e92b15 to create a new DOS disk label. Command (enter m for help): nPartition type: P primary (0 primary, 0 extended, 4 free) e extendedSelect (default p): P partition number (1-4, default 1): start sector (2048-41943039, default is 2048): the default value 2048Last sector will be used, + sector or + size {KMagg G} (2048-41943039, default is 41943039): will use the default value 41943039 partition 1 has been set to Linux type Set the size to 20 GiB command (enter m for help): P disk / dev/sdb:21.5 GB, 21474836480 bytes 41943040 sector Units = sector of 1 * 512 = 512 bytes sector size (logical / physical): 512byte / 512byte I size (min / best): 512byte / 512byte disk label type: dos disk identifier: 0x99e92b15 device Boot Start End Blocks Id System/dev/sdb1 2048 41943039 20970496 83 Linux command (enter m for help): wThe partition Table has been altered calling ioctl () to re-read partition table. Synchronizing disks. [root@localhost ~] # mkfs.xfs / dev/sdb1 'format' meta-data=/dev/sdb1 isize=512 agcount=4, agsize=1310656 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0data = bsize=4096 blocks=5242624 Imaxpct=25 = sunit=0 swidth=0 blksnaming = version 2 bsize=4096 ascii-ci=0 ftype=1log = internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1realtime = none extsz=4096 blocks=0 Rtextents=0 [root@localhost ~] # mkdir / beifen 'create mount point' [root@localhost ~] # mount / dev/sdb1 / beifen 'mount' [root@localhost ~] # cd / beifen [root@localhost beifen] # ls [root@localhost beifen] #

2) then back up the past

[root@localhost beifen] # dd if=/dev/sda of=/beifen/MBR.sda.bak bs=512 count=1 'use the dd command to back up the MBR of / dev/ 's sda disk, note that it is not sba1' that records 1: 0 read-in, 1-0 write-out 512 bytes (512 B) has been copied, 0.000550469 seconds 930 kB/ seconds [root@localhost beifen] # ls' View 'MBR.sda.bak2: simulate failure [root@localhost beifen] # dd if=/dev/zero of=/dev/sda bs=512 count=1' still use dd to overwrite MBR 'record 1' 0 read in record 1'0 write out 512 bytes (512B) replicated, 0.000125984 seconds, 4.1 MB/ seconds [root@localhost ~] # init 6 'restart'

And then there was an error.

3: load the first aid mode in the system image file

1) put the image in first.

2) when reading the note, press esc to enter boot

Choose the third troubleshooting

Select Rescue a CentOS system

3) loading, loading completed

Select the first continue and connect

Then you can enter and type the command.

4: restore the backup data

1) the system entered at this time is the system in the mirror file, not the system in the computer. Here, the mount point is recreated, the disk containing the backup file is mounted, and the dd command is still used to overwrite the backup data.

Then reboot restarts.

Success

Summary:

1: backup mbr sector data

1) add a hard disk for backup sdb

Partition-"format -" mount / abc

2) dd if=/dev/sda of=/abc/sda.mbr.bak bs=512 count=1

2: simulate failure

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

3: load the first aid mode in the system image file

Load the kernel interface in the system image file

4: restore the backup data

Mkdir / aaa

Mount / dev/sdb1 / aaa

Dd if=/aaa/sda.mbr.bak of=/dev/sda bs=512 count=1

Six: repair GRUB boot failure 6.1 cause of failure GRUB boot program in MBR is damaged grub,conf file is lost, boot configuration error 6.2 fault phenomenon system boot stagnant, display "grub >" prompt 6.3 solution try to enter boot command manually (more than 100 lines, don't lose by hand, understand) enter first aid mode Rewrite or restore grub.conf from backup (that is, load the backup in the image file) rebuild the grub program into the MBR sector experiment: grub menu recovery

Train of thought:

1. Use the system first aid mode to enter the command character interface

two。 Load the system in the mirror and enter the bash environment

3. Rebuilding the grub menu system

4. Restart

Steps

1. Check the file location where grub is located first.

[root@localhost ~] # cd / boot' switch to / boot' [root@localhost boot] # lsconfig-3.10.0-693.el7.x86_64efigrubgrub2initramfs-0-rescue-33c124456fa34c50a98483245dfea58d.imginitramfs-3.10.0-693.el7.x86_64.imginitrd-plymouth.imgsymvers-3.10.0-693.el7.x86_64.gzSystem.map-3.10.0-693.el7.x86_64vmlinuz-0-rescue-33c124456fa34c50a98483245dfea58dvmlinuz-3.10.0-693.el7.x86 _ 64 [root@localhost boot] # ls grub/splash.xpm.gz [root@localhost boot] # ls grub2/device.map fonts grub.cfg ('profile') grubenv i386-pc locale [root@localhost boot] #

You can check the grub.cfg file.

[root@localhost grub2] # vim grub.cfg 22 23 if [x "${feature_menuentry_id}" = xy]; then 24 menuentry_id_option= "- id" 25 else 26 menuentry_id_option= "27 fi 28 29 export menuentry_id_option 30 31 if [" ${prev_saved_entry} "] Then 32 set saved_entry= "${prev_saved_entry}" 33 save_env saved_entry 34 set prev_saved_entry= 35 save_env prev_saved_entry 36 set boot_once=true 37 fi 38 39 function savedefault {40 if [- z "${boot_once}"]; then 41 saved_entry= "${chosen}" 42 save_env saved_entry 43 fi 44}

This profile needs to be entered manually. Students who are not busy can memorize it and enter it manually if they really want to try.

Delete the grub.cfg configuration file and restart the test wave

[root@localhost grub2] # rm-rf grub.cfg [root@localhost grub2] # lsdevice.map fonts grubenv i386-pc locale [root@localhost grub2] # init 6

two。 Did not enter the system, there is a manual input grub interface

Enter the first aid mode, still rely on the image file, mount the image file, and enter the esc (press it when reading the system, your eyes are sick and your hands are quick)

Select the third CD-ROM Drive

Like MBR, enter troubleshooting first, then Rescue a centos system, and select 1) continue

Let the user run the command (run this command): chroot / mnt/sysimage to enter the system in the image file

Enter the chroot / mnt/sysimage command to switch from sh to bash

Rebuild grub2

Grub2-install / dev/sda installs grub2 on the sda system disk

This instruction followed by grub2-mkconfig-o / boot/grub2/grub.cfg refers to the creation of a configuration file with the specified location / boot/grub2/grub/cfg (that is, the directory where cfg was originally deleted)

Exit save

Save complete, restart reboot

Solve the problem

Summary:

1: use the system first aid mode to enter the command character interface

2: load the system in the image and enter the bash environment

Chroot / mnt/sysimage

3: rebuild the grub menu system

Grub2-install / dev/sda

Grub2-mkconfig-o / boot/grub2/grub.cfg

Exit

Reboot

Seven: forget root password 7.1failure reason forget root user's password 7.2failure phenomenon cannot carry out management operation that requires root authority if there is no other available account, you will not be able to log in to the system 7.3.The solution will enter the first aid mode and reset the password 7.4experiment.

Steps:

1. Load mirroring system

Esc enters the mirror image and still enters the first aid mode.

two。 Directly change the password

Select 1

Chroot / mnt/sysimage/ enters the system

You can change the password by typing passwd root directly.

Remember to save exit

Reboot restart, test password

Summary:

1: load the mirror system

Chroot / mnt/sysimage

2: change the password directly

Passwd root

Enter password

8: system service control 8.1 system service control common way [root@localhost ~] # systemctl control type service name control type start: start stop: stop restart: restart (will interrupt the service, equivalent to stop first, then start) reload: reload (will reload the configuration file, will not be interrupted) status: check the service status. Nine: run level 9.1View run level runlevel commands systemctl tool 9.2 temporarily switch run level init command (command parameter is the corresponding number of run level) systemctl tool (command parameter is specific target) [root@localhost ~] # runlevel 'View run level' N5N the location at run level 'N5N represents the last run level' [root@localhost ~] # init 3 [root@localhost ~] # init 5 [root@localhost ~] # runlevel3 5

Generally speaking, 35 sectors are switched to each other.

Ten: optimize startup process 10.1 system service management tool ntsysv tool (pseudo-image interface) provides an interactive, visual window that can be run on character terminals to facilitate centralized management of multiple services systemctl tools do not provide interactive, visual window management of a single service is more efficient [root@localhost ~] # ntsysv

The one with an asterisk will boot itself.

Spaces can be added without asterisks.

The tab key can switch the interface

[root@localhost ~] # rpm-Q httpd httpd-2.4.6-90.el7.centos.x86_64 [root@localhost ~] # netstat-ntap | grep 80 'View the network status of port 80' tcp 0 192.168.139.152 httpd httpd-2.4.6 59860 117.78.24.34 root@localhost 80 TIME_WAIT-tcp 0 192.168.139.15290.el7.centos.x86_64 59862 117.78 .24.34 root@localhost 80 TIME_WAIT-[root@localhost ~] # systemctl start httpd' enable httpd' [root@localhost ~] # netstat-ntap | grep 80tcp6 0: 80: * LISTEN 4863/httpd

Setting self-startup will not affect the current running state of the tool

Eleven: startup and control of system services 11.1 ntsysv system service management tools

11.2 View the startup status of system services [root@localhost ~] # systemctl is-enabled service name 11.3 set the startup status of system services [root@localhost ~] # systemctl enable/disable service name

Systemctl enable httpd can be set for self-startup

Systemctl is-enabled httpd can detect whether the service is enabled and self-started.

[root@localhost ~] # systemctl enable httpd'is set to boot 'Created symlink from / etc/systemd/system/multi-user.target.wants/httpd.service to / usr/lib/systemd/system/httpd.service. [root@localhost ~] # systemctl is-enabled httpd' check startup status' enable [root @ localhost ~] # systemctl disable httpd 'disable self-boot' Removed symlink / etc/systemd/system/multi-user.target.wants/httpd.service. [root@localhost ~ ] # systemctl is-enabled httpd 'View again' disabled [root@localhost ~] # chkconfig-- add a tool (name) to the service in the add name system

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report