Skip to main content

GRUB in Linux

 


GRUB (also known as GNU GRUB or GNU Grand Unified Bootloader) is a bootloader and boot manager for Linux and other Unix-based OSes. GRUB starts after BIOS finishes the necessary hardware tests and loads it from the Master Boot Record (MBR). Once loaded, GRUB takes control of the system and loads the Linux kernel.

In this post, we will explore the following topics:

  • When does GRUB come into picture in boot loading?
  • What are the edit options and how to use them?
  • Additional information

When does GRUB come into picture in boot loading?

As mentioned earlier, GRUB is the first program that runs after the BIOS. GRUB is responsible for finding and loading the kernel image and the initial RAM disk (initrd) image, which contain the drivers and modules needed by the kernel. GRUB also allows the user to choose between different operating systems, if there is more than one on the system.

GRUB consists of two stages: the first stage is stored in the boot sector, and the second stage is stored in a file system. The first stage of GRUB loads the second stage, which is a larger program that can access multiple file systems and display a graphical menu. The menu allows the user to select a kernel image to boot, or to edit the boot options. If the user does not select anything, GRUB loads the default kernel image after a timeout.

GRUB can also chain-load other boot loaders, such as Windows Boot Manager, by loading their boot sector instead of a kernel image. GRUB can also load an initial RAM disk (initrd) image, which is a temporary file system that contains drivers and modules needed by the kernel. GRUB then passes control to the kernel, which initializes the rest of the operating system.

What are the edit options and how to use them?

GRUB provides a command-line interface and a graphical menu interface for editing the boot options. The command-line interface can be accessed by pressing c at the GRUB menu, and the graphical menu interface can be accessed by pressing e at the GRUB menu. Both interfaces allow the user to modify the boot parameters, such as the kernel image, the initrd image, the root file system, and the kernel options.

The command-line interface has a prompt that looks like this:

grub>

The user can type commands to list the available devices, partitions, files, and kernels. For example, the command ls lists the devices and partitions, and the command ls (hd0,1)/boot lists the files in the /boot directory of the first partition of the first hard disk. The user can also use the set and unset commands to set and unset environment variables, such as root, prefix, and default. The user can also use the linux and initrd commands to specify the kernel and initrd images to load. The user can also use the boot command to boot the system with the specified parameters.

The graphical menu interface has a screen that looks like this:

GNU GRUB version 2.04
+-------------------------------------------------------------------+
|*Ubuntu                                                            |
| Advanced options for Ubuntu                                       |
| Windows Boot Manager (on /dev/sda2)                               |
| System setup                                                      |
+-------------------------------------------------------------------+

    Use the ↑ and ↓ keys to select which entry is highlighted.
    Press enter to boot the selected OS, `e' to edit the commands
    before booting or `c' for a command-line.

The user can use the arrow keys to select an entry, and press Enter to boot it. The user can also press e to edit the commands for the selected entry. The user can then see a screen that looks like this:

+-------------------------------------------------------------------+
|setparams 'Ubuntu'                                                 |
|                                                                   |
|recordfail                                                         |
|load_video                                                         |
|gfxmode $linux_gfx_mode                                            |
|insmod gzio                                                        |
|if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi |
|insmod part_gpt                                                    |
|insmod ext2                                                        |
|set root='hd0,gpt1'                                                |
|if [ x$feature_platform_search_hint = xy ]; then                   |
|  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt1     |
|  --hint-efi=hd0,gpt1 --hint-baremetal=ahci0,gpt1  b58ee4f7-5b6a-4 |
|else                                                               |
|  search --no-floppy --fs-uuid --set=root b58ee4f7-5b6a-40fb-      |
|fi                                                                 |
|linux   /boot/vmlinuz-5.4.0-42-generic root=UUID=b58ee4f7-5b6a-40f |
|b-8d1c-9f2a8d8a8a8a ro  quiet splash $vt_handoff                   |
|initrd  /boot/initrd.img-5.4.0-42-generic                          |
+-------------------------------------------------------------------+

    The selected entry will be started automatically in 10s.
    Press Ctrl-x to start, Ctrl-c for a command prompt or Escape
    to discard edits and return to the GRUB menu.

The user can use the arrow keys to move the cursor, and press Backspace or Delete to delete characters, and press Insert or Type to insert characters. The user can also press Ctrl-a to move to the beginning of the line, and press Ctrl-e to move to the end of the line. The user can also press Ctrl-x to boot the system with the edited parameters, or press Esc to discard the changes and return to the GRUB menu.

Additional information

GRUB is a powerful and flexible bootloader and boot manager for Linux systems. It supports various file systems, operating systems, and boot methods. It also provides a user-friendly interface for editing the boot options and customizing the boot process.

GRUB is configured by files that specify the boot entries, the menu appearance, and the default settings. The main configuration file is /boot/grub/grub.cfg, which is generated by the grub-mkconfig command. The user should not edit this file directly, as it may be overwritten by system updates. Instead, the user should edit the files in /etc/grub.d and /etc/default/grub, which are the source files for generating the grub.cfg file. The user can also use a graphical tool, such as Grub Customizer, to modify the GRUB settings.

GRUB can be installed, reinstalled, or updated by using the grub-install command. The user can also use the grub-update command to update the grub.cfg file after changing the GRUB configuration files or installing a new kernel. The user can also use the grub-reboot command to set a one-time boot entry for the next reboot, or use the grub-set-default command to set a permanent default boot entry.

Conclusion

In this post, we have learned about GRUB in Linux, and how it works as a bootloader and a boot manager. We have also learned how to access, edit, and customize GRUB with a graphical tool or a terminal command. We hope that this post has given you a better understanding of how GRUB boots and manages your Linux system.

Popular posts from this blog

Cockpit vs. Webmin: A Detailed Comparison for Linux Administration

  Introduction In the realm of Linux system administration, having efficient tools for managing and monitoring servers is crucial. Two popular tools that system administrators often use are  Cockpit  and  Webmin . Both of these tools provide a graphical interface accessible via a web browser, simplifying the management of Linux systems. This blog post will explore what Cockpit and Webmin are, their purposes, a comparison table, and additional information to help you choose the right tool for your needs. What is Cockpit? Overview Cockpit is a web-based graphical interface for managing Linux systems. It is designed to be easy to use, enabling both experienced and novice administrators to manage their systems effectively. Cockpit integrates seamlessly with the system’s existing infrastructure, providing real-time monitoring and management capabilities. Purpose Cockpit is primarily used for: Monitoring system performance and resource usage Managing system services Handli...

How to Set Up Custom Screen Resolution on Fedora 38 Permanently

  If you are using Fedora 38 as your operating system, you may have encountered some issues with the screen resolution. The default resolution may not be suitable for your monitor or your preferences, and you may want to change it to a higher or lower value. However, changing the resolution from the Settings menu may not work properly, or it may not persist after a reboot. In this blog post, I will show you how to set up a custom screen resolution on Fedora 38 permanently using some simple commands and configuration files. The first step is to disable the Wayland display server, which is the default display server for Fedora 38. Wayland is a modern and secure display server, but it may not support some custom resolutions or drivers. To disable Wayland, you need to edit the /etc/gdm/custom.conf file as root. You can use any text editor of your choice, such as nano, vim, or gedit. To open the file with nano, for example, you can type the following command in the terminal: sudo nano ...

Key Concepts and Tools for a Linux System Administrator

  A Linux System Administrator needs to have a comprehensive understanding of various concepts and tools to manage, configure, and maintain Linux systems effectively. Below is a categorized list of essential skills and tools with brief descriptions. Category Key Concepts & Tools Description Operating System Linux Distributions (e.g., Fedora, Ubuntu, CentOS) Knowledge of different Linux distributions, their package management systems, and unique features. Kernel Configuration and Management Understanding how to configure and optimize the Linux kernel for different workloads. System Boot Process (GRUB, systemd) Familiarity with the boot process, bootloaders, and system initialization processes. Command Line Skills Bash Shell Scripting Ability to write and debug shell scripts for automation of tasks. Core Commands (ls, cp, mv, rm, find, grep, awk, sed) Proficiency in using basic and advanced command-line utilities for system management. File System File System Hierarchy Standard (...