To recover the root password on a linux system if you do not have it have misplaced it depends on the major release of the OS. This guide will cover both CentOS 6(and prior releases) and CentOS 7.
Recover a root password on CentOS 6 or older
You will need to boot into single user mode. When the system is powering on you will need to enter the grub configuration and append single to the end of the kernel line
Once you are in the grub configuration editor you will need to type ‘e’ to edit the appropriate kernel. It will take you to another screen. Go down to the line that begins with kernel, you will need to append the following
single
Once you have done that you you can go ahead and type ‘b’ to boot the kernel.
The OS will reboot into single user mode. This will give you a password less prompt which will allow you to reset the root password by typing the following:
passwd
Once you have entered the new password you can go ahead and reboot the machine which will then load into the default runlevel with the updated password.
Recover a root password on CentOS 7
To recover a root password on CentOS 7, you will again need to enter the grub menu and type ‘e’ to edit the appropriate kernel. Go to the line that begins with kernel and append the following to the end of the line
init=sysroot/bin/sh
Then type ‘b’ to boot this kernel. It will boot up and you will need to mount the file system in a read-write state:
mount -o remount,rw /sysroot
You will then need to chroot to the mounted filesystem
chroot /sysroot
Now you can successfully update the root password by typing
passwd
If you have SElinux enabled you will need to enable auto-relabel
touch /.autorelabel
Now you can go ahead and reboot the system and it should load with the newly set password.