Bookmark this page

Resetting the root Password

Objectives

  • Reset the root password.

Resetting the root Password

When the root password for a system is lost or forgotten, an authorized administrator can reset it. Some methods can work remotely, such as through an SSH connection, while others require physical console access.

If any user is still logged in as the root account on an unlocked terminal, then use that active session to change the password. Similarly, use any accessible account that has sufficient sudo shell or passwd command access to reset the root password.

A more complex method is to manually edit the /etc/shadow file by copying in a known password hash from any account that has sudo text editor access, or by editing the virtual machine's disk image with the guestfish command.

Rescue Mode

If the previous methods are not available or unsuccessful, then rescue mode is an alternative. An administrator with physical system access can use the anaconda installation program's rescue mode to boot from installation media or an external media device, and then use that access to change the root password. This procedure requires that either the system's disks are not encrypted or that the encryption password is known. If the firmware password is configured and known, then you can boot from alternative devices on both BIOS and UEFI firmware. The anaconda rescue mode is accessed by booting from a Red Hat Enterprise Linux 8 boot media device, such as a USB installation device, and selecting the Troubleshooting option in the boot menu.

Resetting the root Password without External Media

When the external boot media method is not available or unsuccessful, an administrator can use the systemd startup sequence to halt the initial ramdisk (initramfs) startup sequence. This method requires physical console access, or access through a remote management card or KVM switch, and knowledge of passwords for disk encryption and the boot loader, if configured.

This method for resetting a root password consists of these steps:

  1. Reboot the system and interrupt the boot loader timer by pressing any key except Enter.

  2. Find the entry that is normally booted, and change it so that it halts execution during the initial ramdisk startup sequence.

    1. Use the cursor keys to highlight the entry that would normally be booted, and press e.

    2. Use the cursor keys to move to the line that has the kernel and the kernel arguments. This line normally starts with linux.

    3. Move the cursor to the end of the line by pressing Ctrl+e, and add rd.break.

      Note

      Classroom virtual machine images have a console= kernel setting for a serial console. If you are not using a serial console, then remove this setting to force the initial ramdisk to use the virtual console. If you keep this setting, then control passes to a serial console that you cannot see, and a black screen appears.

    4. Press Ctrl+x to boot with the modified parameters.

  3. The system now boots, but exits the process during initial ramdisk execution. If a prompt does not appear shortly, press Enter to see whether the prompt is obscured by kernel output.

  4. Remount the root file system with read and write capabilities. The file system is currently mounted on the /sysroot directory mount point.

    switch_root:/# mount -o remount,rw /sysroot
  5. Change the working root directory to /sysroot.

    switch_root:/# chroot /sysroot
  6. Reset the root password to a known value.

    sh-4.2# echo "root:newpassword" | chpasswd
  7. Force SELinux to relabel during the next boot.

    sh-4.2# touch /.autorelabel

    Important

    The SELinux relabel in this method is required. SELinux detects whether an alternative access sequence occurred because the SELinux contexts are no longer present on the modified files. To trust the system again, SELinux will not boot until all files are properly relabeled.

  8. Reboot the system by exiting from the chroot environment and from the switch_root prompt by typing exit twice.

  9. Verify that the root password access is reset by either logging in as root or by logging in as a non-privileged user and switching to root with any method that requires entering the root password.

Revision: rh342-8.4-6dd89bd