🌐 🇵🇱 Polski · 🇬🇧 EN
Human memory is good, but sometimes short. It may also happen that there is a need to log in to a system that was installed by someone else and that person is no longer available. The reasons may be different and are not important here. What is important is the method that allows logging in to the CentOS 7/RedHat 7 system when the password is not known.

The method of changing the root password in CentOS/RedHat environments has not changed for many years. The general idea of recovery has also remained the same as in older versions. However, it should be remembered that currently in version 7, the init environment has been replaced by systemd, and we also have to deal with SELinux.
The password change is performed in 5 steps:
- The GRUB2 bootloader entries must be edited to access single user mode.
- Mounting the / partition to enable reading and writing (changing from "ro" to "rw")
- Changing the root password
- Setting SELinux to relabel the file system on boot
- Restarting the system
STEP 1 – Editing GRUB2 and entering single mode
To access the system when the root password is not known, you need to restart the host and then edit the GRUB2 bootloader by pressing "e".


In the "rhgb quiet" entry, type: init=/bin/bash.

After editing, press CTRL+x to boot the system in single mode.

STEP 2 – Mounting the root partition in Read&Write mode
After entering single mode, the root partition must be mounted to enable writing changes. This is done using the mount command.
# mount | grep root
# mount -o remount,rw /
# mount | grep root
As a result, we should get:

STEP 3 – Changing the root password
Now you can easily set a new password for root using the passwd command.
# passwd
The screen will look like this:

STEP 4 – SELinux relabeling
In this step, you need to make sure that the SELinux labels are updated. This is done by creating a hidden file. This will cause SELinux to relabel the file system on the next boot.
# touch /.autorelabel

STEP 5 – Restarting the system
After completing the above steps, all that remains is to restart the system and log in with the new root password.
# exec /sbin/init
After restarting, you can log in to the root account with the new password.

Comments