How to reset the root password of RHEL7 and CentOS7 system

  • 2020-05-17 07:35:22
  • OfStack

purpose

Reset the root password in RHEL7/CentOS7/Scientific Linux 7.

requirements

RHEL7 / CentOS7 / Scientific Linux 7

To guide the

The world of RHEL7 has changed, as has the way you reset the root password. Although the old method of interrupting the boot process (init=/bin/bash) is still valid, it is no longer recommended. "Systemd" USES "rd.break" to break the boot. Let's take a quick look at the process.

Boot into minimum mode

Restart the system and press e on the kernel list page before the system starts. You'll go into edit mode.

Interrupt startup process

In the kernel string - enter rd.break in the line ending linux 16 / vmlinuz-ect. Then restart Ctrl+X. The system starts into the initialization memory disk and mounts on /sysroot. You do not need to enter a password in this mode.

Remount the file system for reading and writing


switch_root:/# mount -o remount,rw /sysroot/

Make /sysroot the root directory


switch_root:/# chroot /sysroot 

The command line prompt will change slightly.

Change the root password


sh-4.2# passwd 

Load the SELinux policy


sh-4.2# load_policy -i 

Set the context type in /etc/shadow


sh-4.2# chcon -t shadow_t /etc/shadow 

Note: you can skip the last two steps by creating the autorelabel file as follows, but auto-reconstructing the volume label can take a long time.


sh-4.2# touch /.autorelabel 

For this reason, even though it's simpler, it should be a "lazy person's choice," not a recommendation.

Exit and restart

Log out and restart and log in with the new root password.


Related articles: