There are some cases, where we play with the system and suddenly realizes that we messed up some thing. This post is about grub in Elementary Luna based on Ubuntu 12.04. So the process is the same for both.

Lets a take a simple scenario that you have messed up your grub and now when you boot your system you see this error:

error: no file found: >;

grub rescue>

This can be different, like: error: no device found. So now the questions is how to restore it. Well, this is simple. Yes, i know there are many tuts and threads about this issue over the net, but i did not find most of them very easy to follow OR is confusing. I will follow the easy way. So lets begin, shall we?.

Boot your system from Live CD of Elementary/Ubuntu. Open terminal and type:

sudo fdisk -l

Sample output:

Disk /dev/sda: 500.1 GB, 500107862016 bytes
  
255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
  
Units = sectors of 1 * 512 = 512 bytes
  
Sector size (logical/physical): 512 bytes / 512 bytes
  
I/O size (minimum/optimal): 512 bytes / 512 bytes
  
Disk identifier: 0x4f431832

Device Boot      Start         End      Blocks   Id  System
  
/dev/sda1           2048    83970047    41984000    7  HPFS/NTFS/exFAT
  
/dev/sda2        83970048    84174847      102400    7  HPFS/NTFS/exFAT
  
/dev/sda3   *    84174848   190670847    53248000   83  Linux
  
/dev/sda4       190672894   976773119   393050113    5  Extended
  
/dev/sda5       190672896   317648895    63488000    7  HPFS/NTFS/exFAT
  
/dev/sda6       317650944   968376319   325362688   83  Linux
  
/dev/sda7       968378368   976773119     4197376   82  Linux swap / Solaris

From the above command, note the partition where your OS is installed. Now lets create a directory to mount the root partition in it.

sudo mkdir /media/sda3

You can create this directory anywhere you want, like in your home directory etc. sda3 is the partition where your system is installed. Not necessary, you use sda3, can be any named directory, like /media/temp etc. Now lets mount the partition in the directory we created:

sudo mount /dev/sda3 /media/sda3

After mounting, we will install the grub by running this command:

sudo grub-install --root-directory=/media/sda3 /dev/sda

The /dev/sda  is your hard disk name, which is shown from the output of fdisk -l. Grub is installed. Reboot and things will be normal this time.

If every thing is cool, skip the following lines. The following are just extra steps in case some one did not made it.

=====================

If you see the following error after booting:

Error: unknown command gfxmode;

No worries, in latest Ubuntu 12.04, grub relies on a function in linux script which is in /etc/grub.d/ProxifiedScripts. BTW, this error is not a show stopper, you can still boot to your OS. The best way to avoid is install grub-customizer and enable script code.

sudo add-apt-repository ppa:danielrichter2007/grub-customizer
sudo apt-get update
sudo apt-get install grub-customizer

Open grub customizer and tick the check box in front of script code. And that will do it.