16.第12章 Linux啟動和內核管理


一.Linux 啟動流程

centos6啟動流程

1.加載BIOS的硬件信息,獲取第一個啟動設備

2.讀取第一個驅動設備MBR的引導加載程序(grub)的啟動信息

3.加載核心操作系統的核心信息,核心開始解壓縮,並嘗試驅動所有硬件設備

4.核心執行init程序,並獲取默認的運行信息

5.init程序執行/etc/rc.d/rc.sysinit文件,重新掛載根文件系統

6.啟動核心的外掛模塊

7.init執行運行的各個批處理文件(scripts)

8.init執行/etc/rc.d/rc.local

9.執行/bin/login程序,等待用戶登錄

10.登錄之后開始以shell控制主機

centos7、8啟動流程

1.UEFI或BIOS初始化,運行POST開機自檢

2.選擇啟動設備

3.引導裝載程序,centos7是grub2,加載裝載程序的配置文件:

​ /etc/grub.d/

​ /etc/default/grub

​ /boot/grub2/grub.cfg

4.加載initramfs驅動模塊

5.加載內核選項

6.內核初始化,centos7使用systemd代替init

7.執行initrd.target所有單元,包括掛載/etc/fstab

8.從initramfs根文件系統切換到磁盤根目錄

9.systemd執行默認target配置,配置文件/etc/systemd/system/default.target

10.systemd執行sysinit.target初始化系統及basic.target准備操作系統

11.systemd啟動multi-user.target下的本機與服務器服務

12.systemd執行multi-user-target下的/etc/rc.d/rc.local

13.systemd執行multi-user.target下的getty.target及登錄服務

14.systemd執行graphical需要的服務

二.修復grub第1階段故障

#破壞grub第1階段
[root@centos6 ~]# dd if=/dev/zero of=/dev/sda bs=1 count=446
446+0 records in
446+0 records out
446 bytes (446 B) copied, 0.00150234 s, 297 kB/s

[root@centos6 ~]# hexdump -C -n 512 -v /dev/sda
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000070  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000080  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000090  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000a0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000100  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000110  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000120  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000130  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000140  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000150  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000160  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000170  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000180  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000190  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001a0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 80 20  |............... |
000001c0  21 00 83 aa 28 82 00 08  00 00 00 00 20 00 00 aa  |!...(....... ...|
000001d0  29 82 83 fe ff ff 00 08  20 00 00 80 3c 0c 00 fe  |)....... ...<...|
000001e0  ff ff 83 fe ff ff 00 88  5c 0c 00 00 40 06 00 fe  |........\...@...|
000001f0  ff ff 05 fe ff ff 00 88  9c 12 00 78 63 06 55 aa  |...........xc.U.|
00000200
#前446字節被清空

[root@centos6 ~]# reboot


一階段被破壞,系統認為硬盤不能啟動,自動從光盤啟動

#chroot /mnt/sysimage 切到根目錄
#grub-install /dev/sda 修復前446字節
#sync  存盤
#exit  重啟


系統可以正常啟動

三.系統驅動文件initramfs-2.6.32-754.el6.x86_64.img文件刪除修復

[root@centos6 ~]# mv /boot/initramfs-2.6.32-754.el6.x86_64.img /data
[root@centos6 ~]# reboot


進入光盤救援模式


語言


鍵盤布局


網絡不配置


Continue


/ 根分區被掛載到/mnt/sysimage


chroot /mnt/sysimage 切換到根目錄

mkinitrd /boot/initramfs-`uname -r`.img `uname -r`  生成驅動文件
sync 存盤
exit  重啟


系統可以正常啟動

四.centos6破解root密碼

按ESC,進入菜單選擇,按a


在最后輸入1,表示臨時進入1模式,單用戶模式


不用密碼直接進來了,passwd 直接修改密碼


進入3模式


輸入新密碼 直接登錄

五.centos6設置grub密碼

[root@centos6 ~]# grub-crypt 
Password: 
Retype password: 
$6$I2yXWff69mimF3TQ$WyJ63VaFZKWr.WPIVnDmcR84oGcsl8dXUjKrk3vOiu5MR.05uQNMDejjRjSFazVmgPLucmLZo7GwDmEPNPjQN0
#生成grub密碼

[root@centos6 ~]# vim /boot/grub/grub.conf 
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sda2
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
password --encrypt $6$I2yXWff69mimF3TQ$WyJ63VaFZKWr.WPIVnDmcR84oGcsl8dXUjKrk3vOiu5MR.05uQNMDejjRjSFazVmgPLucmLZo7GwDmEPNPjQN0  	#在這里設置grub密碼
title CentOS 6 (2.6.32-754.el6.x86_64)
    root (hd0,0)
    kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=UUID=55ed4e69-a262-485e-a2c8-9df9ac044aec rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF
-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
    initrd /initramfs-2.6.32-754.el6.x86_64.img

[root@centos6 ~]# reboot


按p


輸入密碼


只有輸入密碼才可以進到這里

忘記grub密碼,直接去vim /boot/grub/grub.conf文件,把password --encrypt $6$I2yXWff69mimF3TQ$WyJ63VaFZKWr.WPIVnDmcR84oGcsl8dXUjKrk3vOiu5MR.05uQNMDejjRjSFazVmgPLucmLZo7GwDmEPNPjQN0 這行刪除就沒有grub密碼了

六.centos7、8設置錯啟動模式如何恢復

[root@centos8 ~]# systemctl get-default
multi-user.target
[root@centos8 ~]# systemctl set-default reboot.target
Removed /etc/systemd/system/default.target.
Created symlink /etc/systemd/system/default.target → /usr/lib/systemd/system/reboot.target.
[root@centos8 ~]# systemctl get-default
reboot.target
[root@centos8 ~]# reboot

現在系統會一直自動重啟


按e


在linux這行最后,加systemd.unit= multi-user.target,臨時切換模式,按ctrl+x

[root@centos8 ~]# systemctl get-default
reboot.target
[root@centos8 ~]# systemctl set-default multi-user.target

把默認啟動模式改回multi-user.target

七.破解centos7、8root密碼

按e


在linux這行最后,輸入rd.break,ctrl+x


mount -a remount,rw /sysroot 把根掛載成rw


chroot /sysroot 切根
passwd root 直接更改密碼
touch ./autorelabel 如果開啟了SELinux請執行這步,沒有就不用執行
exit 重啟

八.centos7、8設置grub密碼

[root@centos8 ~]# grub2-setpassword      #設置grub密碼
Enter password: 
Confirm password: 
[root@centos8 ~]# ll /boot/grub2
total 32
-rw-r--r--. 1 root root   64 Dec 21 17:02 device.map
drwxr-xr-x. 2 root root   25 Dec 21 17:02 fonts
-rw-r--r--. 1 root root 6605 Dec 23 14:39 grub.cfg
-rw-r--r--. 1 root root 1024 Dec 23 14:39 grubenv
drwxr-xr-x. 2 root root 8192 Dec 21 17:02 i386-pc
-rw-------  1 root root  298 Dec 23 14:54 user.cfg
[root@centos8 ~]#

[root@centos8 ~]# cat /boot/grub2/user.cfg 
GRUB2_PASSWORD=grub.pbkdf2.sha512.10000.00F10E32D3BBA86820ED6C1944C80CBEE10D48CAACD0DD70872DFBE22080507B70AABC39C4F3B0D2323E518FCE0F37F8C05884F5FE66F7663129353617772001.70F58B75EF579D9A358C003EA5466F790E6FD061BD28520EC5A44D99F36500E16BC50D8050A559A43A45DD4D060A56CD12B034BF47679AC8C2D730009DB81DF3

[root@centos8 ~]# reboot


按e


必須要輸入用戶名和密碼


才可以進入grub

[root@centos8 ~]# rm -f /boot/grub2/user.cfg
忘記密碼,刪了這個文件就可以

九./boot分區被破壞修復

[root@centos8 ~]# rm -rf /boot/*
[root@centos8 ~]# reboot


系統不能啟動


光盤引導


選1


chroot /mnt/sysimage
grub2-install /dev/sda 安裝grub


mount /dev/sr0 /mnt 掛載光盤


rpm -ivh /mnt/BaseOS/Packages/kernel-core-4.18.0.-240.e18.x86_64.rpm –force,安裝內核


grub2-mkconfig -o /boot/grub2/grub.cfg 創建grub文件
sync 存盤
exit 重啟


系統可以正常啟動


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM