Contents

Reset Your Linux Root Password

/images/reset-linux-passwd.jpg

A system administrator can easily reset passwords for users who have forgotten theirs. But what happens if the system administrator forgets the root password? This guide will show you how to reset a lost or forgotten root password on a Red Hat-compatible system.


Overview

Please note, if the entire system hard disk has been encrypted with LUKS, you will need to provide the LUKS password when prompted. Also, this procedure is applicable to systems running systemd, which has been the default init system since Fedora 15, CentOS 7, and RHEL 7.

Interrupt the Boot Process

First, you need to interrupt the boot process. Press e or Tab on your keyboard. Use your arrow keys to move to the linux16 line:

../images/grub2.png

Using your Delete key or Backspace key, remove rhgb quiet and replace it with the following:

rd.break enforcing=0

../images/grub3.png

Setting enforcing=0 will allow you to avoid performing a complete system SELinux relabeling. Once the system is rebooted, you’ll only have to restore the correct SELinux context for the /etc/shadow file. I’ll show you how to do this too.

Press Ctrl-x to start.

Access the System via chroot

The system will now be in emergency mode. Remount the hard drive with read-write access:

# mount –o remount,rw /sysroot

Run chroot to access the system:

# chroot /sysroot

You can now change the root password:

# passwd

Type the new root password twice when prompted. If you are successful, you should see a message that reads “all authentication tokens updated successfully.”

Type exit twice to reboot the system.

Restore the System

Log in as root and restore the SELinux label to the /etc/shadow file.

# restorecon -v /etc/shadow

Turn SELinux back to enforcing mode:

# setenforce 1

Reference