- 曝光:Linux企业运维实战
- 吴光科
- 392字
- 2025-02-23 04:16:00
3.9 CentOS 7密码重置
修改CentOS 7 root密码非常简单,只需登录系统,执行命令passwd按Enter键即可,但是如果忘记root密码,无法登录系统,该如何去重置root用户的密码呢?以下为重置root用户密码的方法。
(1)Reboot重启系统,系统启动进入欢迎界面,加载内核步骤时,按E键,然后选中CentOS Linux(3.10.0-327.e17.x86_64)7(Core),如图3-15所示。

图3-15 内核菜单选择界面
(2)继续按E键进入编辑模式,找到ro crashkernel=auto xxx项,将ro改成rw init=/sysroot/bin/sh,如图3-16所示。

图3-16 内核编辑界面
(3)修改后如图3-17所示。

图3-17 内核编辑界面
(4)按Ctrl+X键进入单用户模式,如图3-18所示。

图3-18 进入系统单用户模式
(5)执行命令chroot/sysroot访问系统,并使用passwd修改root密码,如图3-19所示。

图3-19 修改root用户密码
(6)更新系统信息,touch/.autorelabel,执行命令touch/.autorelabel,在/目录下创建一个.autorelabel文件,如果该文件存在,系统在重启时就会对整个文件系统进行relabeling重新标记,可以理解为对文件进行底层权限的控制和标记,如果SELinux属于disabled关闭状态则不需要执行这条命令,如图3-20所示。

图3-20 创建autorelabel文件