What are GRUB Files in Linux? [In-Depth Tutorial]


Written by - Deepak Prasad

What is GRUB in Linux? What does it do?

Grub files, also known as GRand Unified Bootloader , are essential components in the Linux operating system that facilitate the booting process. Grub files are responsible for loading and launching the Linux kernel, which is the core of the operating system.

When a computer is powered on, the BIOS (Basic Input/Output System) loads the Grub bootloader, which then displays a menu of available operating systems or boot options. From there, the user can select the desired operating system, and Grub loads the corresponding kernel, initiating the boot process.

Grub files are located in the /boot/grub/ directory of the Linux file system and consist of various configuration files and modules that determine the boot process's behavior. These files are editable and can be customized to suit the user's requirements. Advanced users can modify the configuration files to include additional boot options or customize the boot menu's appearance.

Grub files play a crucial role in the Linux operating system's booting process, and it is essential to have a basic understanding of them for any Linux user or system administrator. Understanding how Grub works and how to modify its configuration can help users troubleshoot booting issues and optimize their Linux systems' performance.

 

Journey from LILO → GRUB → GRUB2

  • LILO (Linux Loader) was the default bootloader for Linux systems for many years.
  • GRUB (GNU GRand Unified Bootloader) was developed as a new bootloader for Linux systems in the mid-1990s.
  • GRUB Legacy (the first version of GRUB) was released in 1995 and introduced many advanced features, such as a configuration file, support for multiple filesystems, and a modular architecture.
  • GRUB Legacy remained the default bootloader for most Linux distributions for many years, but it had its own limitations and design issues.
  • GRUB2 development began in 2005, with a focus on modular architecture, improved security, and support for modern features such as scripting, multiboot, and internationalization.
  • GRUB2 replaced GRUB Legacy as the default bootloader for most Linux distributions over time.
  • GRUB2 has continued to be updated and improved, with features such as Secure Boot and cryptographic signatures added to provide enhanced security for Linux systems.

GRUB2 (GNU GRand Unified Bootloader version 2) did not directly evolve from LILO (Linux Loader), but rather it was developed as a completely new bootloader with a different design philosophy and set of features.

LILO was the default bootloader for Linux systems for many years, and it was a simple and reliable bootloader that served its purpose well. However, as Linux systems became more complex and began to support multiple operating systems and boot configurations, LILO became less well-suited to the task. In particular, LILO lacked support for advanced features such as scripting, modular architecture, and multiboot support.

To address these limitations, GRUB was developed as a new bootloader for Linux systems. The first version of GRUB (GRUB Legacy) was released in 1995 and was designed to be more flexible and configurable than LILO. GRUB Legacy introduced many advanced features, such as a configuration file, support for multiple filesystems, and a modular architecture.

However, over time, it became clear that GRUB Legacy had its own limitations and design issues. In particular, GRUB Legacy had a monolithic design that made it difficult to maintain and update, and it lacked support for many modern features such as Secure Boot.

To address these issues, development began on GRUB2 in 2005. GRUB2 was designed with a modular architecture that made it easier to maintain and update, and it included many advanced features such as scripting support, multiboot support, and improved internationalization. GRUB2 was also designed to be more secure than GRUB Legacy, with support for features such as Secure Boot and cryptographic signatures.

 

How GRUB2 is different from GRUB?

GRUB2 (GNU GRand Unified Bootloader version 2) is the next generation of the GRUB bootloader, and it has several key differences compared to the original GRUB (version 1). Here are some of the main differences:

  1. Configurability: GRUB2 is much more configurable and flexible than GRUB, allowing for more advanced boot configurations and customization options.
  2. Multiboot support: GRUB2 supports more types of operating systems and boot configurations than GRUB, including booting from disk images, network booting, and chain-loading other bootloaders.
  3. Scripting support: GRUB2 includes a built-in scripting language (GRUB2 Scripting Language or GSL) that allows for more advanced configuration and customization options.
  4. Modular architecture: GRUB2 is designed with a modular architecture, allowing for easier maintenance and development of the bootloader. This also makes it easier to add new features and functionality to GRUB2.
  5. Better internationalization: GRUB2 includes improved support for non-English languages and non-Latin scripts, making it more accessible to users around the world.
  6. Improved security: GRUB2 includes support for Secure Boot, which helps protect against malware and other threats during the boot process.

 

GRUB File Location

The location of the GRUB2 configuration file can vary depending on the Linux distribution and the type of BIOS used. We normally have either Legacy BIOS or UEFI BIOS:

For legacy BIOS systems, the GRUB2 configuration file is typically located at /boot/grub/grub.cfg. This file is automatically generated by the update-grub command when new kernels are installed, and should not be edited manually. Instead, custom settings can be added to the /etc/default/grub file or to files in the /etc/grub.d/ directory.

  • Ubuntu: /boot/grub/grub.cfg
  • Debian: /boot/grub/grub.cfg
  • CentOS/RHEL: /boot/grub2/grub.cfg

For UEFI systems, the location of the GRUB2 configuration file can vary depending on the distribution and the UEFI firmware implementation. On some systems, the configuration file may be located at /boot/efi/EFI/<distribution>/grub.cfg, while on others it may be located at /boot/grub/grub.cfg or /boot/grub2/grub.cfg.

  • Ubuntu: /boot/efi/EFI/ubuntu/grub.cfg
  • Debian: /boot/efi/EFI/debian/grub.cfg
  • CentOS: /boot/efi/EFI/centos/grubx64.efi
  • RHEL: /boot/efi/EFI/redhat/grubx64.efi

Note that in some cases, the Grub2 configuration file may be located in a different directory or have a different name if it has been customized or if a different bootloader is being used. It's always a good idea to check the documentation or support resources for your specific Linux distribution if you're having trouble finding or editing the Grub2 configuration file.

 

Understanding GRUB2 Configuration File Content

/boot/grub2/grub.cfg or /boot/grub/grub.cfg

The grub.cfg file we discussed in the previous section is the configuration file for the GRUB bootloader, specifically GRUB version 2. GRUB is a software package that is commonly used as the bootloader for Linux systems. The bootloader is the software that runs first when a computer starts up, and its primary function is to load the operating system kernel into memory and start it running.

This particular GRUB configuration file has several sections, each starting with a comment that identifies it.

  • The first section is 00_header, which sets up some environment variables and functions that are used later in the file. One of the key functions is savedefault, which saves the current menu selection as the default for the next boot if no other selection is made. The section also sets the timeout for the menu to five seconds and specifies that the menu should be displayed on the console.
  • The next section is 01_users, which is an optional section that allows you to set up user authentication for the bootloader. This section reads a user.cfg file, and if it contains a GRUB2_PASSWORD variable, it sets the root user as a superuser and sets the password for that user using the password_pbkdf2 function.
  • The next section is 08_fallback_counting, which is another optional section that provides a fallback mechanism for booting the system if the previous boot was not successful. This section uses the boot_counter variable to keep track of the number of unsuccessful boot attempts, and if the boot_counter reaches zero, it sets the default menu selection to 1, which is typically a fallback option.
  • The last section is 10_linux, which is the section that actually loads the Linux kernel and starts it running. This section uses the insmod command to load several modules that are necessary for accessing the boot device and the root filesystem, and then uses the search command to locate the root filesystem by its UUID. Finally, the section specifies the kernel command-line options that are passed to the kernel at boot time, which include the root device, the resume device, and some kernel options to control logging and error reporting.

 

/etc/default/grub

The /etc/default/grub file is the main configuration file for the GRUB2 bootloader on Linux systems. It contains various parameters and options that are used to configure the behavior of the GRUB2 bootloader during system startup.

Here are some of the commonly used options and their descriptions:

  • GRUB_DEFAULT: This option sets the default menu entry to be loaded if no other entry is selected during boot. The default value is set to "0", which means the first entry in the menu will be loaded.
  • GRUB_TIMEOUT: This option sets the time in seconds for which the GRUB menu will be displayed during boot. The default value is set to "5" seconds.
  • GRUB_HIDDEN_TIMEOUT: This option sets the time in seconds for which the GRUB menu will be hidden during boot. If this option is set to a non-zero value, the GRUB menu will not be displayed unless the user presses a key. The default value is set to "0", which means the GRUB menu will be displayed for the duration specified by GRUB_TIMEOUT.
  • GRUB_HIDDEN_TIMEOUT_QUIET: If this option is set to "true", the GRUB menu will be hidden and there will be no indication that the system is waiting for user input. This option is only effective if GRUB_HIDDEN_TIMEOUT is also set to a non-zero value.
  • GRUB_CMDLINE_LINUX: This option allows you to add additional kernel parameters to be passed to the Linux kernel during boot. For example, you can use this option to set the root partition or specify kernel options for hardware.
  • GRUB_DISABLE_RECOVERY: If this option is set to "true", the recovery mode entry in the GRUB menu will be hidden. This can be useful for security reasons, as it can prevent an attacker from using the recovery mode to gain access to the system.
  • GRUB_DISABLE_SUBMENU: If this option is set to "true", GRUB will not create submenus for menu entries that have multiple kernels or initrd images. This can make the GRUB menu less cluttered and easier to navigate.

These are just some of the options that can be set in the /etc/default/grub file. The exact options and their default values may vary depending on the Linux distribution and version you are using.

 

How to update GRUB2 config file?

In general, it's recommended to avoid manually editing the GRUB2 configuration file, as this can lead to boot problems or other issues. Instead, it's usually better to add custom settings to the /etc/default/grub file or to files in the /etc/grub.d/ directory, and then regenerate the configuration file using the appropriate command for your system (such as update-grub  or grub2-mkconfig based on your Linux variant).

Open /etc/default/grub using your favorite editor:

sudo nano /etc/default/grub

This will open the Grub configuration file in the Nano text editor, which is a simple text editor that is commonly used in Linux.

Edit the configuration file as desired. The file will contain various lines of configuration settings that you can adjust to customize the behavior of the Grub boot loader. For example, you might want to change the default boot option, adjust the boot timeout value, or specify kernel parameters.

Save the changes to the file by pressing Ctrl+O, then pressing Enter to confirm the filename. You can then exit Nano by pressing Ctrl+X.

Once you've saved your changes, you need to update the Grub configuration by running the following command:

For Ubuntu and other Debian-based distributions:

sudo update-grub

For Fedora and other Red Hat-based distributions:

sudo grub2-mkconfig -o /boot/grub2/grub.cfg

For Arch Linux and other Arch-based distributions:

sudo grub-mkconfig -o /boot/grub/grub.cfg

This will regenerate the Grub configuration file based on the changes you made, and apply them to the next boot.

You can read more at How to update GRUB2 using grub2-editenv and grubby in RHEL 8 Linux

 

How to fix GRUB2 related errors?

Grub errors can be caused by various issues, including misconfiguration, hardware failure, or software conflicts. Here are some steps you can take to fix a Grub error (the steps may vary per distribution):

  1. Boot from a live CD/USB: If you cannot boot into your Linux installation, you will need to boot from a live CD/USB to access your system.
  2. Identify the root partition: Once you have booted from the live CD/USB, open a terminal and run the command "sudo fdisk -l". This command will display a list of all partitions on your hard drive. Identify the root partition of your Linux installation.
  3. Mount the root partition: Mount the root partition of your Linux installation. For example, if your root partition is /dev/sda1, run the command "sudo mount /dev/sda1 /mnt".
  4. Chroot into the root partition: Chroot into the root partition using the command "sudo chroot /mnt".
  5. Reinstall Grub: Run the command "sudo grub-install /dev/sda". This command will reinstall Grub on the master boot record of your hard drive. If you have multiple hard drives, replace /dev/sda with the appropriate device name.
  6. Update Grub configuration: Run the command "sudo update-grub". This command will update the Grub configuration file based on the operating systems installed on your hard drive.
  7. Reboot your system: After completing the above steps, exit the chroot environment and reboot your system. You should now be able to boot into your Linux installation without encountering any Grub errors.

If you are still encountering Grub errors after following the above steps, it may be necessary to perform a more advanced troubleshooting process, such as repairing a corrupted partition or updating your system's BIOS.

 

Can I re-install GRUB if it is corrupted?

To reinstall Grub for different BIOS types and Linux variants. In general you will have to boot into a live Linux distribution on your system. You can use a USB drive or DVD to create a bootable live Linux distribution.

Here I have written the steps to boot into Rescue Mode in RHEL/CentOS Linux variants Boot into rescue mode without password (RHEL/CentOS)

Next you must know the partition where boot partition is installed. In most cases it is always the first partition on the system i.e. /dev/sda1

IMPORTANT NOTE:
If you are not aware of your boot partition then it is not recommended to go through the next steps as it could corrupt your file system.

Reinstall Grub. The specific command will depend on the Linux distribution you are using:

For Ubuntu and Debian-based distributions, run the following command:

sudo grub-install /dev/sda

For Red Hat Enterprise Linux (RHEL) and CentOS, run the following command:

sudo grub2-install /dev/sda

Replace /dev/sda with the disk where you want to install Grub. This is typically the disk where your BIOS or UEFI firmware is installed.

Update the Grub configuration file based on your Linux variant and BIOS Type using either grub2-mkconfig or update-grub.

Reboot the system and verify that Grub is working correctly.

 

Related Articles:

How to protect GRUB2 from booting kernel without password in Linux

How to set GRUB2 password using grub2-setpassword (RHEL / CentOS 7)

Easy steps to change default kernel version RHEL/CentOS 8

How to PROPERLY boot into single user mode in RHEL/CentOS 7/8

 

Summary

In Linux, the GRUB (GRand Unified Bootloader) is a bootloader that is responsible for loading the operating system into memory during system boot. It is an essential component of the boot process and is used to load various operating systems and kernel images.

The GRUB configuration file, located in the /boot/grub directory, contains information about how the bootloader should behave. It includes the default operating system to boot, timeout values, and various other boot options. This file is editable using a text editor, but care should be taken when modifying it to avoid breaking the bootloader.

For different BIOS types such as legacy and UEFI, the GRUB installation process may vary. In legacy BIOS systems, the bootloader is typically installed to the MBR (Master Boot Record), while in UEFI systems, it is installed to the EFI System Partition (ESP). The location of the GRUB configuration file also varies based on the Linux distribution and the version of GRUB being used.

In the event of a GRUB error, several steps can be taken to fix the issue. These may include checking the configuration file for errors, reinstalling the bootloader, repairing the file system, or manually booting the operating system from the GRUB prompt.

Overall, the GRUB files are critical components of the Linux boot process and must be configured correctly to ensure the system boots properly. The GRUB configuration file, along with the bootloader itself, can be customized to fit the needs of the user or system administrator, but care should be taken when making changes to avoid breaking the boot process.

 

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can reach out to him on his LinkedIn profile or join on Facebook page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment