162

I installed a mainline kernel for testing purposes. I would like to set grub to boot from the older kernel by default.

I know I can set the GRUB_DEFAULT=0 setting for the first page of grub but how do I set it to boot by default from one kernel in the second page (Advanced page)?

I would prefer doing this without installing other software (ex. grub-customizer).

Related Questions:
How do I set Windows to boot as the default in the boot loader?
How do I change the grub boot order?

To Do
  • 15,502
  • 3
    @belacqua: It is not a duplicate. This question is about setting an older kernel that is not present in grub's first page of options as default. – To Do Nov 13 '12 at 13:14
  • It seems like this case should be covered in the earlier questions, even if the method needs to be update for 12.10, older kernels, etc.. – belacqua Nov 13 '12 at 15:26
  • 3
    The question's use case is different, and the answer is very specific in a way that doesn't apply to the other claimed duplicates. I came here to solve this particular problem; not to change the boot order; nor to boot Windows. +1 for "not a duplicate". – Robie Basak Mar 16 '15 at 14:09

8 Answers8

191

First, make a backup copy of /etc/default/grub. If something goes wrong, you can easily revert to the known-good copy.

sudo cp /etc/default/grub /etc/default/grub.bak

Then edit the file using the text editor of your choice (e.g. gedit, etc.).

sudo -H gedit /etc/default/grub

Find the line that contains GRUB_DEFAULT - this is what you'll want to edit to set the default. You must know the full name of the kernel you want - e.g. Ubuntu, with Linux 3.13.0-53-generic - along with the full name of the "advanced menu" - e.g. Advanced options for Ubuntu.

You then combine those two strings with > and set GRUB_DEFAULT to them as: GRUB_DEFAULT="Advanced options for Ubuntu>Ubuntu, with Linux 3.13.0-53-generic" (including quotes).

Save it, then build the updated grub menu.

sudo update-grub

See also: GNU GRUB Manual 2.02 or more specifically section 15.1.0 on setting the default.


Note: There is a method utilizing numbers to access kernels and menus but this is not recommended as it is unreliable when kernel updates occur.

Robie Basak
  • 15,670
ananaso
  • 3,970
  • 4
  • 31
  • 51
  • 1
    Thanks very much. It worked but: 1. GRUB_DEFAULT=0 doesn't have the quotation marks by default. Changing to 1>2 requires quotation marks. 2. The third item in the list is "2" because the count starts from "0". – To Do Nov 13 '12 at 13:17
  • @ToDo Changed. I think my zero has quotation marks because I've used grub customizer. Likewise, I thought it was 3>1 since that's how grub customizer listed it in the program; I confirmed that it is indeed 2>0 in the file, so I assume grub customizer does that to minimize confusion for less advanced users. – ananaso Nov 13 '12 at 21:25
  • 27
    Using a numeric value can be problematic when updates occur. It's better to use a text default, ie:

    GRUB_DEFAULT="Previous Linux versions>Ubuntu, with Linux 3.2.0-18-generic"

    – Bealer Jul 04 '13 at 16:45
  • Does this mean that when the kernel is updated (new entry at the top, pushing the rest down), I will move to a kernel which is one version above my choice? – aiao Dec 05 '14 at 20:39
  • I was skeptical but @Bealer hint works perfectly! :) – ceztko Feb 16 '15 at 23:28
  • 37
    One can use sudo grub-mkconfig | less to see all of the possible options – Nitz Feb 17 '15 at 20:55
  • 9
    This is a good solution, but even better is to open a /boot/grub/grub.cfg and there you will see all the manuentry and all the submenus. If you edit them, you can easy set the default ones, even change the Titles of the options in the grub list when restarting a computer. – Aleks Apr 21 '15 at 21:03
  • 12
    It is definitely useful to examine /boot/grub/grub.cfg to determine the exact names to use. But do not change this file as it will be automatically regenerated by update-grub, including on updating packages. – Robie Basak May 19 '15 at 16:36
  • 24
    The formatting for GRUB_DEFAULT is slightly outdated (since at least Ubuntu 14.04.2). Running update-grub I encountered a warning: Please don't use old title Previous Linux versions>Ubuntu, with Linux 3.13.0-53-generic for GRUB_DEFAULT, use Advanced options for Ubuntu>Ubuntu, with Linux 3.13.0-53-generic (for versions before 2.00) or gnulinux-advanced-b0ce60c3-184c-453b-af59-419b56a2584f>gnulinux-3.13.0-53-generic-advanced-b0ce60c3-184c-453b-af59-419b56a2584f (for 2.00 or later) – Wumms Jul 03 '15 at 21:32
  • 1
    @Wumms: same here, using update-grub twice (once to get the new format, edit /etc/default/grub and the second run to have the actual changes in) led to a downgraded kernel after reboot. – WoJ Sep 24 '15 at 15:23
  • 1
    @Wumms Thanks for the heads up; I've changed it in my answer. – ananaso Sep 24 '15 at 16:51
  • "Therefore, it's better to use a text default" - international users beware that the grub strings are internationalizable. – Ricardo Cruz Oct 23 '15 at 00:10
  • This accepted answer differs from what is suggested in http://unix.stackexchange.com/questions/198003/set-default-kernel-in-grub so it would be nice to know which is the preferred method and why. – Reinier Post Dec 03 '15 at 10:48
  • @ReinierPost I think that's approaching the problem slightly differently, by actually moving the entries on the grub boot menu around (such as moving old kernels out of the "previous versions" sub menu and into the main menu), which is preferred if you often use the Grub boot menu and want to change its setup or you often switch between two kernels. This answer solely deals with how to modify the GRUB_DEFAULT variable, which is probably preferred if you have grub auto-boot without showing the menu and don't care about the menu. – ananaso Jan 15 '16 at 15:13
  • 2
    these instructions worked great but were a bit confusing... to clarify for those coming behind me... I had to use the following -> GRUB_DEFAULT="1>Ubuntu, with Linux 4.2.0-27-generic"

    The '1' is to select the "Advanced"-menu; using it's text-equivalent didn't seem to work.

    (incidentally, this is the 2nd time in as many months that I allowed Ubuntu's auto-update feature to do it's thing and ended with a non-functioning box; WTF?)

    – Bane Feb 24 '16 at 16:16
  • 4
    It would be nice if the author authoritatively states which is the preferred method: numbers, text, or number+text (as @Bane did). A good answer shouldn't meander with, "let me explain using numbers, but actually you should use text." – Aleksandr Dubinsky Sep 28 '16 at 08:54
  • Sorry, I wrote this a while ago and haven't gotten around to doing much updating. I'll make that a little bit clearer. – ananaso Oct 02 '16 at 16:13
  • 1
    I just tried to make it more concise, please let me know what you think @AleksandrDubinsky – ananaso Oct 02 '16 at 16:32
  • @DaimyoKirby Thanks for updating it, it was clear to me. – Georg Schölly Jun 20 '18 at 09:15
  • It also works on Ubuntu 20.04, I tried it and it worked fine. – luisito_36 Oct 08 '20 at 16:08
  • works for ubuntu 22.04:
    1. get the kernel names grep -Ei 'submenu|menuentry ' /boot/grub/grub.cfg | sed -re "s/(.? )'([^']+)'.*/\1 \2/" (as in the answer of @Hannu
    2. edit the etc/default/grub file with: GRUB_DEFAULT="Advanced options for Ubuntu>Ubuntu, with Linux 5.19.0-50-generic"
    – B.Kocis Aug 19 '23 at 20:20
81

The best solution for me was to set (in /etc/default/grub):

 GRUB_DEFAULT=saved
 GRUB_SAVEDEFAULT=true

With this settings the last selected value is kept for the next boot. If you have a new kernel you don't have to edit your config.

Don't forget to re-run sudo update-grub

The solution is from reading: info grub-mkconfig

Martin T.
  • 929
37

Now that an Advanced menu is default in Ubuntu, you'll need to select it before the kernel you want using the > character.

Set e.g.:

GRUB_DEFAULT="1>7"

in /etc/default/grub and re-run sudo update-grub.

It is important to note that for the GRUB menu entries numbering starts with 0. Therefore the 1 above points to the Advanced menu. As a precaution, you may want to initially set GRUB_TIMEOUT=5. Some may be unable to access GRUB by hitting a key at boot time. This is a safety net in case you accidentally point to something like Memory test instead. It is also necessary to include the above numbers in quotes. It will not work otherwise.

I tested on 16.04 LTS.

jbrock
  • 3,307
sarnold
  • 1,841
  • 17
  • 20
  • Works in Debian too. – Lawrence D'Oliveiro Mar 16 '18 at 02:36
  • I find this answer very useful, as after a kernel update it still points to the newest kernel. in my setup for example it is 1>0, meaning: advanced-first listing (is always the newest kernel). my system sometime stopped to automatically update the 0 entry of grub with the newest kernel, so i use this method. – Peter Fleix Oct 13 '18 at 09:25
  • 1
    Unsure if this has been mentioned - but you can mix the numbers + string to select the Advanced menu AND the kernel version you want. eg GRUB_DEFAULT="1>gnulinux-5.4.0-121-generic-advanced-e17a595e-18f3-45e3-8f22-e0d1005fbae1" Currently the newest kernel is borked. This gives the confidence it will pick the right version each time on reboot. So this gets me out of a hole for the moment. – jsonUK Jul 15 '22 at 20:59
  • @jsonUK I think I tested the strings versions when preparing this answer and had trouble making them work. Granted, it's been six years :) Thanks for pointing it out. – sarnold Jul 18 '22 at 20:49
  • Thanks @sarnold, Seems this is good option and works me – jawad846 Aug 23 '23 at 12:39
  • Easiest answer and still works perfect today! :) +1 – Terrance Jan 21 '24 at 20:07
23

To be able to set which boot option to use as default you need to know what there is... so:

To display the menu entries without actually booting, try something like:

$ lsb_release -s -rdc
Ubuntu 16.04.4 LTS
16.04
xenial

$ update-grub --version
grub-mkconfig (GRUB) 2.02~beta2-36ubuntu3.17

$ grep -Ei 'submenu|menuentry ' /boot/grub/grub.cfg | sed -re "s/(.? )'([^']+)'.*/\1 \2/"
menuentry  Ubuntu
submenu  Advanced options for Ubuntu
    menuentry  Ubuntu, with Linux 4.4.0-34-generic
    menuentry  Ubuntu, with Linux 4.4.0-34-generic (upstart)
    menuentry  Ubuntu, with Linux 4.4.0-34-generic (recovery mode)
menuentry  System setup

Hannu
  • 5,374
  • 1
  • 23
  • 40
6

When I have initially posted this I haven't noticed the OP's specific requirement not to propose grub-customizer... Sorry.

For what it's worth I'll leave it here but also confirm that the best solution is indeed the most up-voted one HERE, with variables that can be found as indicated in THIS complementary answer.


In order to do it with a GUI, I use Grub Customizer (I'm in Mint but that doesn't count here).

The simplest and most direct way (thanks @eMKi for the suggestion in comment) — best suited for a permanent setting, in General Settings > default entry > predifined, simply select the desired kernel in the drop-down list.

enter image description here


Or, in case one needs the "previous booted entry" to be the one selected (might be useful in dual boot with Windows when doing an Windows update that requires restart, for example) — keeping the Advanced options in the grub list in case the default list is changed within Grub Customizer:

enter image description here

under General settings select the default entry: previous booted entry.

enter image description here

Then, reboot, and during boot select the "Advanced options" entry in the grub list:

enter image description here

and select the older kernel

enter image description here

On the next reboot, the "Advanced options" item will be the one selected by default in the grub list and the last selected kernel from under there will be booted (without opening the full list of kernels).

In case Windows is used, or other option in the grub list, remember to re-do the previous procedure.

cipricus
  • 3,444
  • 2
  • 34
  • 85
  • 1
    Hi, you could also just select a default boot entry from the list of available options in the 'Predefined' line. It depends, if you wish to have it selected permanently then that would be a good option. If you like to choose different entries then your way is better. Thanks for the GUI solution. – eMKi Jan 11 '21 at 19:17
  • @eMKi - I was not aware of that basic setting, which should be the most obvious. So, I added that to my answer. – cipricus Jan 12 '21 at 09:19
1

In order to change the default kernel to load you can set kernel boot priority in /etc/default/grub file.

First Take a backup of /etc/default/grub before making any changes.

While booting if you don't see Grub boot loader at all, you have to make it visible, for that change these 2 lines in Grub as

GRUB_TIMEOUT_STYLE=menu
GRUB_TIMEOUT=10

Now while loading you should be able to see the grub menu.

To change the kernel loading priority you have to make a change in GRUB_DEFAULT as the default setting it is set as

GRUB_DEFAULT=0

check the serial number of the kernel that you want to set as default, the numbering is 0 indexed, change the index accordingly. eg if you want to load the 6th entry from the top, you have to set the index as 5.

GRUB_DEFAULT=5

Now update grub using sudo update-grub and restart.

Check this YouTube video to make grub visible.

Check this Ubuntu help page to know about GRUB_DEFAULT.

Zanna
  • 70,465
  • This is not helpful for the described problem: the desired kernel is hidden inside the "Advanced options" sub-menu list. The "advanced" item in the main menu list can be selected by order with GRUB_DEFAULT but that is not the solution unless the desired kernel is first in the sub-menu. How do you select an item that is second or third in the "advanced" sub-menu? – cipricus Oct 26 '22 at 14:20
0

On Ubuntu 22.04, and possibly other distros & versions, you can configure grub to remember the last selected entry made at boot time, and use this as the default in subsequent boots.

To enable this, edit /etc/default/grub and add the following lines:

GRUB_SAVEDEFAULT=true
GRUB_DEFAULT=saved

Then run sudo update-grub to regenerate the boot files, and then reboot.

Now in grub, when you select a kernel, e.g. from the "Advanced options for Ubuntu" submenu, grub will automatically remember the selection for the next time, and then boot the selected kernel.

This selection is stored in /boot/grub/grubenv, and it should not be edited directly. If you need to modify it from a booted system, you can use sudo grub-editenv:

$ sudo grub-editenv list
saved_entry=gnulinux-advanced-20795ce5-2ede-4ea5-994d-c665c5037760>gnulinux-5.15.0-76-generic-advanced-20795ce5-2ede-4ea5-994d-c665c5037760

$ sudo grub-editenv set 'saved_entry=...'

The entry names can be copy/pasted from /boot/grub/grub.cfg, and note in particular the submenu's menuentry_od_option, followed by >, followed by the menuentry_id_option value of the selected item. Use quotes around the set string to prevent the shell seeing > as a redirect.

For example, this:

submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-20795ce5-2ede-4ea5-994d-c665c5037760' {
    menuentry 'Ubuntu, with Linux 5.19.0-46-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.19.0-46-generic-advanced-20795ce5-2ede-4ea5-994d-c665c5037760' {
        ...

Would result in the command:

$ sudo grub-editenv set 'saved_entry=gnulinux-advanced-20795ce5-2ede-4ea5-994d-c665c5037760>gnulinux-5.19.0-46-generic-advanced-20795ce5-2ede-4ea5-994d-c665c5037760'

The net effect of this feature is that grub, if left to boot automatically, will always pick the last selected boot option, which is a kind of default behaviour. Just be aware that temporarily selecting another kernel will change the default, but you can disable this by removing GRUB_SAVEDEFAULT from /etc/default/grub and regenerating the grub files with sudo update-grub, once you have your default kernel saved.

davidA
  • 101
0

To load with a specific kernel, follow the below steps,

  1. Check the available kernel by using the below command;
sudo grep menuentry /boot/grub/grub.cfg

From the list first kernel number starts from 0, and so on

  1. Edit GRUB configuration file
sudo vi /etc/default/grub

If you want to go with 5th kernel, then mention the GRUB_DEFAULT below

GRUB_DEFAULT='1>4'

save the file and exit the text editor

  1. update GRUB
sudo update-grub
  1. Reboot the Server, and to check the kernel version, use the below command;
uname -r
jawad846
  • 101
  • 3