How can I get grub2 to boot a different option only on the next boot?

37,288

Solution 1

You need to make sure you have GRUB_DEFAULT=saved in your grub config (which is autogenerated, you change this part of it by editing /etc/default/grub). sudo update-grub to make the changes active and then use sudo grub-reboot $1.

grep -i "menuentry '" /boot/grub/grub.cfg

Will give you the menuentries that you can cut-paste in for $1. Or, as you note the options are zero-indexed, so the n-th menuentry can be had by passing (n-1) to grub-reboot.

See eg

Not tested by me as I don't have a 'Windows 7' install.

Solution 2

That is exactly what grub2-once does. Please try it. Seems grub2-once is only available for linux distros from SUSE.

shiraz-1:~/:[0]# grub2-once --list
     0 SLES 15-SP1
     1 Advanced options for SLES 15-SP1>SLES 15-SP1, with Linux 4.12.14-195-default
     2 Advanced options for SLES 15-SP1>SLES 15-SP1, with Linux 4.12.14-195-default (recovery mode)
     3 Advanced options for SLES 15-SP1>SLES 15-SP1, with Linux 4.12.14-95.54.1.19924.5.TEST.1167133-default
     4 Advanced options for SLES 15-SP1>SLES 15-SP1, with Linux 4.12.14-95.54.1.19924.5.TEST.1167133-default (recovery mode)

shiraz-1:~/:[0]# grub2-once 3
shiraz-1:~/:[0]# reboot
Share:
37,288

Related videos on Youtube

cwd
Author by

cwd

Updated on September 18, 2022

Comments

  • cwd
    cwd over 1 year

    I am setting up a dual booting headless Linux / Windows machine. I mostly want the machine to always boot up to Ubuntu, except for occasionally when I need to test something in windows.

    I've installed Windows and Ubuntu, and currently it boots up to Ubuntu every time. I would like to be able to tell it to boot to Windows on the next boot only and then when I restart from Windows have the system return to booting Ubuntu.

    I found Grub Legacy - section 4.3.1 - Booting once-only but I am using Ubuntu 14.04 server which has the newer version of grub. From what I've read, I need to make some changes to /etc/default/grub and use sudo grub-set-default when I want to boot to Windows.

    I think those changes start with adding this to /etc/default/grub:

    GRUB_DEFAULT=saved
    GRUB_SAVEDEFAULT=false
    

    Anytime changes are made to /etc/default/grub I know that I need to then run
    sudo update-grub and which results in:

    Generating grub configuration file ...
    Found linux image: /boot/vmlinuz-3.13.0-43-generic
    Found initrd image: /boot/initrd.img-3.13.0-43-generic
    Found linux image: /boot/vmlinuz-3.13.0-32-generic
    Found initrd image: /boot/initrd.img-3.13.0-32-generic
    Found memtest86+ image: /boot/memtest86+.elf
    Found memtest86+ image: /boot/memtest86+.bin
    Found Windows 7 (loader) on /dev/sda1
    done
    

    Since Windows is the 7th option in that list I believe I would run sudo grub-set-default 6 (0 based counting) - is this correct and is there a different method to get a numbered list of boot choices after the system is already booted?

    Also, are the lines in /etc/default/grub sufficient to achieve this behavior or do I need to add additional configuration?

  • cwd
    cwd over 9 years
    I saw that tool, but I am using Ubuntu 14.04 server ( no GUI ) so I'd love to have a command line solution.
  • Byte Commander
    Byte Commander over 9 years
    Oh, didn't know this. I use 14.10 standard (Unity) and I personally prefer GUIs for jobs like these. Do you know this site? It describes about what you wrote in your question, but if it differs from your first source, maybe there you find what exactly you need.
  • cwd
    cwd over 9 years
    -1 - I'm downvoting your answer because I wonder if you read the question at all. I can understand that you may have missed 'ubuntu server' in there but you just posted the same link that I included in my question in your last comment...
  • Byte Commander
    Byte Commander over 9 years
    Oops. But do you think every time I write a new comment or answer I re-read the whole question and former postings, checking every link!? Besides, you can also install DEs on server distros! I admit that I am no expert (yet), but at least I try helping others where I can, even if the answers are not always 100% correct or useful. You should be glad that there is anybody trying to help you solving this problem, but if you want to piss me off, you succeeded. One last thing: you talked about GRUB_SAVEDEFAULT=false, but I can't find this on our link. And you forgot other lines mentioned there!
  • cwd
    cwd over 9 years
    thanks i think this is what i need ( with additional details that I found at help.ubuntu.com/community/Grub2 )
  • rubo77
    rubo77 about 8 years
    If you happened to try this without changing the line to GRUB_DEFAULT=saved, you have to do this manually later like explained here in the second paragraph for Grub2 and then grub-set-default 0; grub-reboot 0
  • rubo77
    rubo77 about 8 years
    a command to show the numbers of the entry: grep -i "menuentry '" /boot/grub/grub.cfg|sed -r "s|--class .*$||g"|nl -v 0
  • Trent Nelson
    Trent Nelson over 6 years
    As per askubuntu.com/questions/838704/grub-reboot-to-specific-kerne‌​l, menuentrys under a submenu in /boot/grub/grub.cfg must be passed to grub-reboot as SUBMENU>MENUENTRY. So, for instance, using some bash-fu similar to @rubo77's, I wanted to boot menuentry 7, however this entry was under a submenu which was the second entry in the top-level menu, so I needed to pass '1>6' instead (single quotes to avoid the shell interpreting >).
  • nathan
    nathan about 5 years
    WARNING: This doesn't work if you boot off a BTRFS partition. Grub needs to be able to write to grubenv as it boots windows, it can't write to BTRFS. The file is typically stored in /boot/grub and can be viewed with grub-editenv. I fixed my system by mounting BTRFS in /, an EXT4 partition in /boot, and then the EFI partition in /boot/efi.
  • rubo77
    rubo77 about 3 years
    How do I get grub2-once installed? I cannot find it in Ubuntu 20.04. maybe only OpenSuSE?
  • rubo77
    rubo77 about 3 years
    NOTE: it is not clear, that you only need the line-number minus 1 of the visible grub options during start, so if Windows is the 3rd line, just use sudo grub-reboot 2; reboot
  • firo
    firo about 3 years
    Unfortunately, seems yes. It's a Perl script written by employees from suse.com . You could find it by searching for latest grub2 package from opnesuse distro on rpmfind.info
  • rubo77
    rubo77 about 3 years
    So better just use sudo grub-reboot 3; reboot