This article will be helpful if we forget the root password on Centos 7.x or RHEL 7.x. In the previous version of RHEL, we can easily break the root password, as by default SE-Linux is enabled. Also note that, RHEL has brought many special new features.
Reboot/Restart the system.
Press ‘e‘ at the grub boot menu to edit the default boot menu.
This screen looks like this for RHEL 7.x –
Press us thr down arrow key to find the line which contains similar to the ‘linux16 …… rhgb quit …..” and replace the ‘rhgb quiet’ words with ‘init=/bin/bash’. Once we edit the line just press Ctrl – x to boot the system and after editing the line, it should be looking like the below image –
Once the system gets to boot in a single mode, we needed to run the below command to confirm and rest of the root password.
Run the below command to confirm that –
# mount | grep root rootfs on / type rootfs (rw) /dev/mapper/centos_centos--linux--1-root on /sysroot type xfs (ro,relatime,attr2,inode64,noquota) :/#
To mount the root partition with Read/Write flag, we can use the below command –
# mount -o remount,rw /
To confirm that the file system is mounted to read / write mode, run the below command –
# mount | grep root rootfs on / type rootfs (rw,realtime) /dev/mapper/centos_centos--linux--1-root on /sysroot type xfs (ro,relatime,attr2,inode64,noquota) :/#
Now, we can proceed to the actual password recovery. Run the below command –
# passwd New password: Retype new password:
This step regarding SELinux enables the Linux systems to relabel the SELinux to login with our new root password. Run the below command to ensure that the SELinux conetxt so that the entire system is rellabeled after the system reboot.
# touch /.autorelabel
Run the below command to reboot the system on RHEL
# exec /sbin/init
After successful execution of the above command, we can now able to reset the password for RHEL / CentOS 7.x like this. We can restore the last root password on CentOS /RHEL 7.x Linux systems.