The cart is empty

Every system administrator may encounter the need to recover a forgotten root password. This article provides a detailed guide on how to recover the root password on a CentOS 7 system, which is one of the most widely used Linux distributions for server environments.

Preparation

Before initiating the password recovery process, ensure you have physical or console access to the computer running CentOS 7. The process cannot be performed remotely via SSH or any other remote connection.

Step 1: Restart the System and Edit GRUB Parameters

a) Restart your CentOS 7 system. During the system boot, the GRUB menu will appear. If the menu doesn't appear automatically, you can force its display by pressing the "Shift" or "Esc" key, depending on your hardware.

b) Once the GRUB menu appears, select the kernel version you want to boot and press the "e" key to edit the boot parameters.

Step 2: Modify Boot Parameters

In the editing mode, navigate down to the line starting with "linux16" or "linux", depending on your configuration. In this line, locate the part ro quiet splash or just ro and change it to rw init=/sysroot/bin/sh. This temporarily changes the root directory, allowing access to the shell.

Step 3: Restart the System with Modified Parameters

Press "Ctrl+X" or "F10" to restart the system with the modified parameters. The system will boot into a root shell without requiring a password.

Step 4: Remount the Root File System

In the command prompt, enter the following command to remount the root file system as read-write:

mount -o remount,rw /sysroot

Step 5: Change Root Directory

Now, change the root directory using chroot to access the files and execute the necessary commands to change the password:

chroot /sysroot

Step 6: Change the Root Password

In the root shell, enter the passwd command to change the root password. The system will prompt you to enter a new password and confirm it.

Step 7: Update SELinux Policies

If you are using SELinux, it's important to perform a file relabel to ensure the new password functions correctly:

touch /.autorelabel

 

Step 8: Restart the System

After completing these steps, exit the chroot and restart the system using the reboot command. You should now be able to log in to the system with the new root password.

 

Recovering a forgotten root password on CentOS 7 requires physical access to the system and adherence to the precise sequence of steps outlined above. By following this guide, you can ensure your system remains secure and accessible.