How to restore GRUB2 using an Ubuntu Live CD or Thumb Drive 09/02/2010

If you are a tinkerer like me, you will no doubt run into some problems with grub at one time or another.
Even though you do not tinker, you might still have a problem with grub especially if dual-boot your system with windows. In that case, the windows boot-loader overwrites GRUB in the Master boot record (MBR) (if you install Ubuntu first) thereby rendering your Linux partition not bootable. It has been a major bottleneck for a lot of Ubuntu users when they can no longer boot into their desktop and most times are forced to reinstall.

It is however, relatively easy to restore GRUB on your computer irrespective of how you lost it in the first place.
The only requirements are that you still have a healthy installation of Ubuntu on your machine and a ready live CD or a USB thumb drive with Ubuntu loaded on it.

I will assume that the readers of this post have some level of knowledge on how to get certain things done on Ubuntu so i will not be too specific in some areas. if anything is unclear, you can ask it in the comments and i will be glad to clarify.

First, you have to boot the live CD. If you are using a thumb drive, also boot from it.
After booting, you have to determine which of your hard-disk partitions is the root (/) partition. You can do this by typing

Code:

sudo fdisk -l

in the terminal.
Note: If you only have one partition or you already know the address of your root partition, you can skip this step.

Admittedly, this can be a little confusing and might not really tell you which of your partitions is the root partition. Most of the time, what i do is to open up the file manager (nautilus) and check for the partition that resembles my root partition either as a result of the size or maybe a label (if you’re d kinda person that does that). i will then mount that partition from nautilus. After that, i go to the terminal and type the following

Code:

$ cat /etc/mtab

Amongst all the text that will be printed, the one containing the address of your root partition will be there. This is the output from my own system. Note that this will not show up if the partition is not mounted so ensure that only the root partiton is mounted so u dont get confused. alternatively, you can use gparted to also know the partiton address of the root partition.

The line showing my root partition is highlighted in bold on d text above. From this, you can deduce the address of your root partition. I can see that mine is /dev/sda5. Once this has been done, the next step is to mount that partiton in a more easily accessible place. Ubuntu will by default mount the partition according to its UUID (Universally Unique Identifier) in the /media folder as you can see above. However, this makes for a very long file path and makes it very easy to make a mistake.

Go to nautilus again and this time, unmount the previously mounted partition.

Then, use the terminal to mount that partition in the /mnt folder.
In my case, i will use the following command

after this, it is time to install grub2. To do this, use the following command

Code:

$ sudo grub-install --root-directory=/mnt /dev/sda

This command should not be edited if you followed all the steps above. this installs grub2 in the MBR of the Harddisk. This will take a short while after which a confirmatory message will come up.

That is it. To confirm your settings, type in

Code:

$ sudo update-grub

in the terminal. However, even if this gives an error message, most of the time, the installation is already done. If you receive an error message though, you might not see other operating systems on your Computer especially if they were installed after the Ubuntu installation. To remedy this, boot into your freshly repaired Ubuntu and run the above command from there. That should sort it out.

So, that’s it. I hope this helps someone out there get out of a bind. Any further questions can be posted on the comments.

Like this:

Related

Great post! I’ve done this several times. This works with most if not all distros. However, I will like to add that should you wish to still continue dual booting after this procedure, you might have to edit your /boot/grub/grub.conf or /boot/grub/menu.lst (which is a symbolic link to /boot/grub/grub.conf anyway).

thanks. This post is for grub2. in grub2, /boot/grub/menu.lst has been replaced with /boot/grub/grub.cfg which is not edited but instead, the file in /etc/defualt/grub is edited and then, running “update-grub” applies the changes to /boot/grub/grub.cfg. Grub2 has been the default in ubuntu since 10.04 i think. however, for any older version of ubuntu (and other distros) running grub legacy, the steps you outlined in your comments would work.

For adding new OSes in grub2, simply running update-grub would suffice most of the time as it will auto detect all the OSes on the computer and let you know the ones it detects. This is an improvement on grub-legacy