List Grub2 Kernels and Change Default Boot

This article was posted more than 1 year ago. Please keep in mind that the information on this page may be outdated, insecure, or just plain wrong today.

Here’s an easy one-liner to get the list of kernels Grub2 has in /boot/grub/grub.cfg.
First, let’s find out what the default kernel is…

root@vz:~# grep ^GRUB_DEFAULT /etc/default/grub
GRUB_DEFAULT=0

Ok, so 0 is loaded by default. Now, let’s list all the kernels…

root@vz:~# grep ^menuentry /boot/grub/grub.cfg |sed -e s/' .*/'/g|more
menuentry 'Debian GNU/Linux, with Linux 3.2.0-4-amd64'
menuentry 'Debian GNU/Linux, with Linux 3.2.0-4-amd64 (recovery mode)'
menuentry 'Debian GNU/Linux, with Linux 2.6.32-042stab059.7'
menuentry 'Debian GNU/Linux, with Linux 2.6.32-042stab059.7 (recovery mode)'

Ok cool, I’m loading ‘Debian GNU/Linux, with Linux 3.2.0-4-amd64′ by default.
I want to change this to load ‘Debian GNU/Linux, with Linux 2.6.32-042stab059.7′ so I will need to use the following command:
From the list you can change you default boot kernel and setting it by the command line or edit the file with your favorite command line editor (vi ;])
NOTE: The items start at 0, so if I wanted the last item as default, it would be number 3 (not 4).

root@vz~~# sed -i 's/^GRUB_DEFAULT=0/GRUB_DEFAULT=2/' /etc/default/grub

Then, run update_grub and reboot.

root@vz:~# update-grub
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-3.2.0-4-amd64
Found initrd image: /boot/initrd.img-3.2.0-4-amd64
Found linux image: /boot/vmlinuz-2.6.32-042stab059.7
Found initrd image: /boot/initrd.img-2.6.32-042stab059.7
done

Reboot.