I like to have lots of choice about which operating system I can boot to. Between my desktop PC’s two hard drives, I have at least three distributions of Linux and several versions of Windows, so I have complete OS flexibility.

Unfortunately, maintaining a multi-boot configuration like this can be a pain, especially if you later install an operating system which overwrites the GRUB boot loader you had in place (such as a version of Windows). If your boot loader is overwritten, you could be left with no choice but to boot the most recently installed OS.

In this tutorial, I will show you how to restore an overwritten copy of the GRUB boot loader by using a Linux live CD. In this example, my master GRUB installation is on a Kubuntu 8.10 installation, and I’m using an older Kubuntu 8.04 Live CD I have lying around.

This tutorial does require you to have some understanding of how your multi-boot system operates, disk partitions and using the command line. If you are not confident, perhaps find a friend who knows Linux more intimately to do this process.

Before Starting

It’s worth mentioning that you may need to use a live CD somewhat similar to that of your 'master’ OS, where your boot loader configuration is stored. This is due to the technique we use to re-run the GRUB installer.

Also, you need to actually know which system holds the configuration file for your GRUB boot loader and on which partition it is located. If you have a more complicated multi-boot setup, like myself, you probably know this. If you have a more simple Windows-Linux dual boot, there should only be one Linux data partition where it could be.

Boot the Live CD

Start the Live CD up as normal. Don’t choose to install the OS if prompted, you want to come to a full live desktop to run the specific commands we need.

Identify your Partitions

You need to know on which partition the GRUB config file and associated programs are stored. You may wish to use a graphical program such as Gparted (if available). You’ll want to find out the device string (such as sda5) of the relevant partition.

(The screenshot above is actually from my triple-boot MacBook, but still shows you how you can identify the (ext3) partition of Ubuntu on that system.)

If you can’t use a graphical program to work this out, open a Terminal program and use the following command:

$ sudo fdisk -l

This will list all of the partitions on all the devices on your system. Under the 'System’ column, you can see all of the partitions labelled as 'Linux’. This won’t show you the difference between data and OS partitions, so is less useful in a more complex partition layout.

If you can work out where your Linux is from this, note down the information under 'Device’.

Mount the Partition

We now must mount your partition, so that we can access it. Some Live CDs may do this for you, or offer to do so, but here we will perform the process manually.

We will first make a folder in which the partition is mounted and then do the mounting. Replace the device string /dev/sda5 with the device string that you identified earlier.

$ sudo mkdir /mnt/system<br />
$ sudo mount /dev/sda5 /mnt/system

You should now be able to browse your hard drive by navigating to that folder. The next process we are going to perform is to temporarily change the root directory of our terminal (chroot), so that we can run the GRUB installer directly from the hard drive. It won’t even realise it’s not running from the real system

On Ubuntu and other sudo-based distros, we must first do this:

$ sudo -i

to become root fully (sudo is not enough here).

Doing the chroot

The GRUB installer requires to read the devices on disk directly in order to write the GRUB boot record back onto the system properly. It therefore needs a working copy of /dev, inside the mounted directory.

# mount -o bind /dev /mnt/system/dev

Next, we can run chroot:

# chroot /mnt/system

From this point forward, be very careful. You have root privileges and full write access to your hard drive. The usual caveats apply.

Run the GRUB Installer

All we need to do now is to simply run the GRUB installer, which plonks the GRUB boot record back on the hard disk and gives us back all of our choices.

# grub-install /dev/sda

If you need to install GRUB elsewhere (such as a different disk or a specific partition), change /dev/sda. In most cases, just leave this as-is.

GRUB should be re-instated on disk. You can now simply close your terminal, reboot the machine safely and everything should be back to normal.

Peter Upfold is a technology enthusiast from the UK. Peter’s interest in Linux stems back to 2003, when curiosity got the better of him and he began using SUSE 9.0. Now he runs Linux Mint 9 on the desktop, runs a CentOS-based web server from home for his personal website and dabbles in all sorts of technology things across the Windows, Mac and open source worlds.

Discussion: Restoring an Overwritten GRUB Boot Loader

Emil Begtrup-Bright(guest)

I installed Ubuntu 9.04 beside my 8.10 and WinXP, and it messed up the GRUB, so I can't acces my XP, and there is two versions of 8.10 under "other OS" in the GRUB menu, both an /sda1 and a /sda2 - and the first one doesn't work. So perfect, thanks!

SlackLuvr(guest)

Scott(guest)

Got a more complex problem. I have a two-disk setup, sda (first drive) has an old Ubuntu on it, sdb has a new openSUSE.

When I installed openSUSE, it installed the bootloader onto the MBR of sda, since it sees that drive first. But now I want to install Win7 RC onto sda. When I do, I will lose my grub for openSUSE on sda's MBR.

Should I simply follow these instructions to re-establish grub on the first drive after I install Windows 7, or is there a way of creating a MBR on sdb for openSUSE so I can just change the boot order in my bios?

Thanks!

Venkat(guest)

SantAnna(guest)

Hi Peter,
I have a way easier, I think.
If you loose the grub because you installed another sistem after linux (Example : Ubuntu) you can follow this tips:.
Reboot you PC with a Livecd.
Open a Terminal
Sudo su

grub

find /boot/grub/stage1
Depending of the result for example (hd0,4)

root (hd0,4)
setup (hd0)
quit

Voilá, Reboot you Pc and you will see the Grub again.

Hugs

Kaik541(guest)

having gone through the process of XP-->Vista-->Windows 7 pre-betas-->Windows 7 beta--->Windows 7 RC with Ubuntu normally juts sitting there not changing all that much, I've found the easiest thing to do is just use the grub command

sudo grub
find /boot/grub/stage1
root(hdX,Y)
setup(hd0)
quit

found that out somewhere on the ubuntu forums a while back I think an its far less complicated than the process you're describing. and it even if you install a new version of windows (or another os) in the same spot as the old one, then it will still work for them since all the windows chainloaders register the same in grub

Thanks for the link, this is what I've been looking for.
the menu is less then user friendly, so my recovery was more in the 30 sec range, and that's with two hard drives and three operating systems.
Nice.

Afshan(guest)

Phil(guest)

Worked a treat Peter - I guess reading all the other posts there may well be a faster way but was good following your very clear instructions for a newbie who just loaded Windows 7 which had wiped my grub out as it does so well :-)
Cheers M8...good stuff

pu_young(guest)

hi, i have a more complex one,i had window 7 on my laptop ,so i installed centOs on free partion created from the window 7 shrink. after installation i can not find the window 7 boot option during the boot processes. please can any one help me on this my email is pu_young@yahoo.com