How To Reset Forgotten Root Password On CentOS 7 / RHEL 7

Resetting a root password on CentOS / RHEL7 Linux system has not changed much except that now we deal with SElinux and the system is now using systemd instead of initing. Here is the procedure of what needs to be done in order to reset a forgotten root password on CentOS / RHEL 7 Linux.

  1. Edit the GRUB boot menu and enter single-user mode.
  2. Remount ” / ” partition to allow read and write.
  3. Reset the root password.
  4. Make an entry for SELinux relabeling after the first reboot.
  5. Reboot the system from a single user mode.

Step 1: Edit GRUB2 boot menu

Restart the CentOS / RHEL 7 machine and once you see your GRUB boot menu press the ” e “ key to edit your default boot item.

Once you hit the ” e “key you will see a screen similar to the one below.

Scroll down, you will find a line with rhgb quiet” keywords, as highlighted in the below image.

Replace the ” rhgb quiet “ keyword with ” init=/bin/bash “ as shown below.

Once you edited the boot line, press ” CTRL + x “ to boot your CentOS / RHEL 7 system into a single-mode as shown below.

Step 2: Remount ” / ” partition.

In single-user mode, the root partition is mounted as Read Only. You can check the permissions by using the following command as shown below.

bash-4.2# mount | grep root

We can mount the root partition with Read/Write flag, by using the following commands and check with the previous command.

bash-4.2# mount -o remount,rw /

Step 3: Reset the root password.

Now we can proceed with the changing of the root password. To do this we use the “ passwd “ command as shown below.

bash-4.2# passwd root

Step 4: SELinux relabeling.

We need to relabel the SELinux context. If not, we will not able to login with the new root password. By using the following command the SELinux context for the entire system is relabeled after reboot.

bash-4.2# touch /.autorelabel

Step 5: Reboot the system.

After resetting the password and SELinux relabeling, we need to reboot the system from the single-user mode. To reboot the machine use the following command.

bash-4.2# exec /sbin/init

Finally, you have successfully reset your forgotten root password. After the reboot login with your new password.

Leave a Reply

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