Technology Encyclopedia Home >How to install GRUB in Linux?

How to install GRUB in Linux?

Installing GRUB (GRand Unified Bootloader) in Linux involves several steps, typically performed during the installation of a Linux distribution or when repairing a boot issue. GRUB is crucial as it allows you to select which operating system to boot from if you have multiple OSs installed.

Steps to Install GRUB:

  1. Boot from a Live USB/DVD: If you're repairing an existing Linux installation, start by booting your computer from a live Linux USB or DVD.

  2. Open Terminal: Once the live environment is loaded, open a terminal window.

  3. Identify Your Root Partition: Use commands like lsblk or fdisk -l to identify the root partition of your Linux installation (e.g., /dev/sda1).

  4. Mount the Root Partition:

    sudo mount /dev/sda1 /mnt
    

    Replace /dev/sda1 with your actual root partition.

  5. Mount Necessary Filesystems:

    for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt$i; done
    
  6. Chroot into Your System:

    sudo chroot /mnt
    
  7. Install GRUB:

    • For BIOS systems:
      grub-install /dev/sda
      
    • For UEFI systems:
      grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=GRUB
      
  8. Generate GRUB Configuration File:

    update-grub
    
  9. Exit Chroot and Reboot:

    exit
    sudo reboot
    

Example:

If you're using a live USB of Ubuntu to repair GRUB on your installed Ubuntu system, you would follow the steps above, ensuring you replace /dev/sda1 with the correct partition identifier for your root directory.

Cloud-Related Note:

While GRUB installation is a local task on your physical or virtual machine, if you're managing multiple Linux instances in the cloud, services like Tencent Cloud's Cloud Virtual Machine (CVM) can simplify the management of these instances, including boot and bootloader configurations. Tencent Cloud provides comprehensive management tools that can help in setting up and maintaining your Linux environments efficiently.