Root Password Recovery for RHEL, CentOS

One of our client recently stumbled upon with a query that he forgot the root password and can’t loose any data even if he reinstall OS to gain access again. Our team however suggested a password reset for this Centos 7 system. Here’s all the steps we followed him to gain back access to his machine.

OS: Centos 7 (Core 7), RHEL7

We already have IPMI Access, so we accessed the server using KVM.

  1. The first step is to reboot the system and edit the grub2 boot parameters. Once you see the menu you can press arrow keys to hold the screen. Then you must press ‘e’.
  2. Look for a line that mentions linux16 (or linuxefi if you are using UEFI bios). You may need to use the arrow keys to scroll down.  At the end of the linux16 or linuxefi line, find and replace the rhgb quiet parameters with rd.break enforcing=0
  3. Once you have edited the parameters accordingly, hit CTRL-X to start the boot process with the new parameters. The system should boot into the root system.
  4. Enter the following command to remount the sysroot filesystem as read/write: 
    mount -o remount,rw /sysroot
  5. Now we chroot into the sysroot, using the following command: chroot /sysroot
  6. We can use the passwd command to change the root password.
  7. Issue the following command to bring us back to the switch_root:/# prompt: exit
  8. Enter the following command to remount the sysroot filesystem as read-only once again: 
    mount -o remount,ro /sysroot
  9. Now we can exit the session and allow the system to reboot using the following command: exit
  10. Allow the system to boot normally and login as root using the new password that you set in step 6.

Leave a Reply

Your email address will not be published. Required fields are marked *