Restoring an Overwritten GRUB Boot Loader

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.

GParted screenshot

(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.

Fdisk -l screenshot

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 splash

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.

Avatar for peter Peter Upfold - http://peter.upfold.org.uk/

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.

Home » Articles »

Discussion: Restoring an Overwritten GRUB Boot Loader

  1. Emil  Begtrup-Bright (guest)

    # Posted on 06 May 2009 at 11:16 AM

    Thank you, just what I needed :)

    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!



  2. Pallab (guest)

    # Posted on 06 May 2009 at 01:30 PM

    I really needed this as frigging windows keeps deleting grub everything I install it. <Goes into my delicious bookmarks list>



  3. Laurent (guest)

    # Posted on 06 May 2009 at 01:55 PM

    http://www.howtogeek.com/howto/ubuntu/reinstall-ubuntu-grub-bootloader-after-windows-wipes-it-out/

    much easier tutorial.



  4. PetFoodz.Info (guest)

    # Posted on 06 May 2009 at 02:25 PM

    I agree with Laurent above.. I whip in my 8.10 alt CD I had.. Recover GRUB.. Mount .. Reinstall to MBR I think.. Can be done it about a minute..



  5. Neal Jansons (guest)

    # Posted on 06 May 2009 at 03:53 PM

    Nice post, newbies will really benefit. Shared on FF and Twitter.



  6. SlackLuvr (guest)

    # Posted on 06 May 2009 at 05:15 PM

    GRUB Partition, FTW!



  7. Scott (guest)

    # Posted on 06 May 2009 at 07:58 PM

    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!



  8. Venkat (guest)

    # Posted on 06 May 2009 at 08:40 PM

    Great Post dude ! Well written .... Keep posting .... Thanks Venkat



  9. SantAnna (guest)

    # Posted on 06 May 2009 at 10:13 PM

    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



  10. Kaik541 (guest)

    # Posted on 07 May 2009 at 07:02 AM

    I'm sorry, but I have to agree with SantAnna

    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



  11. Aj (guest)

    # Posted on 07 May 2009 at 08:56 AM

    I too agree with Sant Anna. It is as simple as that. I have done it many times when I reinstall my Windows



  12. Bsmith1051 (guest)

    # Posted on 07 May 2009 at 04:21 PM

    OK, so who's copying who? http://webupd8.blogspot.com/2009/05/fix-restore-grub-boot-loader.html http://fosswire.com/post/2009/5/restoring-overwritten-grub/



  13. Lantesh (guest)

    # Posted on 08 May 2009 at 02:54 AM

    Very nice. Thank you.



  14. Zepol (guest)

    # Posted on 17 May 2009 at 01:52 AM

    I can't believe no one mentioned this:

    http://www.supergrubdisk.org/

    I can get grub back in like 10 sec with Super Grub



  15. # Posted on 19 May 2009 at 07:10 PM

    Zepol said:

    I can't believe no one mentioned this:

    http://www.supergrubdisk.org/

    I can get grub back in like 10 sec with Super 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.



  16. bobp (guest)

    # Posted on 30 May 2009 at 10:19 PM

    however this route offers more options than above methods bar the super grub disk.



  17. Afshan (guest)

    # Posted on 30 July 2009 at 11:26 AM

    Thanks

    this was very usefull to recover my CentOs installation. great job keep it up



  18. Phil (guest)

    # Posted on 25 October 2009 at 11:57 AM

    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



  19. Amateur_dev (guest)

    # Posted on 20 July 2010 at 08:38 AM

    One small step if i may add.I have dual boot xp/ubuntu.

    I made changes to grub so that by default windows xp is selected in grub menu.

    now if i go in for your method,as suggested we reinstall grub. Im assuming I have to rewrite the little script i did to get a different default os onto grub menu?



  20. Blogger (guest)

    # Posted on 23 November 2010 at 12:22 PM

    Visit this...Nice explanation of Recovering GRUB after windows XP installation.

    http://jaydeepwagh.blogspot.com/2010/05/recovering-grub-bootloader-after.html



  21. pu_young (guest)

    # Posted on 08 December 2010 at 11:40 AM

    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

    thanks



  22. AswadKannav (guest)

    # Posted on 23 May 2011 at 01:38 PM

    Hi - I am really glad to find this. Good job!



  23. # Posted on 17 August 2011 at 01:41 AM

    WOW!!!! Thanks you :-) Great Job!



  24. # Posted on 17 August 2011 at 01:45 AM

    I'm using Linux mint and Windows 7. After windows startup repair i lost my grub menu list...

    This Tip worked like a charm :)Thanks alot Peter.



Home » Articles » Restoring an Overwritten GRUB Boot Loader