CentOS 7 root密码的重置方式和CentOS 6完全不一样,CentOS 7与之前的版本6变化还是比较大的,以进入单用户模式修改root密码为例。
1、第一步,重启linux
命令表多:有(reboot 或者int 6–用得比较多。或者shutdown -r now)
扩展:平时我们用的其他如【关机:int 0–关机或者poweroff】这里只需要重启就好。
2、选择E键,进入grub界面。
在出现 2个选项的时候,选择对应的selection或者E键,去编辑我们选择的启动项。
出现grub 界面。–这是来找到你系统启动时候的启动盘,(它来管理,引导硬盘加载)
![图片[1]-CentOS7忘记root密码解决办法-CentOS学院论坛-操作系统-久趣源码交流论坛](https://plan.hulug.cn/zb_users/upload/2023/07/20230705013850168849233030205.png)
3、修改ro为rw;init=sysroot/bin/sh;
将光标定位到 linux16处,然后右边方向键,一直到 ro 将这里改为 rw 之前为只读,现在变成读写。
![图片[2]-CentOS7忘记root密码解决办法-CentOS学院论坛-操作系统-久趣源码交流论坛](https://plan.hulug.cn/zb_users/upload/2023/07/20230705013851168849233175778.png)
4、to start (按住CTRL+X)
这是原系统所在的路径:CTrl+X,出来一个界面。我们现在可以运行一些命令。
![图片[3]-CentOS7忘记root密码解决办法-CentOS学院论坛-操作系统-久趣源码交流论坛](https://plan.hulug.cn/zb_users/upload/2023/07/20230705013852168849233265328.png)
可以看到里面没有什么,ls /sysroot/boot/
![图片[4]-CentOS7忘记root密码解决办法-CentOS学院论坛-操作系统-久趣源码交流论坛](https://plan.hulug.cn/zb_users/upload/2023/07/20230705013853168849233349972.png)
因为没有挂载,只有个单独的分区。
5、切换到原系统执行:chroot /sysroot/
原来的系统是在sysroot下,要更改原来系统的密码,怎么办?
1、切换到原来系统的环境中去。用命令;chroot /sysroot/
![图片[5]-CentOS7忘记root密码解决办法-CentOS学院论坛-操作系统-久趣源码交流论坛](https://plan.hulug.cn/zb_users/upload/2023/07/20230705013854168849233469686.png)
6、更改root密码:passwd root
出现的 小方块:定义为英文:LANG=en
执行:passwd root命令,可以修改密码了。
![图片[6]-CentOS7忘记root密码解决办法-CentOS学院论坛-操作系统-久趣源码交流论坛](https://plan.hulug.cn/zb_users/upload/2023/07/20230705013854168849233420359.png)
比如改为:password1234
7、执行: touch /.autorelabel
改完成之后,还要做一步关键操作 touch /.autorelabel
然后重启该虚拟机既可以了。
![图片[7]-CentOS7忘记root密码解决办法-CentOS学院论坛-操作系统-久趣源码交流论坛](https://plan.hulug.cn/zb_users/upload/2023/07/20230705013855168849233583075.jpg)
采用你的新密码登陆试一试。
或者:
最后输入命令【touch /.autorelabel】 ,
在/目录下创建一个.autorelabel文件,而有这个文件存在,系统在重启时就会对整个文件系统进行relabeling。
以及命令【exit】退出,
以及命令【reboot】重启。
下面系统就可以登陆了。
![图片[8]-CentOS7忘记root密码解决办法-CentOS学院论坛-操作系统-久趣源码交流论坛](https://plan.hulug.cn/zb_users/upload/2023/07/20230705013856168849233650536.png)


