How to use the command 'grub-set-default' (with examples)
- Linux
- December 17, 2024
The grub-set-default
command is a utility used in Unix-like operating systems to configure GRUB (Grand Unified Bootloader), specifically to set which operating system or kernel version will load by default during the boot process. This is particularly useful in systems that have multiple operating systems installed or various versions of a kernel, allowing users to define what software stack is initiated when the machine starts. Setting the default boot entry can streamline booting experiences and automate the startup of preferred operating systems or environments without needing manual selection from a boot menu.
Use case 1: Set the default boot entry to an entry number, name or identifier
Code:
sudo grub-set-default 1
Motivation:
Users often have multiple operating systems installed on a single machine for development, testing, or personal uses, such as a dual-boot setup with Linux and Windows. In such cases, setting the preferred operating system to boot by default can save time and effort by not having to manually select it during each boot session. Using the entry number, name, or identifier helps accurately designate the desired boot target.
Explanation:
sudo
: This command requires administrative privileges to execute because it modifies system-level boot configurations.grub-set-default
: The command being used to set the default boot entry in GRUB’s menu.1
: The entry number here signifies the second entry in the GRUB boot menu, considering numbering starts from zero. This could alternatively be a specific boot entry name or UUID identifier.
Example Output:
Changing the default boot entry doesn’t produce direct command line output. Instead, on the subsequent boot, the designated operating system or kernel, corresponding to the entry number 1, boots automatically if no other selection is made. Users can verify the correct default is set via GRUB’s configuration files or using grub-editenv list
.
Use case 2: Set the default boot entry to an entry number, name or identifier for an alternative boot directory
Code:
sudo grub-set-default --boot-directory /mnt/alternate_boot 2
Motivation:
In scenarios where multiple disks are in use, such as separate drives for different operating systems or various test environments, GRUB may be configured with alternative boot directories. Such setups benefit from setting boot entries from non-standard directories. This ensures the system boots from the intended configuration file structure maintained in the alternative directory, especially useful when the main boot directory isn’t on the primary drive or has been corrupted.
Explanation:
sudo
: Required to run the command with the necessary permissions to make system-level changes.grub-set-default
: The command designating the default boot selection within GRUB’s configurations.--boot-directory /mnt/alternate_boot
: This option specifies the alternative path to the GRUB boot directory./mnt/alternate_boot
is the path where the boot directory is located, which contains the GRUB configuration files.2
: The entry number indicates the default choice, the third entry due to0-based
numbering, but can also mean a specific name or identifier known in the GRUB configuration file located at the non-standard directory.
Example Output:
As with setting the default boot entry for the standard directory, no direct command line output appears. Instead, the system’s next boot will default to the entry numbered “2” (as configured under the specified boot directory). Users looking to verify can check the active GRUB list through its configuration outputs or utilities like grub-editenv
.
Conclusion
Mastering the grub-set-default
command empowers users to tailor their boot experience, especially in complex environments with multiple operating systems or specialized boot configurations. By setting a preferred default entry, users streamline system startups and maintain control over which environment initializes automatically, optimizing productivity and reducing boot-related errors.