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.
- 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’.
- 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
- 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.
- Enter the following command to remount the sysroot filesystem as read/write:
mount -o remount,rw /sysroot
- Now we chroot into the sysroot, using the following command: chroot /sysroot
- We can use the passwd command to change the root password.
- Issue the following command to bring us back to the switch_root:/# prompt: exit
- Enter the following command to remount the sysroot filesystem as read-only once again:
mount -o remount,ro /sysroot
- Now we can exit the session and allow the system to reboot using the following command: exit
- Allow the system to boot normally and login as root using the new password that you set in step 6.