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 切到根目錄
#grun-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
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 直接更改密碼
八.centos7、8設定grub密碼
[root@centos8 ~]# grub2-setpassword
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