How can I change the default boot menu option from the GRUB command line?

How can I change the default boot menu option from the GRUB command line?

I have a dual Windows 7/Ubuntu system which I almost always boot into Windows.

When I boot into Ubuntu it makes memtest the default boot menu option. While this is annoying, I could live with it if I were able to change the default back from the GRUB command line available from the boot menu. I don't want to have to boot into Ubuntu to fix this problem every time.

I am able to find the GRUB configuration file and can see the contents (sort of) by using:

cat (hd0,5)/boot/grub/grub.cfg

(although this only shows me the 'tail', not the whole file).

And the command

configfile (hd0,5)/boot/grub/grub.cfg

executes this configuration (again).

Is it possible to edit this file from the GRUB command line?

The GRUB (1.99) manual suggests that I can use the Environment Block to:

set the default menu entry based on what was selected the last time

which may fix my problem, but I can't find any examples of their use... Can I somehow use the save_env and load_env commands?

Note: I have GRUB version 1.98

答案1

No, there isn't any way to change the default boot menu from the GRUB command line. The easiest way to do this is by booting into Linux.

Next time you boot, look at the order of boot options. Let's say you want to make Windows the default boot option. Note down the line it is on, i.e., the fourth or whatever.

Edit /etc/default/grub.

Find the line -

GRUB_DEFAULT=0

Subtract 1 from the number you noted above. Let's say it was 4 and change the text to read:

GRUB_DEFAULT=3

Run update-grub2 from the shell.

答案2

Using the Super Grub Disk is easier than using a live CD.

See this tutorial : GRUB 2 recovery.

答案3

To change the default boot of Backtrack 5 R3 to Windows 7 or any other OS, you must first make a note of what line in the list the option is on as you will need it in step 2.

My Windows 7 boot option is on line 5. Let's open the file we need to edit, as root:

gedit /boot/grub/grub.cfg

Then find the following line (it's on line 12 in mine)

set default="0"

Now subtract 1 from your chosen boot menu line I asked you to remember earlier. Mine was on line 5 so I change the "0" to a "4"

set default="4"

Now click on save, close down everything you have open and restart your computer to check it has worked. You should now find your chosen boot option highlighted.

答案4

This can be done using the GRUB command line tools.

Step 1: Find the number of your boot selection, as root:

grep "^menuentry" /boot/efi/EFI/redhat/grub.cfg

Start counting from zero to find the number of the menu entries that you want.

Step 2: Set the default. Given that the menu item that you want is number 1 (i.e., the second in the list), run the following command:

grub2-set-default 1

It's also worth noting that there may be multiple grub.cfg files on your system. For example, under Red Hat, CentOS, and Fedora there is one for legacy boot and one for EFI boot.

find /boot -name grub.cfg

Output:

/boot/efi/EFI/redhat/grub.cfg
/boot/grub2/grub.cfg

So, if you find yourself banging your head against your desk because the default boot selection never changes despite following all of the advice you've found, see if you might be modifying the wrong grub.cfg file.

It is also worth noting is that if you've completely hosed your grub.cfg file, you can rebuild it with grub2-mkconfig (e.g., grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg).

相关内容