How to use the command 'grub-mkconfig' (with examples)
- Linux
- December 17, 2024
GRUB, or the GRand Unified Bootloader, is an integral part of GNU/Linux systems that allows users to have different operating systems on one machine and manage them at the boot level. The command grub-mkconfig
is used for generating a new configuration file for the GRUB bootloader. This command scans the system for installed kernels and creates a configuration file used to boot those kernels. Understanding how to use grub-mkconfig
effectively can be crucial when installing new kernels, or making changes to the boot configuration.
Use case 1: Do a dry run and print the configuration to stdout
Code:
sudo grub-mkconfig
Motivation:
Undertaking a dry run by using grub-mkconfig
without specifying an output file is an excellent way to preview the changes that will be made to the GRUB configuration. This use case is particularly valuable for system administrators and users who need to check any modifications to the boot configuration before applying them. Ensuring that GRUB recognizes the correct kernel and entries is critical in preventing system boot issues.
Explanation:
sudo
: Runninggrub-mkconfig
usually requires superuser privileges since it involves reading configurations that are restricted.grub-mkconfig
: The command itself that processes the GRUB configuration files and lists the detected installations and kernels to standard output (stdout
).
Example output:
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.11.0-27-generic
Found initrd image: /boot/initrd.img-5.11.0-27-generic
Found linux image: /boot/vmlinuz-5.4.0-80-generic
Found initrd image: /boot/initrd.img-5.4.0-80-generic
done
This output shows the found kernel images and any associated initrd images, helping to verify that the configurations are as expected before saving them.
Use case 2: Generate the configuration file
Code:
sudo grub-mkconfig --output=/boot/grub/grub.cfg
Motivation:
When new kernels are installed, or other changes need to be reflected in the boot options, generating a new GRUB configuration file is essential. This ensures the system can boot the latest kernel version or reflect any custom boot configurations. Directing the output to the GRUB configuration file updates the current settings with the newfound information, which ensures the boot-up process uses the most up-to-date resources.
Explanation:
sudo
: Needed because writing to/boot/grub/grub.cfg
typically requires elevated privileges.grub-mkconfig
: Generates the configuration script based on the available system kernels.--output=/boot/grub/grub.cfg
: Directs the generated configuration to the actual GRUB file used during boot. This argument replaces the default location with a specified one, typically where GRUB loads its entries from.
Example output:
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.11.0-27-generic
Found initrd image: /boot/initrd.img-5.11.0-27-generic
Found linux image: /boot/vmlinuz-5.4.0-80-generic
Found initrd image: /boot/initrd.img-5.4.0-80-generic
done
The same series of checks occur here, but it also writes this information into the GRUB configuration file, ensuring the latest system boot data is captured.
Use case 3: Display help
Code:
grub-mkconfig --help
Motivation:
When using a sophisticated command like grub-mkconfig
, having quick access to its flags and options is extremely useful, especially for beginners or users who do not regularly modify boot settings. Displaying the help menu is beneficial for understanding all potential modifications that can be made, fostering a more intuitive use of the command.
Explanation:
grub-mkconfig
: Calls the utility that constructs GRUB configurations.--help
: Displays a brief synopsis of available command-line options and a description of what they do. While GRUB has comprehensive online documentation, this flag provides a quick reference directly in the terminal.
Example output:
Usage: grub-mkconfig [OPTION]...
Generate a GRUB configuration file.
Commands:
--help display this help and exit
--version output version information and exit
--output=FILE save output in FILE
Report bugs to: <https://www.gnu.org/software/grub/manual/grub.html>
This shows relevant options to efficiently utilize the command, assisting users in managing their boot configuration with greater control.
Conclusion:
Utilizing grub-mkconfig
is essential for effective GRUB management. From previewing changes with a dry run to applying them via configuration files, and accessing built-in help, each use case illustrated here demonstrates the importance and versatility of grub-mkconfig
in maintaining a stable boot process. With these use cases, users can ensure that their GRUB configurations are accurately reflecting the system’s kernel states and boot needs.