Change GRUB boot order in Ubuntu
With Ubuntu, you can change the boot order of the GRUB menu. This is useful if you have multiple operating systems installed on your computer and you want to change the default boot order. In my case Windows 11 and Ubuntu 24.04.01, the default boot order is set to Windows 11. If you want to change the default boot order to Ubuntu, you can follow these steps.
Understanding GRUB Configuration
GRUB (GRand Unified Bootloader) is the primary bootloader used by most Linux distributions, including Ubuntu. The GRUB configuration determines which operating system boots by default, timeout settings, and other boot parameters. The main configuration file is located at /etc/default/grub, and changes made to this file must be applied using the update-grub command, which regenerates the actual GRUB configuration at /boot/grub/grub.cfg.
Key GRUB Configuration Parameters
- GRUB_DEFAULT: Specifies the default menu entry (0-indexed)
- GRUB_TIMEOUT: Boot menu display time in seconds (default: 5)
- GRUB_TIMEOUT_STYLE: Controls timeout behavior (menu, countdown, hidden)
- GRUB_CMDLINE_LINUX: Additional kernel parameters
- GRUB_DISABLE_OS_PROBER: Disables automatic detection of other operating systems
Identifying Boot Menu Entries
Before changing the boot order, you need to identify the correct entry number. GRUB menu entries are zero-indexed, meaning the first entry is 0, the second is 1, and so on.
List all GRUB menu entries
1# View the generated GRUB configuration2sudo grep -E "^menuentry |^submenu " /boot/grub/grub.cfg | nl -v 03
4# Alternative method: check with awk5awk -F\' '/menuentry |submenu / {print i++ " : " $2}' /boot/grub/grub.cfgThis command displays all available boot entries with their corresponding index numbers. In a dual-boot system with Windows and Ubuntu, you might see output like:
0 : Ubuntu
1 : Ubuntu, with Linux 6.8.0-50-generic
2 : Ubuntu, with Linux 6.8.0-50-generic (recovery mode)
3 : Memory test (memtest86+.elf)
4 : Memory test (memtest86+.bin)
5 : Windows Boot Manager (on /dev/nvme0n1p1)
Change GRUB boot order
1sudo nano /etc/default/grub2GRUB_DEFAULT=43sudo update-grubwindows set as line 4 so change it to 0 for Ubuntu.
Method 1: Numeric Index (Simple)
The simplest method is to use the numeric index of the desired boot entry:
1# Open GRUB configuration file2sudo nano /etc/default/grub3
4# Change GRUB_DEFAULT to the desired entry number5# For Ubuntu (typically entry 0)6GRUB_DEFAULT=07
8# For Windows (if it's entry 5)9# GRUB_DEFAULT=510
11# Save and exit (Ctrl+X, then Y, then Enter)After editing, apply the changes:
1# Update GRUB configuration2sudo update-grub3
4# Reboot to test5sudo rebootMethod 2: Using Entry Names (Recommended)
Using entry names is more reliable, especially after kernel updates that might change entry positions:
1# Find the exact menu entry name2grep -E "^menuentry " /boot/grub/grub.cfg | cut -d "'" -f23
4# Edit GRUB configuration5sudo nano /etc/default/grub6
7# Set GRUB_DEFAULT to the exact entry name8GRUB_DEFAULT="Ubuntu, with Linux 6.8.0-50-generic"9
10# Or for Windows11# GRUB_DEFAULT="Windows Boot Manager (on /dev/nvme0n1p1)"12
13# Update GRUB14sudo update-grubMethod 3: Submenu Entries
If your desired entry is within a submenu (common for older kernel versions), use the format "submenu_name>entry_name":
1# Example for an older kernel in "Advanced options for Ubuntu" submenu2GRUB_DEFAULT="Advanced options for Ubuntu>Ubuntu, with Linux 6.5.0-28-generic"Advanced GRUB Configuration Options
Setting Boot Timeout
Control how long the GRUB menu is displayed before booting the default entry:
1# Display menu for 10 seconds2GRUB_TIMEOUT=103
4# Boot immediately without showing menu (use with caution)5GRUB_TIMEOUT=06
7# Wait indefinitely for user selection8GRUB_TIMEOUT=-1Remember Last Boot Choice
Make GRUB remember and boot the last selected operating system:
1# Edit GRUB configuration2sudo nano /etc/default/grub3
4# Change GRUB_DEFAULT to saved5GRUB_DEFAULT=saved6
7# Enable save feature8GRUB_SAVEDEFAULT=true9
10# Update GRUB11sudo update-grubHidden Menu with Timeout
Hide the menu by default but show it when Shift is pressed:
1GRUB_TIMEOUT_STYLE=hidden2GRUB_TIMEOUT=53GRUB_HIDDEN_TIMEOUT_QUIET=trueTroubleshooting
GRUB Changes Not Applied
If changes don't take effect after update-grub:
1# Verify GRUB configuration syntax2sudo grub-mkconfig -o /dev/null3
4# Manually regenerate GRUB configuration5sudo grub-mkconfig -o /boot/grub/grub.cfg6
7# For UEFI systems, ensure proper installation8sudo grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=ubuntuWindows Entry Not Detected
If Windows doesn't appear in GRUB menu:
1# Enable os-prober2sudo nano /etc/default/grub3
4# Ensure this line is commented or removed5# GRUB_DISABLE_OS_PROBER=true6
7# Install os-prober if missing8sudo apt update9sudo apt install os-prober10
11# Mount Windows EFI partition (adjust partition as needed)12sudo mount /dev/nvme0n1p1 /mnt13
14# Run os-prober15sudo os-prober16
17# Update GRUB18sudo update-grub19
20# Unmount21sudo umount /mntBackup and Restore GRUB Configuration
Always backup before making changes:
1# Backup current configuration2sudo cp /etc/default/grub /etc/default/grub.backup3sudo cp /boot/grub/grub.cfg /boot/grub/grub.cfg.backup4
5# Restore from backup if needed6sudo cp /etc/default/grub.backup /etc/default/grub7sudo update-grubVerification
After making changes, verify the configuration:
1# Check the default entry in the generated config2sudo grep "set default" /boot/grub/grub.cfg3
4# View complete GRUB environment5sudo grub-editenv list6
7# Test GRUB configuration without rebooting8sudo grub-script-check /boot/grub/grub.cfgSecurity Considerations
Password-Protect GRUB
To prevent unauthorized users from modifying boot parameters:
1# Generate password hash2grub-mkpasswd-pbkdf23
4# Copy the generated hash, then edit GRUB config5sudo nano /etc/grub.d/40_custom6
7# Add these lines (replace HASH with your generated hash)8set superusers="admin"9password_pbkdf2 admin HASH10
11# Update GRUB12sudo update-grubThis configuration requires a password to edit boot entries but allows normal booting without authentication.