Recover a Redhat 7 or CentOS 7 root password

lock

There can be instances where you would had lost your root user’s passwords and thus cannot perform certain activities allowed only to the root user. The process of recovering the root password in Redhat/CentOS 7 is different than the previous versions of the OS.

The procedure to recover the Linux root user password in a Redhat 7 or CentOS 7 system is as follows:

  • Boot the system and once the Grub screen appears, press any key so that the system halts to receive further commands, instead of continuing to boot.
  • In a CentOS system, the screen should appears with lines, including one that starts with “CentOS Linux, with Linux <version>…
  • Select the above line, and press “e” to edit the configuration.
  • Find the line that looks like “linux16 /vmlinuz-<version> root=UUID…
  • Delete the words “rhgb” and “quiet” from the above mentioned line.
  • In the same line, append the following statement “init=/bin/sh” at the end of the line.
  • Press CTRL+X to save and reboot the host
  • You will get to the shell prompt, bypassing the login screen
  • Enter the following set of commands:
  • Enter “/usr/sbin/load_policy -i” to load the installed SELinux policy
  • Type “mount -o remount,rw /” to remount root file system in read/write mode
  • Issue the command “passwd” to set a password for the present user.
  • Type “mount -o remount,ro /” to remount the root filesystem in Read Only mode.
  • Enter the command “exec /sbin/init 6” to reboot the host