实验:在nodea 上进行操作
磁盘引导文件被删除后怎么修复 mbr数据丢失
添加光驱镜像 (在哪个机子做实验,就在哪个机子上添加光驱镜像)
注意:添加光驱镜像之前须关闭系统
![]()
[root@westoslinux Desktop]# fdisk -l 查看系统引导文件的位置
Disk /dev/vda: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000
磁盘引导文件是在/dev/vda上
Device Boot Start End Sectors Size Id Type
/dev/vda1 * 2048 2099199 2097152 1G 83 Linux
[root@westoslinux Desktop]# dd if=/dev/zero of=/dev/vda bs=446 count=1 清空系统/dev/vda上的mbr数据
[root@westoslinux Desktop]# reboot
恢复方式
用光盘进入挽救模式 将系统默认从光驱启动
![]()
chroot /mnt/sysimage
grub2-install /dev/vda 重新安装grub2到系统硬盘上
exit
exit
修复完毕后换成磁盘进入
关闭linux内核中提供的强制访问控制系统 加快开机速度
[root@westoslinux Desktop]# vim /etc/sysconfig/selinux
文件引导阶段的原理及修复
boot/grub2/grub.cfg 主引导文件 文件丢失后系统起不来
/boot/loader/entries/xxxxxxxx.conf 子引导文件
在系统启动的过程中,如果引导文件出问题,可以手动引导,但是需要知道/分区所在的系统磁盘在哪里 boot分区所在的系统磁盘在哪里
/dev/vda3 17814528 3338772 14475756 19% /
/dev/vda1 1038336 173220 865116 17% /boot
[root@westoslinux ~]# cd /boot/grub2/
[root@westoslinux grub2]# ls
device.map fonts grub.cfg grubenv i386-pc
rm -fr /boot/grub2/grub.cfg 删除丢失
grub2-mkconfig > /boot/grub2/grub.cfg 恢复
rm -fr /boot/grub2/grub.cfg 丢失后重启系统
reboot
【grub > set root=‘hd0,msdos1‘ 指定boot分区所在的磁盘位置
(第一块磁盘)(第一块分区)
grub > linux16 /vmlinuz-4.18.0-193.el8.x86_64 ro root=/dev/vda3 磁盘/分区的位置
grub > initrd16 /initramfs-4.18.0-193.el8.x86_64.img boot分区
grub > boot 启动】
恢复删掉的文件
grub2-mkconfig > /boot/grub2/grub.cfg 恢复
reboot 重启 系统恢复正常
开机时系统显示的两个标题 子引导文件
企业8
[root@westoslinux grub2]# cd /boot/loader/entries/
[root@westoslinux entries]# ls
8bf8757aa7ac4d73b173c9970462643f-0-rescue.conf
8bf8757aa7ac4d73b173c9970462643f-4.18.0-193.el8.x86_64.conf 如果删掉两个 系统故障启动失败
rm -fr /boot/loader/entries/* 删除之后用安装命令安装
kernel-install add 4.18.0-193.el8.x86_64 /lib/modules/4.18.0-193.el8.x86_64/vmlinuz
[root@westoslinux entries]# ls /boot/loader/entries/ 安装完成后文件恢复
8bf8757aa7ac4d73b173c9970462643f-0-rescue.conf
8bf8757aa7ac4d73b173c9970462643f-4.18.0-193.el8.x86_64.conf
rm -fr /boot/loader/entries/*
reboot 如果继续删除 重启系统 需要手动引导
【grub > set root= ’hd0,msdos1’
grub > linux16 /vmlinuz-4.18.0-193.el8.x86_64 ro root=/dev/vda3
grub > initrd16 /initramfs-4.18.0-193.el8.x86_64.img
grub > boot】
kernel-install add 4.18.0-193.el8.x86_64 /lib/modules/4.18.0-193.el8.x86_64/vmlinuz
引导需要读取的文件有两个 1个是主引导文件 另一个是启动所看到的引导程序所使用到的启动文件
5. 内核加载加载内核作用 :/ boot / vmlinuz - $(uname - r)系统初始化硬件设备只读挂在 / 设备内核引导文件丢失修复
[root@westoslinux entries]# ll /boot/vmlinuz-4.18.0-193.el8.x86_64
-rwxr-xr-x. 1 root root 8913760 Mar 27 2020 /boot/vmlinuz-4.18.0-193.el8.x86_64
[root@westoslinux entries]# rm -fr /boot/vmlinuz-4.18.0-193.el8.x86_64
[root@westoslinux entries]# cp /lib/modules/4.18.0-193.el8.x86_64/vmlinuz /boot/vmlinuz-4.18.0-193.el8.x86_64 恢复 复制系统中usr下的内核文件到/boot中即可
[root@westoslinux entries]#reboot 重启之后仍然故障 手动驱动不行需借助光驱启动
【 将光驱启动调到第一 选择Troubleshooting
Rescue a Red Hat Enterprise Linux system
1 Continue
[sh-4.4# chroot /mnt/sysimage
bash-4.4# cp /lib/modules/4.18.0-193.el8.x86_64/vmlinuz /boot/vmlinuz-$(uname - r)$括号内的先执行
bash-4.4# ls /boot/ (第2列的最后一个)
bash-4.4# exit] 重新调整系统启动用到的设备】
6. 系统初始化镜像/ boot / initramfs - 4.18.0 - 80. el8.x86_64.img加载系统时钟加载 selinux加载系统主机信息加载 / etc / fstab 文件中磁盘挂在策略加载磁盘配额初始化系统程序开启开机启动服务开启虚拟控制台开启图形系统初始化镜性文件 丢失修复
[root@westoslinux ~]# cd /boot/
[root@westoslinux boot]# ls
[root@westoslinux boot]# rm -fr initramfs-4.18.0-193.el8.x86_64.img 文件丢失
[root@westoslinux boot]# = mkinitrd /boot/initramfs-$(uname -r).img $(uname -r) 恢复
[root@westoslinux boot]# rm -fr initramfs-4.18.0-193.el8.x86_64.img 文件丢失
[root@westoslinux boot]# reboot
【 光驱启动 将光驱启动调到第一 选择Troubleshooting
Rescue a Red Hat Enterprise Linux system
1 Continue
[sh-4.4# chroot /mnt/sysimage
bash-4.4# df
bash-4.4# mkinitrd /boot/initramfs-$(uname -r).img $(uname -r)
bash-4.4# ls /boot/
bash-4.4# exit
bash-4.4# exit 调整硬盘为第一】
7.系统启动级别
0 关机1 单用户2 无图形网络模式3 无图形网络模式4 无图形网络模式5 有图形网络模式6 重启系统启动级别
[root@westoslinux system]# cd /etc/systemd/system/ 服务引导阶段
[root@westoslinux system]# cd multi-user.target.wants 所有服务启动脚本的链接
[root@westoslinux multi-user.target.wants]# ls -l sshd.service
lrwxrwxrwx 1 root root 36 Oct 29 13:49 sshd.service -> /usr/lib/systemd/system/sshd.service
[root@westoslinux multi-user.target.wants]# systemctl status sshd 查看sshd服务状态开启
[root@westoslinux multi-user.target.wants]# systemctl disable sshd.service
Removed /etc/systemd/system/multi-user.target.wants/sshd.service.
[root@westoslinux multi-user.target.wants]# systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; 服务在开机的时侯是关闭状态
[root@westoslinux multi-user.target.wants]# ll sshd.service
ls: cannot access 'sshd.service': No such file or directory
[root@westoslinux multi-user.target.wants]# systemctl enable sshd 开机的时候文件作为启动
Created symlink /etc/systemd/system/multi-user.target.wants/sshd.service → /usr/lib/systemd/system/sshd.service. 系统启动原理创建链接
[root@westoslinux multi-user.target.wants]# ll sshd.service
lrwxrwxrwx 1 root root 36 Oct 29 14:13 sshd.service -> /usr/lib/systemd/system/sshd.service
[root@westoslinux system]# ll default.target
lrwxrwxrwx 1 root root 40 Oct 29 13:56 default.target -> /usr/lib/systemd/system/graphical.target
default.target决定了系统开机时候运行级别
[root@westoslinux system]# systemctl set-default poweroff.target
Removed /etc/systemd/system/default.target.
Created symlink /etc/systemd/system/default.target → /usr/lib/systemd/system/poweroff.target.
[root@westoslinux system]# ll default.target
lrwxrwxrwx 1 root root 39 Oct 29 14:18 default.target -> /usr/lib/systemd/system/poweroff.target
[root@westoslinux system]# reboot 由于某些原因链接链到了poweroff 系统在开机的时候会关闭
恢复
e 在开机选择界面按<e>进入到引导文件编辑模式
5 为5有图形的网络模式 在内核加载选项最后加入启动级别5
ctrl x 启动
系统开机后重启还是会关闭 所以需要重新手动设定启动级别
[root@westoslinux Desktop]# systemctl set-default graphical.target
Removed /etc/systemd/system/default.target.
Created symlink /etc/systemd/system/default.target → /usr/lib/systemd/system/graphical.target.
8.超级用户密码忘记
恢复 :1. 在开机选择界面按 < e > 进入到引导文件编辑模式2. 在内核加载选项最后删除到 ro 选项3. 更改 ro ----> rw4. 加入 rd.break 参数终止系统启动进入单用户模式5. ctrl + x 启动6. chroot / sysroot7. echo lee | passwd -- stdin root8. touch / .autorelabel ## 当 / .autorelabel 文件存在 selinux 会重新初始化 cp kernelcore - 4.18.0 - 80. el8.x86_64.rpm / mnt /exitexit[root@westoslinux Desktop]# reboot
e 编辑
rw rd.break 表示在内核启动之后把引导程序打断 得到单用户模式 可以直接看到超级用户
ctrl x 启动
switch_root:# 单用户模式 chroot /sysroot/ 切换到真实的shell
sh-4.4# echo westos | passwd --stdin root 修改密码
sh-4.4# touch /.autorelabel(自动重新设置标签) 如果selinux开启,建立.autorelabel文件,对selinux重新刷新 默认selinux拒绝更改密码 如果没有建立 否则密码更改无效
exit
exit
![]()