LinuxCommandLibrary

grub-install

Install GRUB bootloader

TLDR

Install GRUB on a BIOS system

$ sudo grub-install [path/to/device]
copy

Install GRUB on a BIOS system while specifying architecture
$ sudo grub-install --target [i386-pc] [path/to/device]
copy

Install GRUB on an UEFI system
$ sudo grub-install --efi-directory [path/to/efi_directory]
copy

Install GRUB on an UEFI system while specifying architecture and boot menu text
$ sudo grub-install --target [x86_64-efi] --efi-directory [path/to/efi_directory] --bootloader-id [GRUB]
copy

Install GRUB pre-loading specific modules
$ sudo grub-install --target [x86_64-efi] --efi-directory [path/to/efi_directory] --modules "[part_gpt part_msdos]"
copy

Display help
$ grub-install [[-?|--help]]
copy

SYNOPSIS

grub-install [OPTION...] [INSTALL_DEVICE]
or
grub-install [OPTION...] --target=TARGET --directory=DIR [INSTALL_DEVICE]

Common usage example for BIOS:
grub-install /dev/sda

Common usage example for UEFI:
grub-install --target=x86_64-efi --efi-directory=/boot/efi

PARAMETERS

--target=TARGET
    Specify the platform for which to install GRUB. Common targets include i386-pc for BIOS/CSM systems and x86_64-efi for UEFI systems.

--efi-directory=DIR
    For UEFI systems, specify the path to the EFI System Partition (ESP) mount point, e.g., /boot/efi.

--boot-directory=DIR
    Install GRUB images under the specified directory instead of the default /boot/grub. Useful for custom setups or when /boot is not on the root partition.

--bootloader-id=ID
    Assign a unique identifier for the bootloader entry in UEFI NVRAM, e.g., GRUB or Ubuntu. This ID appears in the UEFI boot menu.

--removable
    For UEFI installations, create a removable boot entry (/EFI/BOOT/BOOT[ARCH].EFI) that can be booted from USB drives or without relying on NVRAM entries.

--force
    Proceed with installation even if errors are detected.
Use with extreme caution, as it can lead to unbootable systems if underlying issues are not resolved.


INSTALL_DEVICE
    The device onto which GRUB will be installed, typically the whole disk (e.g., /dev/sda) for BIOS systems. For UEFI, this argument is often omitted or specifies the EFI System Partition itself.

DESCRIPTION

grub-install is a fundamental command-line utility used to install the GRUB (GRand Unified Bootloader) bootloader onto a specified disk device or partition. Its primary function is to write the GRUB core image and its modules to the target location, effectively making the system bootable. This command is indispensable for setting up new operating systems, recovering from bootloader corruption, or configuring complex multi-boot environments. It abstracts away the complexities of different boot modes, such as traditional BIOS/CSM and modern UEFI, and various disk layouts, ensuring the operating system kernel can be properly loaded upon system startup.

CAVEATS

grub-install is a powerful command that can make a system unbootable if used incorrectly. Always double-check the target device to avoid overwriting the wrong bootloader.
For UEFI systems, ensure the EFI System Partition (ESP) is correctly mounted and specified with --efi-directory.
When repairing GRUB from a live environment, it is highly recommended to chroot into the target system before running grub-install to ensure it operates in the correct context.

COMMON USAGE SCENARIOS

  • BIOS Systems: To install GRUB to the Master Boot Record (MBR) of the first disk, use: grub-install /dev/sda
  • UEFI Systems: First, mount your EFI System Partition (ESP), e.g., mount /dev/sdb1 /boot/efi. Then, install GRUB: grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=GRUB

USING IN A CHROOT ENVIRONMENT

When repairing or installing GRUB from a live CD/USB, it's crucial to chroot into the target system. This involves mounting the root partition, then any separate /boot or /boot/efi partitions, and binding /dev, /proc, and /sys before executing chroot. This ensures grub-install sees the correct file system layout and device paths and installs GRUB for the correct architecture and location.

HISTORY

GRUB (GRand Unified Bootloader) originated in 1999 as a replacement for the older LILO (Linux Loader). grub-install is an integral part of the GRUB project. GRUB 2, a complete rewrite, was released in 2007 and quickly became the default bootloader for many Linux distributions. grub-install evolved significantly with GRUB 2 to support modern features like UEFI, GPT partition tables, and various file systems, making it more robust and flexible than its GRUB Legacy counterpart.

SEE ALSO

Copied to clipboard