in Technology

Restore Grub in Ubuntu after installing Windows

When you install some operating systems such as Windows or Mac OS X, they will overwrite your master boot record. If you have Linux installed you will be unable to access it, until you restore Grub on the MBR.

The easiest way is to find your Ubuntu or other Linux rescue/live/installation CD and boot it. Pop open a terminal and first find which partition is your root using fdisk.

$ sudo fdisk -l
Device Boot Start End Blocks Id System
/dev/hda1 * 1 2550 20482843+ af Unknown
/dev/hda2 2551 3825 10241437+ 83 Linux
/dev/hda3 3826 3890 522112+ 82 Linux swap / Solaris
/dev/hda4 3891 12161 66436807+ 7 HPFS/NTFS

Here we see that /dev/hda2 is our Linux partition, so we will want to mount it in order to read the Grub configuration.

sudo mount /dev/hda2 /mnt

If your don’t get any errors now your root partition should be mounted under /mnt. You can check for the grub configuration by listing the contents of the directory /mnt/boot/grub.

Now to restore the configuration just write the following.

$ sudo grub-install —-root-directory=/mnt /dev/hda

This will install Grub to MBR and use the configuration files in /mnt/boot/grub. Now just reboot your computer and you will hopefully be greeted with the ever friendly Grub menu once again.

Comment

Comment

20 Comments

  1. [quote comment=”19934″]Might want to note that its –root-directory not -root-directory

    Other than that this saved me lots of time[/quote]
    Thanks for noticing. It seems like WordPress is suppressing one dash if there are two in a row. Maybe three in a row is the correct recipe.

    Edit: Four ones did it good enough. Thanks again.

  2. [quote comment=”20040″]I tried this just now and got:
    “/dev/hda does not have any corresponding BIOS drive.”

    Any ideas?[/quote]
    Well, I was a bit unclear about that I realize. You have to install it on the device that is reported by fdisk -l. My best guess is that you should use /dev/sda instead.

  3. Tack sÃ¥ mycket. Räddade min kväll idag dÃ¥ 😉 Hade dock en sata-disk sÃ¥ det var sda istället för hda … 🙂

  4. [quote comment=”46986″]Tack sÃ¥ mycket. Räddade min kväll idag dÃ¥ 😉 Hade dock en sata-disk sÃ¥ det var sda istället för hda … :)[/quote]
    Kul att du hade nytta av den. Ja, alla har ju sata nuförtiden, så jag borde kanske byta i guiden.