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