The cart is empty

Forgetting the root password can be a critical issue for system administrators and users trying to gain access to important functions and files on their servers. Fortunately, CentOS 7 offers a method to reset this password, allowing you to quickly regain access to the system and continue your work. This guide will walk you through the process of resetting a forgotten root password on a CentOS 7 system, step by step.

Preparation

Before you begin, ensure you have access to your physical or virtual server and can restart the system and edit GRUB bootloader parameters.

Step 1: Restart Your System

The first step is to restart your server. During the restart process, once the GRUB menu appears, press the 'e' key to edit the boot parameters.

Step 2: Edit Boot Parameters

On the editing screen, locate the line starting with linux16 or linux (depending on your configuration). Move the cursor to the end of this line and add rd.break to interrupt the system startup process and gain access to a shell before the root filesystem is mounted. Then press Ctrl + X to start the system.

Step 3: Switch Root Filesystem to Read-Write Mode

After the restart and adding rd.break, you'll end up in an emergency shell. Here, you'll need to remount the root filesystem as read-write to be able to change the password. Use the following command:

mount -o remount,rw /sysroot

Then change the root directory to /sysroot using the command:

chroot /sysroot

Step 4: Change the root Password

Now you can change the root password using the passwd command. The system will prompt you to enter a new password. After entering and confirming the new password with the command:

passwd root

your password will be successfully changed.

Step 5: Update SELinux Information

If your system uses SELinux, it's important to update SELinux policies to match the new password. You can do this using the command:

touch /.autorelabel

 

Step 6: Restart the System

Finally, exit the shell and restart the system using the exit command twice, and then reboot the system using the reboot command. Your system should restart and allow you to log in with the new root password.

 

Resetting a forgotten root password on CentOS 7 may initially seem like a complex process, but by following the steps outlined above, you can easily and securely regain access to your system. It's important to keep security best practices in mind and regularly change and back up your passwords to prevent future access issues.