When you see Grub command prompt at the boot time instead of grub menu I think that is when these commands will come handy to boot into the underlying OS. I tested it with ubuntu and pretty sure it should work with any linux distro.
Identify your boot partition:
Use the ls command to list all disks and partitions GRUB can see. Partitions are typically named like (hd0,msdos1) for MBR or (hd0,gpt1) for GPT.
grub> ls
Locate the kernel files:
Once you see a list of partitions, use ls on potential candidates to find the one containing your Linux filesystem, which will have /vmlinuz and /initrd.img (or similar) files. You can use tab completion to help.
grub> ls (hd0,gpt1)/
# Look for files like vmlinuz-*-generic and initrd.img-*-generic or vmlinuz and initrd.img at the root or inside a /boot directory.
Set the root partition:
Once you identify the correct partition (e.g., (hd0,gpt1)), set it as the root for subsequent commands.
grub> set root=(hd0,gpt1)
Load the Linux kernel:
Use the linux command to load the kernel, specifying the full path you found (use Tab for completion). Crucially, append the root= parameter (using the Linux device name, e.g., /dev/sda1 for (hd0,gpt1)) and your desired rw init=/bin/bash parameters to this line.
- Note: GRUB uses a different naming convention than Linux.
(hd0,gpt1)generally maps to/dev/sda1in Linux,(hd0,gpt2)to/dev/sda2,(hd1,gpt1)to/dev/sdb1, etc..
grub> linux /boot/vmlinuz-*-generic root=/dev/sda1 rw init=/bin/bash
# (Replace '*' with your specific kernel version, found with the 'ls' command in step 2)
Load the initial RAM disk (initrd):
Use the initrd command to load the initial RAM disk image, ensuring the version matches the kernel version you selected.
grub> initrd /boot/initrd.img-*-generic
# (Replace '*' with the same specific kernel version as above)
Boot the system:
Execute the boot command to start the system with your specified parameters.
grub> boot
Ensure changes are written to disk with sync:
The sync command forces all buffered data and metadata (including the updated password hash) to be written to the disk drive immediately. This prevents data loss during an abrupt reboot.
# After you have successfully run 'passwd [username]'
sync
Remount the filesystem cleanly (Optional but Recommended)
If the filesystem was mounted in a weird state during the recovery boot, sometimes a clean remount helps.
mount -o remount,rw /
sync # Run sync again after remount if you wish
Reboot the system properly
Once the changes are synced, do not just yank the power cord. Use a proper reboot command.
# Exit the temporary shell cleanly
exit
# The system might automatically try to shut down, or you can force a reboot:
reboot -f
# OR
exec /sbin/init # Tries to start the normal init process