Debian and Ubuntu
Method 1: Using recovery mode (GRUB)
-
Open the hypervisor console and reboot the VM.
-
When the GRUB menu appears, quickly press
eto edit the boot entry. -
Find the line that starts with:
linux /boot/vmlinuz... -
Move to the end of that line and append:
rw init=/bin/bash -
Press Ctrl + X or F10 to boot.
-
The system will boot into a root shell.
-
Remount the filesystem as read/write (if needed):
mount -o remount,rw / -
Change the password:
passwd rootor for a specific user:
passwd username -
Reboot the system:
exec /sbin/initor simply:
reboot -f
After reboot, log in using the new password.
AlmaLinux and CentOS (RHEL-based systems)
Method 1: Using GRUB console access
-
Open the hypervisor console and reboot the VM.
-
At the GRUB menu, highlight the default entry and press
eto edit. -
Find the line starting with:
linux16or, on newer systems:
linux -
At the end of that line, append:
rd.breakThis boots the system into the emergency (initramfs) shell.
-
Press Ctrl + X to boot.
-
Once at the shell prompt, remount the system as writable:
mount -o remount,rw /sysroot -
Enter the chroot environment:
chroot /sysroot -
Change the password:
passwd rootor:
passwd username -
Relabel SELinux contexts (important for RHEL-based systems):
touch /.autorelabel -
Exit and reboot:
exit reboot
After the system reboots (relabeling might take a few minutes), use the new password to log in.