centos7/8破解root密码以及启动过程的故障修复

1. 破解centos7和8的root密码

方法1:

启动时任意键暂停启动
按e键进入编辑模式
将光标移动到linux 开始的行,添加内核参数rd.break
按ctrl-x启动
mount -o remount,rw /sysroot
chroot /sysroot
passwd root

#如果SELinux是启用的,才需要执行下面操作,如果没有启动,不需要执行
touch /.autorelabel

exit
reboot

方法2:

启动时任意键暂停启动
按e键进入编辑模式
将光标移动到linux开始的行,改为rw init=/sysroot/bin/sh
按ctrl-x启动
chroot /sysroot
passwd root

#如果SELinux是启用的,才需要执行下面操作,如果没有启动,不需要执行
touch /.autorelabel

exit
reboot

2. 实现GRUB2安全

#添加grub密码
[root@centos8 ~]#grub2-setpassword
Enter password:
Confirm password:
[root@centos8 ~]#ls -l /boot/grub2/
total 32
drwxr-xr-x 2 root root 4096 Jan 19 15:17 fonts
-rw-r--r-- 1 root root 5101 Jan 19 15:18 grub.cfg
-rw-r--r-- 1 root root 1024 Jan 19 15:18 grubenv
drwxr-xr-x 2 root root 12288 Jan 19 15:17 i386-pc
-rw------- 1 root root 298 Jan 19 18:20 user.cfg
[root@centos8 ~]#ls -l /boot/grub2/user.cfg
-rw------- 1 root root 298 Jan 19 18:20 /boot/grub2/user.cfg
[root@centos8 ~]#cat /boot/grub2/user.cfg
GRUB2_PASSWORD=grub.pbkdf2.sha512.10000.60AAA29A65F4DC77E8861EF25BDE2034C9B30CE1
E07EE688D7F30460E7E87E7356B0893A6DFFB250B27D2EB9D3ED3E9207199C494D7882E2E8C772C8
2E2DDB7A.5E42FD69FA04293DECD68F077E83875A8E4572A7FBB89BA9F161B15EAFE54FBA963FE5D
52E16764944823396231803E5118DA1D9CAF3EB73C175A7D7A3682A90

#清空grub密码
[root@centos8 ~]#cat /dev/null > /boot/grub2/user.cfg
或者
#删除文件
[root@centos8 ~]#rm -f /boot/grub2/user.cfg

3. 修复grub2

GRUB2:CentOS 7,8及ubuntu1804都使用
引导提示时可以使用命令行界面,可从文件系统引导
主要配置文件:/boot/grub2/grub.cfg
修复配置文件:grub2-mkconfig > /boot/grub2/grub.cfg
修复grub

grub2-install /dev/sda #BIOS环境
grub2-install	#UEFI环境

范例:调整默认启动内核

[root@centos8 ~]#cat /boot/grub2/grubenv 
# GRUB Environment Block
saved_entry=0
kernelopts=root=/dev/mapper/cl-root ro crashkernel=auto resume=/dev/mapper/cl-swap rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet net.ifnames=0 
boot_success=0
boot_indeterminate=0
####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################[root@centos8 ~]#
[root@centos8 ~]#ls /boot/loader/entries/
5b85fc7444b240a992c42ce2a9f65db5-0-rescue.conf
5b85fc7444b240a992c42ce2a9f65db5-4.18.0-147.el8.x86_64.conf
5b85fc7444b240a992c42ce2a9f65db5-5.6.12-wanglinux-6.6.6.conf

#以下命令是修改 /boot/grub2/grubenv 实现
[root@centos8 ~]#grub2-set-default 1
#或者
[root@centos8 ~]#vim /etc/default/grub
GRUB_DEFAULT=1

在这里插入图片描述

4. centos7,8破坏MBR后进行恢复

dd if=/dev/zero of=/dev/sda bs=1 count=446
光盘进入救援模式
chroot /mnt/sysimage
grub2-install /dev/sda
sync
sync
exit
reboot

5. centos7,8删除/boot/grub2/*所有内容进行恢复

在这里插入图片描述

光盘进入救援模式
chroot /mnt/sysimage
grub2-install /dev/sda
grub2-mkconfig -o /boot/grub2/grub.cfg
sync
sync
exit
reboot

6. centos7,8删除/boot/下所有文件后进行恢复

1.光盘救援模式下安装grub2
特别说明:CentOS8必须先grub,再安装kernel,否则安装kernel-core时会提示grub出错

chroot /mnt/sysimage
mount /dev/sr0 /mnt
grub2-install /dev/sda

2.安装kernel

#centos 7
rpm –ivh /mnt/Packages/kernel-3.10.0-1062.el7.x86_64.rpm --force

#centos 8
rpm -ivh /mnt/BaseOS/Packages/kernel-core-4.18.0-147.el8.x86_64.rpm --force

3修复grub配置文件
生成grub2.cfg文件
grub2-mkconfig –o /boot/grub2/grub.cfg

4 退出重启
sync
sync
exit
reboot
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值