grub-install

grub-install is a command-line utility used to install the GRUB bootloader onto a device, typically the Master Boot Record (MBR) or UEFI system partition of a hard drive. This command is crucial for setting up GRUB to be able to boot the operating system(s) installed on the machine.

Syntax

The basic syntax of grub-install is:

grub-install [OPTION] INSTALL_DEVICE
  • OPTION: Various options that control the behavior of the command.

  • INSTALL_DEVICE: The device where GRUB should be installed (e.g., /dev/sda).

Common Options

Here are some commonly used options with grub-install:

  • --root-directory=DIR: Specify the root directory where the installation should take place. This is useful when installing GRUB from a live CD or USB.

  • --boot-directory=DIR: Specify the boot directory where GRUB's files will be installed.

  • --recheck: Recheck the device map even if --no-floppy is specified.

  • --target=TARGET: Specify the target platform (e.g., i386-pc, x86_64-efi).

  • --force: Force GRUB installation, even if warnings are issued.

  • --no-floppy: Do not probe any floppy drives.

Examples

Basic Installation to MBR

To install GRUB on the MBR of the first hard disk:

sudo grub-install /dev/sda

This command installs GRUB on /dev/sda's MBR.

Installing to a Specific Directory

When installing GRUB from a live environment, you might need to specify the root directory:

sudo mount /dev/sda1 /mnt
sudo grub-install --root-directory=/mnt /dev/sda

In this example, GRUB is installed on the MBR of /dev/sda, but its files are placed in /mnt/boot/grub.

Specifying the Target Platform

To install GRUB for UEFI systems:

sudo grub-install --target=x86_64-efi --efi-directory=/boot/efi --boot-directory=/boot --removable

This command installs GRUB for an x86_64 UEFI system.

Troubleshooting

Rechecking Devices

If GRUB fails to install due to device map issues, use --recheck:

sudo grub-install --recheck /dev/sda

Force Installation

If you encounter warnings or errors and need to force the installation:

sudo grub-install --force /dev/sda

Post Installation

After installing GRUB, you should always update the GRUB configuration to ensure that all operating systems are correctly recognized and configured:

sudo update-grub

Recovering GRUB

If you need to recover GRUB (e.g., after installing another OS that overwrites the bootloader), you can use a live CD/USB:

  1. Boot from Live CD/USB.

  2. Mount the Root Filesystem:

    sudo mount /dev/sda1 /mnt
  3. Mount Other Necessary Filesystems:

    sudo mount --bind /dev /mnt/dev
    sudo mount --bind /proc /mnt/proc
    sudo mount --bind /sys /mnt/sys
  4. Chroot into the Mounted Filesystem:

    sudo chroot /mnt
  5. Reinstall GRUB:

    grub-install /dev/sda
    update-grub

Conclusion

The grub-install command is a powerful and essential tool for managing the GRUB bootloader. Whether setting up a new system, recovering from boot issues, or managing multi-boot configurations, understanding how to use grub-install effectively is crucial for any Linux system administrator.

Last updated