Top Tags

Change GRUB boot order in Ubuntu

Change the default boot order of the GRUB menu in Ubuntu

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

bash
1# View the generated GRUB configuration
2sudo grep -E "^menuentry |^submenu " /boot/grub/grub.cfg | nl -v 0
3
4# Alternative method: check with awk
5awk -F\' '/menuentry |submenu / {print i++ " : " $2}' /boot/grub/grub.cfg

This 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

bash
1sudo nano /etc/default/grub
2GRUB_DEFAULT=4
3sudo update-grub

windows 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:

bash
1# Open GRUB configuration file
2sudo nano /etc/default/grub
3
4# Change GRUB_DEFAULT to the desired entry number
5# For Ubuntu (typically entry 0)
6GRUB_DEFAULT=0
7
8# For Windows (if it's entry 5)
9# GRUB_DEFAULT=5
10
11# Save and exit (Ctrl+X, then Y, then Enter)

After editing, apply the changes:

bash
1# Update GRUB configuration
2sudo update-grub
3
4# Reboot to test
5sudo reboot

Method 2: Using Entry Names (Recommended)

Using entry names is more reliable, especially after kernel updates that might change entry positions:

bash
1# Find the exact menu entry name
2grep -E "^menuentry " /boot/grub/grub.cfg | cut -d "'" -f2
3
4# Edit GRUB configuration
5sudo nano /etc/default/grub
6
7# Set GRUB_DEFAULT to the exact entry name
8GRUB_DEFAULT="Ubuntu, with Linux 6.8.0-50-generic"
9
10# Or for Windows
11# GRUB_DEFAULT="Windows Boot Manager (on /dev/nvme0n1p1)"
12
13# Update GRUB
14sudo update-grub

Method 3: Submenu Entries

If your desired entry is within a submenu (common for older kernel versions), use the format "submenu_name>entry_name":

bash
1# Example for an older kernel in "Advanced options for Ubuntu" submenu
2GRUB_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:

bash
1# Display menu for 10 seconds
2GRUB_TIMEOUT=10
3
4# Boot immediately without showing menu (use with caution)
5GRUB_TIMEOUT=0
6
7# Wait indefinitely for user selection
8GRUB_TIMEOUT=-1

Remember Last Boot Choice

Make GRUB remember and boot the last selected operating system:

bash
1# Edit GRUB configuration
2sudo nano /etc/default/grub
3
4# Change GRUB_DEFAULT to saved
5GRUB_DEFAULT=saved
6
7# Enable save feature
8GRUB_SAVEDEFAULT=true
9
10# Update GRUB
11sudo update-grub

Hidden Menu with Timeout

Hide the menu by default but show it when Shift is pressed:

bash
1GRUB_TIMEOUT_STYLE=hidden
2GRUB_TIMEOUT=5
3GRUB_HIDDEN_TIMEOUT_QUIET=true

Troubleshooting

GRUB Changes Not Applied

If changes don't take effect after update-grub:

bash
1# Verify GRUB configuration syntax
2sudo grub-mkconfig -o /dev/null
3
4# Manually regenerate GRUB configuration
5sudo grub-mkconfig -o /boot/grub/grub.cfg
6
7# For UEFI systems, ensure proper installation
8sudo grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=ubuntu

Windows Entry Not Detected

If Windows doesn't appear in GRUB menu:

bash
1# Enable os-prober
2sudo nano /etc/default/grub
3
4# Ensure this line is commented or removed
5# GRUB_DISABLE_OS_PROBER=true
6
7# Install os-prober if missing
8sudo apt update
9sudo apt install os-prober
10
11# Mount Windows EFI partition (adjust partition as needed)
12sudo mount /dev/nvme0n1p1 /mnt
13
14# Run os-prober
15sudo os-prober
16
17# Update GRUB
18sudo update-grub
19
20# Unmount
21sudo umount /mnt

Backup and Restore GRUB Configuration

Always backup before making changes:

bash
1# Backup current configuration
2sudo cp /etc/default/grub /etc/default/grub.backup
3sudo cp /boot/grub/grub.cfg /boot/grub/grub.cfg.backup
4
5# Restore from backup if needed
6sudo cp /etc/default/grub.backup /etc/default/grub
7sudo update-grub

Verification

After making changes, verify the configuration:

bash
1# Check the default entry in the generated config
2sudo grep "set default" /boot/grub/grub.cfg
3
4# View complete GRUB environment
5sudo grub-editenv list
6
7# Test GRUB configuration without rebooting
8sudo grub-script-check /boot/grub/grub.cfg

Security Considerations

Password-Protect GRUB

To prevent unauthorized users from modifying boot parameters:

bash
1# Generate password hash
2grub-mkpasswd-pbkdf2
3
4# Copy the generated hash, then edit GRUB config
5sudo nano /etc/grub.d/40_custom
6
7# Add these lines (replace HASH with your generated hash)
8set superusers="admin"
9password_pbkdf2 admin HASH
10
11# Update GRUB
12sudo update-grub

This configuration requires a password to edit boot entries but allows normal booting without authentication.