Skip to content

Instantly share code, notes, and snippets.

@royki
Last active July 11, 2023 03:40
Show Gist options
  • Save royki/aa7e075e42491c005a0ee9acb61996c7 to your computer and use it in GitHub Desktop.
Save royki/aa7e075e42491c005a0ee9acb61996c7 to your computer and use it in GitHub Desktop.
GRUB Command or Recovery
  • In legacy GRUB the default is /boot/grub/menu.list
  • In GRUB2 the default is /boot/grub/grub.cfg
  • We mainly edit /etc/default/grub, which controls mainly the appearance of the GRUB menu.
  • We may also edit the scripts in /etc/grub.d/
  • These are the scripts that boot your operating systems, control external applications such as memtest & os_prober
  • theming./boot/grub/grub.cfg is built from /etc/default/grub & /etc/grub.d/*
  • update-grub
  • grub> prompt, that is the full GRUB 2 command shell.
  • GRUB2 started normally and loaded the normal.mod module other modules which are located in /boot/grub/[arch]/
  • If GRUB2 didn’t find grub.cfg file then, grub rescue> is prompted that means it couldn’t find normal.mod, so it probably couldn’t find any of the boot files.
  • grub> prompt - a lot of functionality similar to any command shell such as history and tab-completion.
  • grub rescue> mode is more limited, with no history and no tab-completion.
  • Invokes the pager, for paging long command outputs: grub> set pager=1
grub> ls
(hd0) (hd0,msdos2) (hd0,msdos1)
  • msdos - That means this system has the old-style MS-DOS partition table, rather than the new Globally Unique Identifiers partition table (GPT). If there is running GPT it will say (hd0,gpt1)
grub> ls (hd0,1)/
lost+found/ bin/ boot/ cdrom/ dev/ etc/ home/  lib/
lib64/ media/ mnt/ opt/ proc/ root/ run/ sbin/ 
srv/ sys/ tmp/ usr/ var/ vmlinuz vmlinuz.old 
initrd.img initrd.img.old

grub> cat (hd0,1)/etc/issue
Ubuntu 14.04 LTS n l

Booting From grub>

grub> set root=(hd0,1)
grub> linux /boot/vmlinuz-3.13.0-29-generic root=/dev/sda1
grub> initrd /boot/initrd.img-3.13.0-29-generic
grub> boot
  • The first line sets the partition that the root filesystem is on. The second line tells GRUB the location of the kernel you want to use. Start typing /boot/vmli, and then use tab-completion to fill in the rest. Type root=/dev/sdX to set the location of the root filesystem. If you leave this out you’ll get a kernel panic. How do you know the correct partition? hd0,1 = /dev/sda1. hd1,1 = /dev/sdb1. hd3,2 = /dev/sdd2. I think you can extrapolate the rest.

The third line sets the initrd file, which must be the same version number as the kernel.

$ ls -l /
vmlinuz -> boot/vmlinuz-3.13.0-29-generic
initrd.img -> boot/initrd.img-3.13.0-29-generic

Booting From grub-rescue>

grub rescue> set prefix=(hd0,1)/boot/grub
grub rescue> set root=(hd0,1)
grub rescue> insmod normal
grub rescue> normal
grub rescue> insmod linux
grub rescue> linux /boot/vmlinuz-3.13.0-29-generic root=/dev/sda1
grub rescue> initrd /boot/initrd.img-3.13.0-29-generic
grub rescue> boot

Making Permanent Repairs

# update-grub
# grub-install /dev/sda
  • grub-install remember you’re installing it to the boot sector of your hard drive and not to a partition, so do not use a partition number like /dev/sda1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment