Wednesday, September 22, 2010

Editing Grub2 Multiboot

I have a multi-boot laptop. I dual boot between Ubuntu 9.x and Windows 7. Grub is the boot loader. The default OS in my Grub was the latest build of the Kernel. Windows 7 was the last in the list. I wanted Windows 7 to be the default choice.

Apparently Grub2 is a little more complicated to edit-at least it seemed at first. The older Grub boot loader all you had to edit was the menu.lst file. Grub2 was made in such a way to protect the system and provide more features.

If you Google this issue you will run into a lot of entries about how hard it is. Some also give what I think is wrong instructions.

First just a little more information:

The old file to edit was in /boot/grub/menu.lst - this is a text file that you could edit by entering this command: sudo gedit /boot/grub/menu.lst. But, Grub2 is configured a little different. The file that controls how Grub boot is /boot/grub/grub.cfg.

Grub.cfg is actually read only, and while you can change that and edit it-the file itself warns you about editing it. See the following from the beginning of the file:

# It is automatically generated by /usr/sbin/grub-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub

### BEGIN /etc/grub.d/00_header ###

Now this file is edited by Grub itself.

The solution though is rather simple. All you have to do is edit the /etc/default/grub/grub. This file is actually used to build the one read upon boot.

Here is what mine looked like:

# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.

GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`

# Uncomment to disable graphical terminal (grub-pc only)

# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'

# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux

# Uncomment to disable generation of recovery mode menu entrys

The most important line is at the top:

The default is the OS in order from the grub.cfg to default to if you don't make another selection from the menu. One point here is that the list starts with "0". So O is the first, and usually the default: GRUB_DEFAULT=0. So count to see which number you need.

In the /boot/grub/grub.cfg each choice starts in the file with: menuentry. So just count how many lines start with menuentry starting with "0". In my case, I counted 10: 0,1,2,3,..,10. The actual full entry below:

menuentry "Windows 7 (loader) (on /dev/sda1)" {
insmod ntfs
set root=(hd0,1)
search --no-floppy --fs-uuid --set 707c80a97c806c22
chainloader +1

So once you are done editing the /etc/default/grub/grub file, you must run this command:

sudo update-grub

Now next time you boot, you will have the new default.


1. Look at the /boot/grub/grub.cfg and count the lines from 0 to see what number the OS you want is to be the default.

2. Edit the /etc/default/grub/grub file-the line with the GRUB_DEFAULT={put your number here}.

3. After saving the file, run sudo update-grub.

4. Reboot.


No comments: