How to Set Grub to Remember Your Last Selected Entry

How many times have you rebooted from Linux to load Windows? Those that dual-boot their system have done this quite a lot. The thing about dual-booting is that it relies heavily on the Grub bootloader and its entries. This gets worse when you realize that Windows has to restart a lot as it updates.

The result is users load windows, updates, and find themselves in Linux (or another installed operating system) thanks to Grub’s terrible default settings. Not to worry! There’s a way to set Grub to remember where you last left off! This means when you load Windows (or any operating system), and it restarts, it’ll load it again!

Back Up Grub

Before any configurations changes are made, it is a good idea to back up all Grub configurations just in case anything goes wrong. Assuming you are currently running Linux in your machine, start off by opening a terminal and making a backup directory.

mkdir -p ~/grub-backup

The above command will place a folder called “grub-backup” in the user’s home directory for future use.

With the backup directory made, it’s time to start copying configuration files. This first command will copy everything from “/etc/default/grub.” This is where most of the Grub configurations live.

cp /etc/default/grub ~/grub-backup

This last part of the backup process involves taking files from the “/etc/grub.d” directory. This is where the rest of Grub’s configurations are.

cp -a /etc/grub.d ~/grub-backup

Grub is now backed up. Changes (including the ones in this guide) can now occur without the fear of messing something up.

Editing Grub

Here’s how to set the Grub bootloader so it will remember the last booted item. Open a terminal and enter the following command:

sudo nano /etc/default/grub

Inside gedit, look for GRUB_DEFAULT=0. Change it to GRUB_DEFAULT=saved. Then, press the Enter key on the keyboard and paste this (using the keyboard shortcut “Ctrl + Shift + V”) below the GRUB_DEFAULT line:

GRUB_SAVEDEFAULT=true

After editing the grub file, press “Ctrl + o” to save and “Ctrl + x” to exit.

grub-edit-configuration-file

Other Ways to Tweak Grub

Setting Grub to remember the last booted item is a useful feature. However, there are other little tweaks you can do to Grub to make the bootloader more useful. Start off by opening the Grub configuration file with this command:

sudo gedit  /etc/default/grub

Changing the timeout time

grub-grub-timeout-time

Grub is useful, but for some, the menu loads way too fast. To change that, look for GRUB_TIMEOUT=5. The default is five seconds. Change this number to anything that sounds like it would be more comfortable (like ten or fifteen, for example).

Additionally, if the Grub timeout is way too slow, change it to three seconds for increased speed.

Hide Grub unless the user presses the Shift key

grub-hidden-menu-tweak

Some people absolutely hate the way Grub looks but understand they have to use it. Here’s a way to hide it. Add this line to the “/etc/default/grub” configuration file. From now on when the user reboots, the Grub menu will be totally hidden unless the “Shift” key is pressed.

Update the Changes in Grub

In some versions of Linux it is possible to update the changes in Grub by running update-grub. Not all Linux distributions support this though, and it is essentially an alias for a longer command. To update Grub on Ubuntu, open a terminal and enter the following command:

sudo update-grub

grub-update-grub-configuration-changes

To update Grub on operating systems that do not have an “update-grub” function, run this command in the terminal:

sudo grub-mkconfig -o /boot/grub/grub.cfg

Running these commands will tell Grub to pull from the new configuration changes and save them. Now, all you’ll need to do is reboot. From this point on, the Grub bootloader will remember the last selection that was booted.

Restore the Backup

To restore the backups made earlier in the guide, open a terminal and type the following:

sudo rm /etc/default/grub
sudo rm -rf /etc/grub.d
sudo cp ~/grub-backup/grub /etc/default/
sudo cp -a ~/grub-backup/grub.d/ /etc/

Once the configurations are copied from the backup, re-run the update-grub command or grub-mkconfig to restore the original configurations.

Conclusion

Grub is aging technology, that much is clear. A lot of simple features most would expect to be there by default are bolted on. Luckily, as there really isn’t another bootloader that anyone takes seriously (as of now), Grub gets all the love. This means that this bootloader will slowly get updates and new features as time progresses. Until then, users will have to settle with editing configuration files.

What do you think about Grub? Let us know below!

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Derrik Diener

Derrik Diener is a freelance technology blogger.