Wednesday, December 17, 2025

Ubuntu: Resetting the root password while accessing the server console in EFI boot mode

If you need to reset a forgotten password on Ubuntu OS installed in EFI mode, follow these steps:
  1. During boot, hold the "ESC" key to enter the GRUB menu. A "grub>" command prompt should appear.

  2. Execute the "ls" command. The console will display available partitions, for example:
    (hd0,gpt3) (hd0,gpt2) (hd0,gpt1) (lvm/ubuntu--vg-ubuntu--lv)

  3. Now, check these partitions with the commands:
    ls (hd0,gpt1)/
    ls (hd0,gpt2)/
    ls (hd0,gpt3)/
    ls (lvm/ubuntu--vg-ubuntu--lv)/


    You need to find:
    The System Partition - contains directories like familiar folders at its root: boot, home, usr, etc.
    The Boot Partition - contains:
    - The kernel file (vmlinuz): a file whose name starts with "vmlinuz-", e.g., "vmlinuz-6.8.0-31-generic".
    - The initrd file: a file whose name starts with "initrd.img-" and contains the same version as the kernel, e.g., "initrd.img-6.8.0-31-generic".
    Write down or remember the full names of these two files.

  4. Manually set the boot commands.
    Let's assume your boot partition is "(hd0,gpt2)" and the system partition is "(lvm/ubuntu--vg-ubuntu--lv)".

  5. Tell GRUB where to find the boot files (the boot partition):
    set root=(hd0,gpt2)

  6. Load the kernel:
    linux /vmlinuz-6.8.0-31-generic root=/dev/mapper/ubuntu--vg-ubuntu--lv rw init=/bin/bash
    where
    root=/dev/mapper/ubuntu--vg-ubuntu--lv - the path to the LVM volume as the root partition;
    rw - mount for writing (ro - read-only);
    init=/bin/bash - boot directly into the root shell.

  7. Load the initrd image (necessary for LVM to work):
    initrd /initrd.img-6.8.0-31-generic

  8. Start the boot process:
    boot
    After the OS image loads, an input prompt in the format "root@(none):/#" will appear.

  9. Change the password for "root" or another user:
    passwd root

  10. Perform a reboot:
    reboot -f

If boot errors occur and the "(initramfs)" input prompt appears instead of "root@(none):/#", try mounting the filesystem manually:

  1. Create a mount point:
    mkdir /rootfs

  2. Mount your LVM volume:
    mount /dev/mapper/ubuntu--vg-ubuntu--lv /rootfs

  3. Check the mount:
    ls /rootfs
    You should see the contents of the root filesystem (/): folders like bin, home, usr, etc, etc.

  4. Use the chroot command to make the mounted partition the root for subsequent commands:
    chroot /rootfs

  5. Change the password for "root" or another user:
    passwd root
    Enter the new password twice.

  6. Reboot the system to normal mode:

    Exit chroot:
    exit

    Unmount the partition:
    umount /rootfs

    Perform a reboot:
    reboot -f

Note: Make sure to use the exact kernel version and LVM path found on your system when entering the commands.

No comments:

Post a Comment