# [SOLVED] grub2 default kernel

## ttre

How to set up new kernel as default in grub2 ?  I have total 2 kernels, one new and one older.

?Last edited by ttre on Sun Mar 19, 2017 3:51 pm; edited 1 time in total

----------

## apiaio

You can edit grub config file /etc/default/grub and in the line

 *Quote:*   

> GRUB_DEFAULT= ...

 set the default menu entry by menu position.

----------

## ttre

 *apiaio wrote:*   

> You can edit grub config file /etc/default/grub and in the line
> 
>  *Quote:*   GRUB_DEFAULT= ... set the default menu entry by menu position.

 

so I have

```
GRUB_DISTRIBUTOR="Gentoo"

# Default menu entry

GRUB_DEFAULT=1

```

I did :

```
grub-mkconfig -o /boot/grub/grub.cfg
```

after it it was done.

but still old kernel is loading.

Why ?

----------

## guitou

Hello.

I might be telling bullshit, but I believe Grub default is to consider latest kernel as the default one to boot, then I guess best approach is to change ctime and/or mtime of kernel files in /boot (see touch command)

++

Gi)

----------

## apiaio

If the new kernel is the first one, you should use 

```
   

GRUB_DISTRIBUTOR="Gentoo"

# Default menu entry

GRUB_DEFAULT=0 
```

----------

## ttre

 *apiaio wrote:*   

> If the new kernel is the first one, you should use 
> 
> ```
>    
> 
> ...

 

The first one, but by data/time created kernel file or what?

----------

## apiaio

 *ttre wrote:*   

> 
> 
> The first one, but by data/time created kernel file or what?

 

I do not understand this question. 

The first line in menu entry is 0 no 1. Did you try

```
GRUB_DEFAULT=0 
```

?

----------

## ttre

 *apiaio wrote:*   

>  *ttre wrote:*   
> 
> The first one, but by data/time created kernel file or what? 
> 
> I do not understand this question. 
> ...

 

Yes, tried 0 and 1.

and it changed nothing.

Here is my grub.cfg file.

```

# cat /boot/grub/grub.cfg 

#

# DO NOT EDIT THIS FILE

#

# It is automatically generated by grub-mkconfig using templates

# from /etc/grub.d and settings from /etc/default/grub

#

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

if [ -s $prefix/grubenv ]; then

  load_env

fi

if [ "${next_entry}" ] ; then

   set default="${next_entry}"

   set next_entry=

   save_env next_entry

   set boot_once=true

else

   set default="0"

fi

if [ x"${feature_menuentry_id}" = xy ]; then

  menuentry_id_option="--id"

else

  menuentry_id_option=""

fi

export menuentry_id_option

if [ "${prev_saved_entry}" ]; then

  set saved_entry="${prev_saved_entry}"

  save_env saved_entry

  set prev_saved_entry=

  save_env prev_saved_entry

  set boot_once=true

fi

function savedefault {

  if [ -z "${boot_once}" ]; then

    saved_entry="${chosen}"

    save_env saved_entry

  fi

}

function load_video {

  if [ x$feature_all_video_module = xy ]; then

    insmod all_video

  else

    insmod efi_gop

    insmod efi_uga

    insmod ieee1275_fb

    insmod vbe

    insmod vga

    insmod video_bochs

    insmod video_cirrus

  fi

}

if [ x$feature_default_font_path = xy ] ; then

   font=unicode

else

insmod part_gpt

insmod ext2

set root='hd0,gpt4'

if [ x$feature_platform_search_hint = xy ]; then

  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt4 --hint-efi=hd0,gpt4 --hint-baremetal=ahci0,gpt4  f32681fc-c967-4245-a4cf-57a6157a4e28

else

  search --no-floppy --fs-uuid --set=root f32681fc-c967-4245-a4cf-57a6157a4e28

fi

    font="/usr/share/grub/unicode.pf2"

fi

if loadfont $font ; then

  set gfxmode=auto

  load_video

  insmod gfxterm

fi

terminal_output gfxterm

if [ x$feature_timeout_style = xy ] ; then

  set timeout_style=menu

  set timeout=5

# Fallback normal timeout code in case the timeout_style feature is

# unavailable.

else

  set timeout=5

fi

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

### BEGIN /etc/grub.d/10_linux ###

menuentry 'Gentoo GNU/Linux' --class gentoo --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-f32681fc-c967-4245-a4cf-57a6157a4e28' {

   load_video

   if [ "x$grub_platform" = xefi ]; then

      set gfxpayload=keep

   fi

   insmod gzio

   insmod part_gpt

   insmod ext2

   set root='hd0,gpt2'

   if [ x$feature_platform_search_hint = xy ]; then

     search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2  233dd17e-81c0-41af-bfaf-c5d198a77ca1

   else

     search --no-floppy --fs-uuid --set=root 233dd17e-81c0-41af-bfaf-c5d198a77ca1

   fi

   echo   'Loading Linux x86_64-4.4.6-gentoo ...'

   linux   /kernel-genkernel-x86_64-4.4.6-gentoo root=UUID=f32681fc-c967-4245-a4cf-57a6157a4e28 ro  

   echo   'Loading initial ramdisk ...'

   initrd   /initramfs-genkernel-x86_64-4.4.6-gentoo

}

submenu 'Advanced options for Gentoo GNU/Linux' $menuentry_id_option 'gnulinux-advanced-f32681fc-c967-4245-a4cf-57a6157a4e28' {

   menuentry 'Gentoo GNU/Linux, with Linux x86_64-4.4.6-gentoo' --class gentoo --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-x86_64-4.4.6-gentoo-advanced-f32681fc-c967-4245-a4cf-57a6157a4e28' {

      load_video

      if [ "x$grub_platform" = xefi ]; then

         set gfxpayload=keep

      fi

      insmod gzio

      insmod part_gpt

      insmod ext2

      set root='hd0,gpt2'

      if [ x$feature_platform_search_hint = xy ]; then

        search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2  233dd17e-81c0-41af-bfaf-c5d198a77ca1

      else

        search --no-floppy --fs-uuid --set=root 233dd17e-81c0-41af-bfaf-c5d198a77ca1

      fi

      echo   'Loading Linux x86_64-4.4.6-gentoo ...'

      linux   /kernel-genkernel-x86_64-4.4.6-gentoo root=UUID=f32681fc-c967-4245-a4cf-57a6157a4e28 ro  

      echo   'Loading initial ramdisk ...'

      initrd   /initramfs-genkernel-x86_64-4.4.6-gentoo

   }

   menuentry 'Gentoo GNU/Linux, with Linux x86_64-4.4.6-gentoo (recovery mode)' --class gentoo --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-x86_64-4.4.6-gentoo-recovery-f32681fc-c967-4245-a4cf-57a6157a4e28' {

      load_video

      if [ "x$grub_platform" = xefi ]; then

         set gfxpayload=keep

      fi

      insmod gzio

      insmod part_gpt

      insmod ext2

      set root='hd0,gpt2'

      if [ x$feature_platform_search_hint = xy ]; then

        search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2  233dd17e-81c0-41af-bfaf-c5d198a77ca1

      else

        search --no-floppy --fs-uuid --set=root 233dd17e-81c0-41af-bfaf-c5d198a77ca1

      fi

      echo   'Loading Linux x86_64-4.4.6-gentoo ...'

      linux   /kernel-genkernel-x86_64-4.4.6-gentoo root=UUID=f32681fc-c967-4245-a4cf-57a6157a4e28 ro single 

      echo   'Loading initial ramdisk ...'

      initrd   /initramfs-genkernel-x86_64-4.4.6-gentoo

   }

   menuentry 'Gentoo GNU/Linux, with Linux 4.9.6-gentoo-r1' --class gentoo --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.9.6-gentoo-r1-advanced-f32681fc-c967-4245-a4cf-57a6157a4e28' {

      load_video

      if [ "x$grub_platform" = xefi ]; then

         set gfxpayload=keep

      fi

      insmod gzio

      insmod part_gpt

      insmod ext2

      set root='hd0,gpt2'

      if [ x$feature_platform_search_hint = xy ]; then

        search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2  233dd17e-81c0-41af-bfaf-c5d198a77ca1

      else

        search --no-floppy --fs-uuid --set=root 233dd17e-81c0-41af-bfaf-c5d198a77ca1

      fi

      echo   'Loading Linux 4.9.6-gentoo-r1 ...'

      linux   /vmlinuz-4.9.6-gentoo-r1 root=UUID=f32681fc-c967-4245-a4cf-57a6157a4e28 ro  

      echo   'Loading initial ramdisk ...'

      initrd   /initramfs-genkernel-x86_64-4.9.6-gentoo-r1

   }

   menuentry 'Gentoo GNU/Linux, with Linux 4.9.6-gentoo-r1 (recovery mode)' --class gentoo --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.9.6-gentoo-r1-recovery-f32681fc-c967-4245-a4cf-57a6157a4e28' {

      load_video

      if [ "x$grub_platform" = xefi ]; then

         set gfxpayload=keep

      fi

      insmod gzio

      insmod part_gpt

      insmod ext2

      set root='hd0,gpt2'

      if [ x$feature_platform_search_hint = xy ]; then

        search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2  233dd17e-81c0-41af-bfaf-c5d198a77ca1

      else

        search --no-floppy --fs-uuid --set=root 233dd17e-81c0-41af-bfaf-c5d198a77ca1

      fi

      echo   'Loading Linux 4.9.6-gentoo-r1 ...'

      linux   /vmlinuz-4.9.6-gentoo-r1 root=UUID=f32681fc-c967-4245-a4cf-57a6157a4e28 ro single 

      echo   'Loading initial ramdisk ...'

      initrd   /initramfs-genkernel-x86_64-4.9.6-gentoo-r1

   }

}

### END /etc/grub.d/10_linux ###

### BEGIN /etc/grub.d/20_linux_xen ###

### END /etc/grub.d/20_linux_xen ###

### BEGIN /etc/grub.d/30_os-prober ###

### END /etc/grub.d/30_os-prober ###

### BEGIN /etc/grub.d/40_custom ###

# This file provides an easy way to add custom menu entries.  Simply type the

# menu entries you want to add after this comment.  Be careful not to change

# the 'exec tail' line above.

### END /etc/grub.d/40_custom ###

### BEGIN /etc/grub.d/41_custom ###

if [ -f  ${config_directory}/custom.cfg ]; then

  source ${config_directory}/custom.cfg

elif [ -z "${config_directory}" -a -f  $prefix/custom.cfg ]; then

  source $prefix/custom.cfg;

fi

### END /etc/grub.d/41_custom ###

```

----------

## apiaio

I can see, that on the first position is:

kernel-4.4.6

second

kernel-4.4.6 /recovery mode/

third

kernel-4.9.6

and finaly

kernel-4.9.6 /recovery mode/.

Such that the new kernel is on the third position.

Please try

```
GRUB_DEFAULT= 2
```

----------

## ttre

 *apiaio wrote:*   

> I can see, that on the first position is:
> 
> kernel-4.4.6
> 
> second
> ...

 

2 is not working, 

I thought the same way, but maybe it is matter that, all what You wrote is in:

subentry 'Advanced options for Gentoo GNU/Linux'

...

...

but how to write it  right way.

1. Gentoo Linux

2. 'Advanced options for Gentoo GNU/Linux'

2.1 kernel-4.4.6

2.2 kernel-4.4.6 /recovery mode/

2.3 kernel-4.9.6

2.4 kernel-4.9.6 /recovery mode/.

----------

## ttre

ok,  found it

GRUB_DEFAULT="1>2" works.

Thx all.

----------

## mimosinnet

This has been a useful tread. In my case:

Menu grub structure: 

```
# grep "menuentry" /boot/grub/grub.cfg | cut -d "'" -f1

menuentry 

submenu 

        menuentry 

        menuentry 

        menuentry 

        menuentry 

        menuentry 

        menuentry
```

Menu grub entries: 

```
# grep "menuentry" /boot/grub/grub.cfg | cut -d "'" -f2

Gentoo GNU/Linux

Advanced options for Gentoo GNU/Linux

Gentoo GNU/Linux, with Linux x86_64-4.19.97-gentoo

Gentoo GNU/Linux, with Linux x86_64-4.19.97-gentoo (recovery mode)

Gentoo GNU/Linux, with Linux x86_64-4.19.86-gentoo

Gentoo GNU/Linux, with Linux x86_64-4.19.86-gentoo (recovery mode)

Gentoo GNU/Linux, with Linux 5.4.28-gentoo-x86_64

Gentoo GNU/Linux, with Linux 5.4.28-gentoo-x86_64 (recovery mode)
```

It looks like the sub-menu grub entries are:

```
0. Gentoo GNU/Linux, with Linux x86_64-4.19.97-gentoo

1. Gentoo GNU/Linux, with Linux x86_64-4.19.97-gentoo (recovery mode)

2. Gentoo GNU/Linux, with Linux x86_64-4.19.86-gentoo

3. Gentoo GNU/Linux, with Linux x86_64-4.19.86-gentoo (recovery mode)

4. Gentoo GNU/Linux, with Linux 5.4.28-gentoo-x86_64

5. Gentoo GNU/Linux, with Linux 5.4.28-gentoo-x86_64 (recovery mode)
```

Setting GRUB_DEFAULT="1>4" in /etc/default/grub, and executing grub-mkconfig -o /boot/grub/grub.cfg, the following like appears in /boot/grub/grub.cfg: 

```
   set default="1>4" 
```

The kernel boots into: Gentoo GNU/Linux, with Linux 5.4.28-gentoo-x86_64

Thanks!   :Very Happy: 

----------

## piekarski

Thanks. This thread was indeed helpful and thank you for explaining how to select the kernel by picking menu entries, so to speak.

I was updating to a new kernel version and I ran into some issues. For the time being I thought to use an older version (no time to fix at the moment).

So instead of checking docs I took my chances and went for the "gentoo-way" to select things. I ran 

```
sudo eselect kernel list
```

 and than 

```
sudo eselect kernel set 3
```

 to select the previous kernel version. Just in case I rerun a 

```
sudo grub-mkconfig -o /boot/grub/grub.cfg
```

, because I did not see eselect doing it. But this way does not change the default kernel.

My question is why does eselect do not do the job and is there any other way to set a default kernel? 

I mean something like GRUB_DEFAULT="1>4" works, but well no offense it does not look very reliable especially when I emerge another kernel this will boot another version than. Is there no way to specify something like GRUB_DEFAULT="5.19.6"?

Maybe you can point me to some discussion or resource?

Thanks in advance.

----------

