How to reset root password in Linux

Debian and Ubuntu

Method 1: Using recovery mode (GRUB)

  1. Open the hypervisor console and reboot the VM.

  2. When the GRUB menu appears, quickly press e to edit the boot entry.

  3. Find the line that starts with:

    linux /boot/vmlinuz...
  4. Move to the end of that line and append:

    rw init=/bin/bash
  5. Press Ctrl + X or F10 to boot.

  6. The system will boot into a root shell.

  7. Remount the filesystem as read/write (if needed):

    mount -o remount,rw /
  8. Change the password:

    passwd root

    or for a specific user:

    passwd username
  9. Reboot the system:

    exec /sbin/init

    or simply:

    reboot -f

After reboot, log in using the new password.


AlmaLinux and CentOS (RHEL-based systems)

Method 1: Using GRUB console access

  1. Open the hypervisor console and reboot the VM.

  2. At the GRUB menu, highlight the default entry and press e to edit.

  3. Find the line starting with:

    linux16

    or, on newer systems:

    linux
  4. At the end of that line, append:

    rd.break

    This boots the system into the emergency (initramfs) shell.

  5. Press Ctrl + X to boot.

  6. Once at the shell prompt, remount the system as writable:

    mount -o remount,rw /sysroot
  7. Enter the chroot environment:

    chroot /sysroot
  8. Change the password:

    passwd root

    or:

    passwd username
  9. Relabel SELinux contexts (important for RHEL-based systems):

    touch /.autorelabel
  10. Exit and reboot:

    exit reboot

After the system reboots (relabeling might take a few minutes), use the new password to log in.