借助initramfs文件系统 访问到我们操作系统真正的/ 然后进行root密码的恢复
一:开机时内核引导项按e进入编辑模式
![](https://img-blog.csdnimg.cn/img_convert/86b22362c48c037aa1e283a9c173eaf7.png)
二:找到linux开头的那行 后面添加rd.break(微型的文件系统initramfs)
此处为rhel8.3系统
![](https://img-blog.csdnimg.cn/img_convert/7030f117ca7991204b91a7968f55588b.png)
此处为CentOS7.9系统
![](https://img-blog.csdnimg.cn/img_convert/6faf259b8c91d1bfbd1465c6ff6561e1.png)
三:ctrl+x保存进入initramfs文件系统
当前/为initramfs文件系统的/
/ sysroot才是实际操作系统的/
![](https://img-blog.csdnimg.cn/img_convert/fc00e34d97b59b22c9ea6f3b0b536215.png)
四:以读写的方式重新挂载一下/sysroot
![](https://img-blog.csdnimg.cn/img_convert/00156021894a3f79400f2da7b238a725.png)
![](https://img-blog.csdnimg.cn/img_convert/e284bb5a788e05252c1334cc0e8479b2.png)
五:将当前initramfs系统/换为/sysroot
现在使用 passwd 命令操作的还是initramfs文件系统中的/etc/shadow文件
所以要将当前/换为/sysroot
![](https://img-blog.csdnimg.cn/img_convert/b448c003f794fb73a03ba0fd75c4bd4c.png)
六:更改root密码
通过passwd root命令 对root密码重置
![](https://img-blog.csdnimg.cn/img_convert/fb1faa6c1b0b4f11aa28b34ade106611.png)
或者通过接收标准输入方式更改root密码
![](https://img-blog.csdnimg.cn/img_convert/6f7e8320f0bdfd455c99e7545922a3c0.png)
七:创建/.autorelabel隐藏文件(selinux关闭时不用做)
在重启时系统检测到/目录下有.开头的autorelabel的隐藏文件时
会重打系统selinux标签以及selinux的安全上下文
防止非原有/的用户对这个文件更改造成权限不一致的问题
selinux开启时不进行这步 root密码修改会失败
![](https://img-blog.csdnimg.cn/img_convert/ae1945e2cdb033754d29ec981ab1af20.png)
八:退出两次 自动进行重启 使用新密码登录即可
![](https://img-blog.csdnimg.cn/img_convert/c3a00ffb5329032e6578836569a5a532.png)