41

I'm looking for a way to reboot into Windows from Ubuntu on a 10.10/Vista dual boot system. The specific use case is that I would like to be able to ssh into my running Ubuntu instance and issue a command that will initiate a reboot directly into Windows.

I found a promising blog post, but the script that it suggests isn't working:

#!/bin/bash

WINDOWS_ENTRY=`grep menuentry /boot/grub/grub.cfg  | grep --line-number Windows`
MENU_NUMBER=$(( `echo $WINDOWS_ENTRY | sed -e "s/:.*//"` - 1 ))
sudo grub-reboot $MENU_NUMBER
sudo reboot

man grub-reboot isn't much help, but it seems to be leading me in the right direction:

set the default boot entry for GRUB, for the next boot only

WINDOWS_ENTRY=`grep menuentry /boot/grub/grub.cfg  | grep --line-number Windows`
MENU_NUMBER=$(( `echo $WINDOWS_ENTRY | sed -e "s/:.*//"` - 1 ))
echo $MENU_NUMBER

This returns the expected value, but on reboot the first menu entry is still highlighted. Any ideas why this isn't working or suggestions for other solutions?

Organic Marble
  • 23,641
  • 15
  • 70
  • 122
  • If you copy the windows grub boot line to the top of the list in the grub menu then on each boot windows will be the default load.. if you're in ubuntu and ssh into it then.sudo reboot now should reboot the machine into the first item in the grub list. – karthick87 Dec 19 '10 at 21:05
  • Sure, but I rarely boot into that Windows partition. I don't want to make it the default entry. – andrewsomething Dec 19 '10 at 21:33

7 Answers7

52
  • You have to edit your grub first.

    sudo gedit /etc/default/grub
    
  • Search for the line GRUB_DEFAULT=0 and modify it to GRUB_DEFAULT=saved alt text

  • Update your grub using the following command.

    sudo update-grub  
    
  • Now create a script file,

    sudo gedit switch-to-windows.sh
    
  • Then add these lines.

    #!/bin/bash
    WINDOWS_ENTRY=`grep menuentry /boot/grub/grub.cfg  | grep --line-number Windows`
    MENU_NUMBER=$(( `echo $WINDOWS_ENTRY | sed -e "s/:.*//"` - 1 ))
    sudo grub-reboot $MENU_NUMBER
    sudo reboot
    
  • Make the script executable.

    sudo chmod +x switch-to-windows.sh
    
  • And now you can run this script from terminal to reboot into windows.

    ./switch-to-windows.sh
    
  • Or you can execute the following command in your terminal

    sudo grub-reboot X  
    
  • Where X is the menuentry position of the OS you want to restart in from the GRUB menu.(starting with 0 as the first entry)

For Example:

  • If this is your grub menu and if you want to boot into windows you should give the value of X as 5.
  • sudo grub-reboot 5

    alt text

  • You can also create a launcher for the above command,so that double clicking the launcher will reboot into windows.
karthick87
  • 81,947
  • I can get it to work once. After that you have to enter passwords. –  Jan 02 '11 at 19:37
  • 7
    How did you get grub to look like that? – Mandy May 14 '11 at 01:03
  • follow up the question above? how can you get grub looking so perty? – Diego Aug 08 '12 at 21:30
  • 5
    Solution is really great, but as I have 14 menu entries with tab before entries, with the title insthead of menu entry number worked better. So you can change your code with
    #!/bin/bash WINDOWS_TITLE=grep -i 'windows' /boot/grub/grub.cfg|cut -d"'" -f2 sudo grub-reboot "$WINDOWS_TITLE" sudo reboot
    – Philippe Gachoud Feb 03 '14 at 11:20
  • Philippe Gachoud, please make yours an answer! it is the only one that works! – honi Apr 05 '17 at 19:39
  • 1
    There is no reason to set GRUB_DEFAULT=saved, it would mess up your configuration. What grub-reboot does is to write next_entry=ENTRY to /boot/grub/grubenv. That's not saved_entry=ENTRY. Next reboot will be fine though, grub reads next_entry. Following boots may fail if no entry is saved as saved-entry=ENTRY in grubenv. – mook765 May 28 '19 at 05:35
  • On my Ubuntu 20.04 system there are lines that contain "menuentry" but aren't menu entries, so MENU_NUMBER wouldn't be correct. I think the WINDOWS_TITLE method, mentioned in another comment, works better now – Xor Jul 15 '20 at 21:34
  • For Linux Mint 19.3 (Ubuntu 18.04), I used the same script but replaced WINDOWS_TITLE line with: WINDOWS_ENTRY=$(grep -iE '^menuentry' /boot/grub/grub.cfg | grep -iE --line-number '*' | head -n 1) – lolsky Oct 09 '20 at 01:49
12

There is a grub command just to do so, it is grub-reboot.

It seems to only work when you have grub configured to start with the last saved entry. So if you have not already done so, modify /etc/default/grub and set

GRUB_DEFAULT=saved

then update grub configuration file:

sudo update-grub

From now on, at each boot grub will start the last used entry.

Now, if you want to set in advance what should be the system to boot the next time, use

sudo grub-reboot ENTRY

where ENTRY could be a number relative to a menu entry (numbered starting from 0), or an exact menu entry title, for example

sudo grub-reboot "Microsoft Windows XP Professional (on /dev/sda1)"

This command can easily be made available as a launcher

#!/usr/bin/env xdg-open
#
# save as ~/Desktop/reboot-into-windows.desktop
#

[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Exec=sh -c 'gksu "grub-reboot 2" && gnome-session-save --shutdown-dialog'
Name=Reboot into Windows
Icon=gnome-panel-launcher

but I don't know how it could be integrated into the system menu.

You can obtain the available menu entry title with

sed -n '/menuentry/s/.*\(["'\''].*["'\'']\).*/\1/p' /boot/grub/grub.cfg 
enzotib
  • 93,831
  • 1
    There is no reason to set GRUB_DEFAULT=saved, it would mess up your configuration. What grub-reboot does is to write next_entry=ENTRY to /boot/grub/grubenv. That's not saved_entry=ENTRY. Next reboot will be fine though, grub reads next_entry. Following boots may fail if no entry is saved as saved-entry=ENTRY in grubenv. – mook765 May 28 '19 at 05:31
4

I think I have found an even nicer way for people who want to the same while locally at their pc without ssh.

A solution to reboot into a specific system choosen through a unity launcher was just posted on webupd8. See http://www.webupd8.org/2011/05/custom-unity-launcher-to-reboot-in.html

I know this is not exactly what the question is about but in case someone has a similar question later this might be helpful.

LayerCake
  • 1,046
4

So for me the best was to create following script with grub-reboot command

#!/bin/bash 
WINDOWS_TITLE=$(grep -i 'windows' /boot/grub/grub.cfg|grep "^[^#;]"|cut -d"'" -f2) 
sudo grub-reboot "$WINDOWS_TITLE" 
echo "Your computer will reboot on ${WINDOWS_TITLE} in 3 seconds, press Ctrl+C if you want to abord it"
sleep 3 && sudo reboot
Philippe Gachoud
  • 5,900
  • 3
  • 43
  • 50
0

https://gist.github.com/weezah/73ca3e69a3f5a83c3616aeb373d20efe

you can use this script! Cheers

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review – Artur Meinild Sep 22 '23 at 09:58
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Community Sep 22 '23 at 11:20
-1

I'm not sure if this will 100% also work in ubuntu, but I once made this work on a centos machine

basically you'll have to tell grub which id # from grub.conf it should run next. The id # can be found out by

echo `sudo grep ^title /boot/grub/grub.conf | grep -n Windows | cut -f 1 -d:`-1

so in my case this would e.g. output "4-1" as the fourth entry in my grub.conf contains the text "Windows". (Replace this with the actual entry name (also partially) you want to use. Then you can feed this to grub like:

echo "savedefault --stage2=/boot/grub/stage2 --default=4-1 --once" | sudo /sbin/grub

and at the next reboot it should automatically boot the entry you've told it to.

this all can be put together in a script like the following:

#!/bin/sh
let NEWBOOT=`sudo grep ^title /boot/grub/grub.conf | grep -n Windows | cut -f 1 -d:`-1
echo Booting $NEWBOOT - `sudo grep ^title.\*Windows /boot/grub/grub.conf`
echo "savedefault --stage2=/boot/grub/stage2 --default=$NEWBOOT --once" | sudo /sbin/grub
sudo reboot

the script will automatically determine which entry from your grub.conf corresponds to the text "Windows", will feed this to grub to tell what should be loaded on next boot and will reboot your pc.

for a gui you could e.g. add a shortcut icon to your desktop to achive this.

  • 1
    The config file for grub-legacy is menu.lst, not grub.conf. For grub2, it is grub.cfg, but your second command is only for grub-legacy. – psusi Sep 26 '11 at 15:46
-4

Simply edit /boot/grub/grub.cfg and change the default entry to point to the entry you want to boot. The value can either be the ordinal number of the entry (starting from zero for the first one) or you can place the full title of the entry you want in quotes.

enzotib
  • 93,831
psusi
  • 37,551
  • 2
    Don't advice to modify boot.cfg, it will be restored at each manual or automatic update-grub. User should modify /etc/default/grub, instead. – enzotib Sep 26 '11 at 16:46
  • 1
    @enzotib, since the change is only meant to pertain to the next boot, there is no reason to do it that way. – psusi Sep 26 '11 at 17:14
  • 2
    you forget that a wrong editing on that file could lead to an unbootable system – enzotib Sep 26 '11 at 18:08
  • 1
    @enzotib, not really; one of the beautiful things about grub2 is that you can always recover manually at the prompt. Also the danger of screwing up other parts of the file is a specious argument, and if you get the default line wrong, the worst that happens is that it defaults to the first entry. – psusi Sep 26 '11 at 18:11