Saturday, May 9, 2015

How to Reset Root Password in CentOS 7

Resetting the root password is not as easy as what you did for CentOS /RHEL 6 or older versions. 
It is no longer possible to change the password in single-user mode because the root password is now required to operate in single-user target. 
Emergency mode also has the same issue.

To reset the password on a CentOS / RHEL 7 box follow these steps:
  • Restart the machine. 
  • On the boot screen, press E key. As you may guess E stand for Edit!
  • Use down arrow key until you reach the line starts with linux16 (if the machine has UEFI instead of legacy BIOS, you see linuxefi instead of linuxe16)
  • Delete rhgb and quiet parameters from the end of the line. (You can use Ctrl+a and Ctrl+e to jump over to the start and end of a line.)
  • Add the following parameter at the end of the linux16 line (or linuxefi): 
      init=/bin/sh
  • Now use Ctrl+x to boot the system. Then shell prompt will appear.
  • Load the initial SELinux policy into the kernel in order to preserve the SELinux context of the files that are to be modified:
# /usr/sbin/load_policy -i
  • As the Linux file system is mounted read-only, you have to remount the file system to make it writable:
# mount -o remount, rw 
  • Now it is ready to change the root password:  
# passwd root
  • Do not forget to mount the Linux file system as read only again:
# mount -o remount, ro
  • Run the following command to finish the system boot:
# exec /sbin/init
  • Do not forget to remove init=/bin/sh from the kernel parameters line and put rhgb and quite again.