What Is Grub In Linux?
GRUB in Linux is a highly customizable bootloader developed by the GNU (GNU's Not Unix) project. It stands for Grand Unified Bootloader. A bootloader is a program necessary to start an operating system when a computer boots up. The bootloader is the first program to run once the system boots.
GRUB is used as the default bootloader in most major Linux distributions.
Some features of GRUB are:
-
GRUB is one of the most flexible bootloaders available for Linux.
-
GRUB also supports booting operating systems other than Linux including Windows and macOS.
-
Kernel parameters passed on to the kernel can be edited directly from GRUB.
-
We do not need to specify the exact location of the kernel on the disk. GRUB in Linux only needs the partition name, partition number and the name of the Linux kernel's bootable image.
-
GRUB supports booting a lot of different operating systems using direct boot and chain loading boot methods.
GRUB has two main editions which are used widely:
-
GRUB legacy: This is the older edition of GRUB developed in 1995.
-
GRUB2: This is the latest version of GRUB which is used in most major Linux distributions now.
Installing GRUB on a Linux system:
It is possible to install GRUB on an existing Linux distribution or a new distribution by performing the following steps:
-
The first step is to ensure that the /boot partition exists on the system. To check if this partition exists, we can run the command: fdisk -l /dev/<partition-name>
-
If the /boot partition does not exist, it can be created by running the command: mkfs.fat -F 32 /dev/<partition-name>
-
To install GRUB on this partition, we need to run the command: grub-install /dev/<partition-name> --bootloader-id=grub
-
If the /boot partition is an EFI partition, a modified version of the previous command is required. The command, in that case, would be: grub-install --target=x86_64-efi --efi-directory=/dev/<partition-name> Here, the flag --target=x86_64-efi denotes that the target system exists on an EFI partition and the --efi-directory refers to the EFI boot partition.
Boot Loader vs Boot Manager
A boot manager is a software needed for managing the booting process in a system. When multiple bootable kernels are present on a system, the boot manager allows the user to choose different kernels to boot from.
A boot manager is also responsible for loading the actual bootloader in memory.
A popular example of a boot manager is the Windows Boot Manager used to boot Microsoft Windows.
A boot loader, on the other hand, is a small software used to boot the kernel selected by the user.
Popular examples of boot loaders include software like GRUB, systemd-boot etc.
In a system with multiple bootable kernels, the boot manager is the first program to run when the system turns on. It allows the user to select the kernel they wish to boot. Once the user has selected their choice of kernel, the flow of control is passed onto the bootloader which boots the user's kernel of choice.
Alternatives to the GRUB bootloader
While GRUB in the Linux ecosystem is the most widely used bootloader in popular Linux distributions, various other bootloaders also exist in the Linux ecosystem. Some of them are:
-
Systemd-boot: Systemd-boot is a UEFI-based bootloader which is part of the systemd init system. Unlike GRUB, systems running on the systemd init system do not need to install a separate program for systemd-boot. Installing systemd-boot is very easy and can be easily installed with the command: bootctl install. Systemd-boot also requires nearly no configuration. If a user wants to add a kernel to the boot entry, they only need to create a .conf file inside /boot/EFI/loader/entries/<kernel-name>.conf and define the name of the kernel's bootable image and the kernel parameters required by the user. Systemd-boot can also automatically detect any EFI bootable images and so, it can automatically detect and show entries for Microsoft Windows on systems with a multi-boot configuration.
-
LILO: LILO stands for Linux Loader. LILO was a popular bootloader used by major Linux distributions. It supported booting kernels from MBR partitions. LILO was less customizable compared to GRUB and only supported booting Linux kernels. LILO was configured by writing its configuration file present in /etc/lilo.conf. The file contained the locations of the boot and root partitions, the kernel image to be booted, kernel parameters, bootloader timeout etc.
-
Syslinux: Syslinux is a collection of bootloaders capable of booting from a variety of devices like CDs, network drives, Hard Disk Drives etc. Syslinux also supports booting from a lot of different file partitions like NTFS, FAT, XFS etc. Syslinux is an old bootloader and only supported booting kernel images from an MBR-based partition table. Before GRUB, syslinux was the most widely used bootloader in the Linux ecosystem.
Customizing GRUB
GRUB is a highly customizable boot manager. You can change the order of the bootable kernels, font of the boot text and even add graphics and images to the background. GRUB in Linux reads its configuration file from /boot/grub/grub.cfg.
But to change any configuration in GRUB, the user needs to edit the file at /etc/default/grub. Whenever a user makes configuration changes in this file, they need to re-create the GRUB's configuration present in /boot/grub/grub.cfg by running the command:
Some frequently used configuration options for customizing GRUB are:
-
GRUB_DEFAULT: This defines the default kernel entry in the menu that is highlighted when the system is booted. It is usually set to 0 initially but can be changed to any required kernel.
-
GRUB_TIMEOUT: This defines the countdown timer that counts down to 0 from the setup value before GRUB boots the kernel entry currently selected. This can be set to 0 as well. It ensures that the GRUB boot screen is not shown during boot and the bootloader boots the default selected kernel automatically.
-
GRUB_CMDLINE_LINUX: This configuration option defines the kernel parameters or command line arguments that are passed on to the Linux kernel before booting. Kernel parameters are command line options passed on to the kernel which enable or disable certain kernel features. Some examples of kernel parameters include quiet or splash. The quiet parameter reduces the visual output on the screen during the boot process. The splash parameter is used by a program called Plymouth to show animations during the boot process.
GRUB Customizer for Easily Customizing GRUB
The GRUB customizer application in Linux is a GUI application which makes it easier to customize the configuration for the GRUB bootloader.
On Ubuntu-based systems, GRUB customizer can be installed by running the command:
Grub customizer not only allows changing the existing configuration options present in the /etc/default/grub configuration file but also makes it easier for the users to add additional configurations like background images, change fonts on the GRUB bootloader or even change the bootloader entries.
Conclusion
-
The GRUB bootloader in Linux is powerful and highly configurable in Linux. It can be installed by running the command: grub-install /dev/<partition-name>.
-
It supports editing a boot entry's kernel parameters and other options directly from the boot loader itself.
-
GRUB reads its configuration file from /boot/grub/grub.cfg but the actual changes to the configuration are made from a file present at /etc/default/grub.