How can I tell GRUB I want to reboot into Windows—before I reboot?

93,734

Solution 1

In order for the grub-reboot command to work, several required configuration changes must be in place:

  • The default entry for grub must be set to saved. One possible location for this is the GRUB_DEFAULT= line in /etc/default/grub
  • Use grub-set-default to set your default entry to the one you normally use.
  • Update your grub config (e.g. update-grub).

This should take care of the initial set-up. In the future, just do grub-reboot <entry> for a one-time boot of <entry>.

Solution 2

  1. Edit the /etc/default/grub and replace GRUB_DEFAULT=0 with GRUB_DEFAULT=saved
  2. sudo update-grub
  3. Your command will be:

    sudo grub-reboot "$(grep -i windows /boot/grub/grub.cfg|cut -d"'" -f2)" && sudo reboot
    

    A pretty function for your ~/.bashrc or .bash_aliases could look like:

    # Reboot directly to Windows
    # Inspired by http://askubuntu.com/questions/18170/how-to-reboot-into-windows-from-ubuntu
    reboot_to_windows ()
    {
        windows_title=$(grep -i windows /boot/grub/grub.cfg | cut -d "'" -f 2)
        sudo grub-reboot "$windows_title" && sudo reboot
    }
    alias reboot-to-windows='reboot_to_windows'
    

    Editor's notes:

    • I have replaced deprecated backticks (``) by $(...) construct.

    • In general, I have re-written it to adhere to current POSIX (wiki) standards, and while at it, did a few minor other changes.

    • For completeness, I left the below function untouched for comparison.


In case, your grub.conf contains multiple lines for Windows, following functions will take care only about lines starting by menuentry and picking just the first one, referring to Windows:

function my_reboot_to_windows {
    WINDOWS_TITLE=`grep -i "^menuentry 'Windows" /boot/grub/grub.cfg|head -n 1|cut -d"'" -f2`
    sudo grub-reboot "$WINDOWS_TITLE"
    sudo reboot
}

Solution 3

Agree with @jw013. And you can also give the menu tile to grub-reboot (including the title of parent menu). e.g:

$ sudo grub-reboot "Advanced options for Ubuntu>Ubuntu, with Linux 4.13.0-26-generic"
$ sudo reboot

Where "Advanced options for Ubuntu" is the parent menu, "Ubuntu, with Linux 4.13.0-26-generic" is submenu.

Solution 4

As of Fedora 30, Roy's answer doesn't work anymore. Fedora entries are not listed as menuentry in grub.cfg and instead are taken from files in /boot/loader/entries. You can either lookup the entries with grubby --info=ALL or take the number of files in /boot/loader/entries.

Reddit thread Fedora Docs Boot Loader Specs

Here the modified script below (Fedora 30 and higher):

#!/bin/bash
if [ `readlink /boot/grub2/grubenv` == "/boot/efi/EFI/fedora/grubenv" ]; then
    sudo mv /boot/grub2/grubenv /boot/grub2/grubenv-original
    sudo ln -s ../efi/EFI/fedora/grubenv /boot/grub2/grubenv
fi
MENU_ENTRY=`grep ^menuentry /boot/efi/EFI/fedora/grub.cfg  | grep --line-number Windows`
FEDORA_ENTRIES=`grubby --info=ALL | grep index | wc -l`
#FEDORA_ENTRIES=`ls -1 /boot/loader/entries | wc -l`
MENU_NUMBER=$(( `echo $MENU_ENTRY | sed -e "s/:.*//"` + `echo $FEDORA_ENTRIES` - 1))
sudo grub2-reboot $MENU_NUMBER
sudo reboot

Solution 5

In Fedora, you can use the following script. Note that this is mostly the same as described in https://askubuntu.com/a/18186/149422, with a few modifications for GRUB 2 in Fedora.

#!/bin/bash
if [ `readlink /boot/grub2/grubenv` == "/boot/efi/EFI/fedora/grubenv" ]; then
    sudo mv /boot/grub2/grubenv /boot/grub2/grubenv-original
    sudo ln -s ../efi/EFI/fedora/grubenv /boot/grub2/grubenv
fi
MENU_ENTRY=`grep ^menuentry /boot/grub2/grub.cfg  | grep --line-number Windows`
MENU_NUMBER=$(( `echo $MENU_ENTRY | sed -e "s/:.*//"` - 1 ))
sudo grub2-reboot $MENU_NUMBER
sudo reboot
Share:
93,734

Related videos on Youtube

jadkik94
Author by

jadkik94

Enthusiast developer and FOSS supporter What I do/like to do: JLyr; Android Lyrics Application wxpos; Python Point-of-Sale Application coinbox; And another POS software, though much better ;) Other Stuff; Some game solvers, because computers love games too!

Updated on September 18, 2022

Comments

  • jadkik94
    jadkik94 almost 2 years

    I have a dual boot Linux/windows system set up, and frequently switch from one to the other. I was thinking if I could add a menu item in one of the menus to reboot directly into windows, without stopping at the GRUB prompt.

    I saw this question on a forum, that's exactly what I want but it's dealing with lilo, which is not my case.

    I thought of a solution that would modify the default entry in the GRUB menu and then reboot, but there are some drawbacks, and I was wondering if there was a cleaner alternative.

    (Also, I would be interested in a solution to boot from Windows directly into Linux, but that might be harder, and does not belong here. Anyway, as long as I have it in one way, the other way could be set up as the default.

    UPDATE It seems someone asked a similar question, and if those are the suggested answers, I might as well edit /boot/grub/grubenv as grub-reboot and grub-set-default and grub-editenv do. )

    Thanks in advance for any tips.

    UPDATE:

    This is my GRUB version: (GRUB) 1.99-12ubuntu5-1linuxmint1

    I tried running grubonce, the command is not found. And searching for it in the repositories gives me nothing. I'm on Linux Mint, so that might be it...

    Seeing man grub-reboot, it seems like it does what I want, as grubonce does. It is also available everywhere (at least it is for me, I think it is part of the grub package). I saw two related commands: grub-editenv and grub-set-default.

    I found out that after running sudo grub-set-default 4, when running grub-editenv list you get something similar to:

    saved_entry=4
    

    And when running grub-reboot 4, you get something like:

    prev_saved_entry=0
    saved_entry=4
    

    Which means both do the same thing (one is temporary one is not).

    Surprisingly, when I tried:

    sudo grub-reboot 4
    sudo reboot now
    

    It did not work, as if I hadn't done anything, it just showed me the menu as usual, and selected the first entry, saying it will boot this entry in 10s.

    I tried it again, I thought I might have written the wrong entry (it is zero-based, right?). That time, it just hanged at the menu screen, and I had to hard-reset the PC to be able to boot.

    If anyone can try this out, just to see if it's just me, I'd appreciate it. (mint has been giving me a hard time, and that would be a good occasion to change :P).

    Reading the code in /boot/grub/grub.cfg, seems like this is the way to go, but from my observations, it's just ignoring these settings...

    • jw013
      jw013 almost 12 years
      what distro are you on? I think i've got it working on my Debian.
  • jadkik94
    jadkik94 almost 12 years
    I turned off Windows update, so that should not be a problem :D, I'll try this. From what I saw, it seems like this does the same thing as grub-reboot. I'm editing my question with more detail. Thanks anyway.
  • Jan Vlcinsky
    Jan Vlcinsky over 9 years
    My file /boot/grub/grub.cfg contains more lines with "Windows" in it, many of them stating something like ...=1 i915.semaphores=1 acpi_osi='!Windows 2012' $vt..., so I assume your code would fail on it. This can be fixed by grepping for ^menuentry 'Windows.
  • Taha Rehman Siddiqui
    Taha Rehman Siddiqui about 8 years
    This should be marked as the correct answer.
  • zhangxaochen
    zhangxaochen almost 8 years
    I see ur words "default entry for grub must be set to saved", while my keeping GRUB_DEFAULT=0 also sufficed. So is saved unnecessary?
  • Brian Thomas
    Brian Thomas over 7 years
    Im not sure if everyone realizes how cool this feature really is, I plan to boot win7ult64 with rhel64, and vice versa, whilst having the rhel become auto available for another win networked systems scheduled backup task, which pushes to the rhel zfs raidz2. Then at backup complete, the grubbed system auto boots back into a windows gamer iis server playground for the rest of the week, rinse and repeat. Its a shape shifter. :-)
  • Carson Ip
    Carson Ip about 7 years
    The function is more helpful than I thought.
  • Nobody
    Nobody about 7 years
    It took me ages to notice that grub-reboot fails silently, giving a success return code. I hope my suggested edit is ok. :-)
  • Dylan Smith
    Dylan Smith over 5 years
    I edited my post to include details of installing and using the extension.
  • redanimalwar
    redanimalwar about 5 years
    Its actually part of grub and trustedgrub (whatever that is) and NOT grub2 so I do not have it installed on tumbleweed june 2019
  • AlexOnLinux
    AlexOnLinux about 4 years
    sudo grub-reboot '0>3' or sudo grub-reboot '1>3' did not work but sudo grub-reboot 'Advanced options for Ubuntu>Ubuntu, with Linux 4.4.0-179-generic' did. Thank you.
  • XANi
    XANi about 3 years
    That doesn't appear to be the case anymore, at the very least in Debian Buster grub version neither running update-grub nor having default set to saved was necessary, grub-reboot <entry> was all that was needed for me
  • adlr0
    adlr0 almost 3 years
    Sadly, that extension is now unmantained and not compatible with modern versions of GNOME.
  • Admin
    Admin about 2 years
    "This will not work on gnome shell 3.34+."