41

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?

J. Starnes
  • 1,979
cwd
  • 1,663
  • 5
  • 19
  • 24

4 Answers4

43

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). Then run this command to make the changes active:

sudo update-grub

and then use:

sudo grub-reboot $1

where $1 is the menuentry you want to boot in on the next reboot. To get the menuentries that you can cut-paste in for $1 run:

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

Or, you may note that the options are zero-indexed so to boot to the n-th menuentry you can pass n-1 to grub-reboot.

See for example:

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

pbhj
  • 3,364
4

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

firo
  • 1,928
0
awk -F\' '/^menuentry / {print $2}' /boot/grub/grub.cfg|cat -n|awk '{print $1-1,$1="",$0}'

That'll give you the numbers and names of the available grub boot menu options.

-4

Try installing grub-customizer (installation guide below) to your Ubuntu system. This GUI tool will provide you a nice set of options with that you can configure your GRUB settings, including names and order of the boot options, default system, screen resolution, fonts, background etc...
I am not sure if it has the ability to directy set what you want, but at least it can show you the os numbers. They can be referenced by their name too, I think.

How to install grub-customizer:

sudo add-apt-repository ppa:danielrichter2007/grub-customizer
sudo apt-get update
sudo apt-get install grub-customizer

(source: launchpad - daniel richter)

Byte Commander
  • 110,243