Tuesday, 13 January 2015

Forgot root password of your RHEL 7 server. How to recover it ?

Let us discuss about a small topic. On RHEL 6.0 and earlier, an administrator could boot the system into runlevel 1, and be presented with  a root prompt. The closest analog to runlevel 1  on RHEL 7.0 machine are the rescue.target and emergency.target both of which require a root password to log in.

On RHEL 7.0, it is possible to have the scripts that run from the initramfs pause at certain points, provide root shell,and then continue to when the shell exits. this is mostly meant for debugging however it can also be used to recover the root password.

Here is the steps to recover the password :

1. Reboot the server.
2. Interrupt the boot loader countdown by pressing any key.
bootloader countdown
3. Press "e" to edit the selected entry.
4. move the cursor to the kernel command line (the line starts with linux16.)
5. Append rd.break (this will break just before control is handed from initramfs to the actual system)
rd.break
If you are recovering the root password of Virtual machine ( KVM) then you need to add serial console "console=ttyS0" along with "rd.break" otherwise we will not get the # prompt to change the password.
vm root password
6. Press CNTL+x to boot with changes.
At this point, a root shell will be presented, with the root filesystem for the actual system will be mounted read-only  on "/sysroot" .
recover shellFrom this shell we need to recover the password.
7. Remount the /sysroot as read-write mode.
# mount -o remount,rw /sysrootsysroot rw mode
8. Switch to chroot, where /sysroot is treated as the root of the filesystem tree.
# chroot /sysroot
chroot
9. Set a new password . Here i resetted my server password as redhat.
# echo "redhat" | passwd --stdin root
root password reset
10. Make sure that all the unlabeled files ( including /etc/shadow at this point) get labeled during boot.
#touch /.autorelabel
selinux updating

11.  exit twice. First will exit from chroot and second will from  the initramfs .
12. At this point, Server will reboot and now you could login with the new password set to root.

Hope you find this post useful ! if you have any feedback/queries post in the below comment box.!

1 comment:

  1. the best linux blog..appreciate your hard work!!

    ReplyDelete