How to reset a lost root password in Centos 7 or Redhat 7

How do I reset my root password which I have forgotten in Linux?

In this article, I will show you how to reset a lost root password in Centos 7 or RedHat 7.

1.) Interrupt the Boot Loader

Start or if already started then reboot your system. Once the boot loader menu appears on the screen you can press any key to interrupt the countdown.
boot loader

2.) Edit the Default Boot Loader

Now you need to edit the default bootloader in the memory so that boot process can be aborted. This need to be done after all the file systems have been mounted but before the control is handed over to systemd.

  • Highlight the default boot loader entry.
  • Press e for editing the current entry.
  • Navigate to the line that starts with linux16
  • Move the cursor to the end of line which can be done by pressing END key
  • Append rd.break to the end of the line
  • Press Ctrl+x to boot the system using the modified configuration

edit bootloader append rd

3.) Now we need to remount /sysroot file systemd read-write at the switch_root prompt.

switch_root:/# mount -oremount, rw /sysroot

mount oremount

4.) Use chroot command to go into chroot jail at /sysroot

switch_root:/# chroot /sysroot

chroot sysroot

5.) Change the root password

In this example, I have changed my root password to Red700serV

sh-4.2# echo Red700serV | passwd --stdin root

Root Password Changed

6.) Perform an automated full SELinux relabel after boot

This step is necessary because in this example passwd tool has recreated the /etc/shadow file without creating an SELinux context for it.

sh-4.2# touch /.autorelabel

autorelabled SELinux

7.) Type exit twice on the terminal which will continue booting your operating system as normal.

During booting the system runs an automated SELinux relabel and then, reboot itself.

Video Tutorial

If you find this post helpful, please share it on your social networks. If you have any questions? Leave a comment below, or you can contact me using the contact us page.

Add Comment