How To Reset Root Password On CentOS 7

How To Reset Root Password On CentOS 7

Resetting root password in CentOS/RHEL 7 is slightly different than the older versions. The post describes the procedure to reset the lost root password.

Reboot and edit grub2

Reboot the system and press any key to stop the auto-boot from the default kernel. Press “e” after selecting the kernel line.
在这里插入图片描述

Append rd.break to kernel

When you are in edit mode, go to the line starting with linux16 and append rd.break to the end of this line.
在这里插入图片描述

This will cause the machine to boot into emergency mode, which gives you root user privileges without you having to enter a root user password. Even if the root user password hasn’t been set, this still works.

Reboot the system

Press CTLR+x after appending the rd.break to the kernel. This will reboot the system into emergency mode.
在这里插入图片描述

Remount sysroot

First we will remount the sysroot file system in read write mode and then use chroot to got into a chroot jail:

# mount -o remount,rw /sysroot
# chroot /sysroot

Reset root password

Finally, type passwd command in the command line and set the new password for root user. You might get some warnings like “password fails dictionary check” if your password is weak. You may safely ignore the warning and set the password you want.

# passwd

在这里插入图片描述

SElinux relabeling
Make sure that all unlabeled files ( including shadow file ) gets relabeled during booting. Touching the hidden file autorelabel instructs SElinux to relabel the files changed outside of its regular context, like the file /etc/shadow.

# touch /.autorelabel

sync

To flush all cache to disk, type the command:

# sync

Reboot

Type twice the exit command to leave the chroot environment and log out. The system will apply some SELinux contexts and reboot. You can now log in using your newly set root password.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值