LinuxCommandLibrary

grub-mkconfig

Generate GRUB configuration file

TLDR

Do a dry run and print the configuration to stdout

$ sudo grub-mkconfig
copy

Generate the configuration file
$ sudo grub-mkconfig [[-o|--output]] [/boot/grub/grub.cfg]
copy

Display help
$ grub-mkconfig [[-h|--help]]
copy

SYNOPSIS

grub-mkconfig [OPTION]...

PARAMETERS

-o FILE, --output=FILE
    Write the generated configuration to FILE instead of standard output.

-v, --verbose
    Print verbose debugging messages to standard error during the configuration generation process.

-V, --version
    Display version information about grub-mkconfig and exit.

--help
    Display a help message listing all available options and exit.

DESCRIPTION

grub-mkconfig is a crucial utility for automatically generating the grub.cfg file, the primary configuration file for the GRUB 2 bootloader. It streamlines the complex process of identifying installed operating systems and their kernel images, then creating appropriate boot entries.

The command gathers configuration data from two main sources: user-defined settings in /etc/default/grub and a series of executable scripts located in /etc/grub.d/. These scripts are responsible for detecting various components, such as Linux kernels, initramfs images, and other installed operating systems (often with the help of os-prober).

The generated grub.cfg is typically saved to a location like /boot/grub/grub.cfg or /boot/grub2/grub.cfg. By automating this process, grub-mkconfig significantly reduces the risk of errors associated with manual configuration file editing. It ensures that GRUB's boot menu accurately reflects the system's current state, making system updates and multi-boot setups more manageable. Direct manual editing of grub.cfg is generally discouraged because changes will be overwritten the next time this command is run.

CAVEATS

Caveats and Limitations:

grub-mkconfig requires root privileges to run and write the configuration file to system directories.

Manual edits made directly to grub.cfg are generally overwritten upon subsequent runs of grub-mkconfig. All customizations should primarily be made in /etc/default/grub or by adding custom scripts to the /etc/grub.d/ directory.

The command's ability to detect other operating systems (such as Windows or alternative Linux distributions) is dependent on the os-prober package being installed and properly enabled on the system.

While generally safe, it's always prudent to consider backing up your existing grub.cfg before allowing grub-mkconfig to overwrite it, especially if you have a highly customized boot setup.

CONFIGURATION FILES

grub-mkconfig relies on specific configuration files and directories to generate the final grub.cfg. These include:

/etc/default/grub:
This file contains user-editable variables that define global GRUB settings, such as the default boot entry, timeout duration, appearance (theme), and kernel parameters to be passed to Linux. It's the primary location for general user customization.

/etc/grub.d/:
This directory contains a series of executable scripts. Each script is responsible for generating a specific section or entry within the grub.cfg file. Examples include 10_linux (for detecting Linux kernels), 30_os-prober (for detecting other operating systems), and 40_custom (for user-defined entries). These scripts are executed in alphanumeric order, and their output is concatenated to form the final configuration file.

Common Usage (update-grub):
On many Linux distributions, particularly Debian and Ubuntu, users commonly invoke `update-grub` instead of directly running `grub-mkconfig`. `update-grub` is typically a simple wrapper script or a symlink that executes `grub-mkconfig -o /boot/grub/grub.cfg` with the appropriate system-specific paths, making the process even simpler for end-users.

HISTORY

The GRUB bootloader underwent a significant evolution from GRUB Legacy (version 0.9x) to GRUB 2. GRUB Legacy used a simpler menu.lst file which was often manually edited.

GRUB 2, initially released in 2009 and widely adopted in distributions post-2010, introduced a more robust and modular configuration system. With GRUB 2, direct manual editing of the main grub.cfg file became discouraged due to its complexity and the potential for errors.

grub-mkconfig was developed as a core component of GRUB 2 to automate the generation of grub.cfg. It processes configuration fragments from /etc/grub.d/ scripts and user settings from /etc/default/grub, ensuring a consistent and update-friendly boot configuration. This shift aimed to improve stability, facilitate automated updates of boot entries (e.g., when new kernels are installed), and reduce the burden of manual configuration for users.

SEE ALSO

update-grub(8), grub-install(8), grub(8), grub.cfg(5), grub-probe(8), os-prober(8)

Copied to clipboard