90

Okay, so I did something stupid. I was trying to clean up my Grub entries, and accidentally removed all of my Linux kernels from Grub (they're still on the hard drive). So now, obviously, Grub doesn't give me any way to boot into Ubuntu; I can boot into Windows just fine, but Ubuntu isn't even listed.

So I just want to run "sudo upgrade-grub" somehow to restore Ubuntu to the list. I can boot from a LiveCD, but once there how do I run that command? (My Ubuntu installation is on sda5, by the way.)

Kelley
  • 33,062
  • This works perfectly...did it just 2 days ago- http://opensource-sidh.blogspot.in/2011/06/recover-grub-live-ubuntu-cd.html – Nirmik Jun 01 '12 at 21:37
  • at this thread want to point to - that at 12.10 command update-grub does not work with grub < 2.0 ?! - I myself had last weekend botched up my installation of 12.10 ( pear OS 7.0.1 ) with grub-install and then update-grub was not working - had to do rescue with supergrub-CD ... – dschinn1001 Jul 02 '13 at 17:57
  • 1
    @Nirmik link is dead :(. – pevik Jun 05 '20 at 05:47
  • 1
    @pevik https://web.archive.org/web/20120427150352/http://opensource-sidh.blogspot.in/2011/06/recover-grub-live-ubuntu-cd.html – localhost Jun 20 '23 at 10:41

7 Answers7

119

Since you say your grub bootloader appears, but the menu is empty, I think you don't need to reinstall grub, but rather, as you ask, run update-grub. To achieve this, you can use a Live CD, mount the relevant partitions from your hard disk, chroot into the mounted directory, and run update-grub, which should work as if you were operating on the actual hard disk.

Boot with your Live CD, selecting "Try Ubuntu without installing".

Once it boots, open a terminal (ctrl-alt-t) and mount your Ubuntu partition on /mnt. I'm assuming the Ubuntu partition is /dev/sda5, but you should determine this yourself. Let me know if you need help to do this:

sudo mount /dev/sda5 /mnt

Then mount a few more directories that are needed:

sudo mount --bind /dev /mnt/dev
sudo mount --bind /sys /mnt/sys
sudo mount --bind /proc /mnt/proc

Also, if you have a separate Ubuntu boot partition (pretty uncommon these days, but it may be the case):

sudo mount /dev/sdaX /mnt/boot

How can you tell if you have a boot partition?

Once you have your Ubuntu partition mounted, open /mnt/etc/fstab. If you see an entry for /boot, note which device it is pointing to (/dev/sda4 maybe?). This is the one you have to mount.

Once these are mounted, do chroot to start using the mounted directory as the root partition:

sudo chroot /mnt

You'll get a #/ prompt. First thing to do is confirm that you're using the correct /boot directory. Go to /boot/grub and look at the files there. There should be a bunch of .mod files and a grub.cfg file. If the directory is empty, don't continue, because it means this is NOT your actual boot directory. Look above to see how to determine if you need to mount an additional boot directory.

Once you've confirmed that /boot/ contains the correct files, meaning that it is the correct location, type:

sudo update-grub

This should rebuild your /boot/grub/grub.cfg file with the menu entries.

Then exit the chroot:

exit

At this point you may want to check that things were correctly updated. For this, cd /mnt/boot/grub and check that grub's files are there, there should be a bunch of .mod files and grub.cfg, the latter should have entries for your Ubuntu kernels. If you only see grub.cfg and no .mod files, it means that this is NOT the correct boot directory, look above for how to mount a separate boot partition.

Unmount the filesystems:

sudo umount /mnt/dev
sudo umount /mnt/sys
sudo umount /mnt/proc
sudo umount /mnt/boot #Only if you mounted it earlier
sudo umount /mnt/

And then reboot, hopefully your Grub menu will be restored.

roadmr
  • 34,222
  • 9
  • 81
  • 93
  • When I try to run the three mount commands, I get "mount point /mnt/dev does not exist" or the corresponding one for each. – Kelley Jun 01 '12 at 21:15
  • is your Ubuntu install on /dev/sda5? If so, after sudo mount /dev/sda5 /mnt you should be able to cd /mnt and see your Ubuntu installation's tree there. It should have dev, sys and proc. If not, maybe you're mounting the wrong partition. Can you check this? Thanks! – roadmr Jun 01 '12 at 21:17
  • I restarted the LiveCD and this time it appears to be working. I must have typed something wrong the first time, although I was careful. Anyway, I'm rebooting now ... and no, nothing has changed. Still no choice of Ubuntu in the Grub menu. – Kelley Jun 01 '12 at 21:26
  • So to answer your question, after running "sudo mount /dev/sda5 /mnt", and then "cd /mnt", I do see dev, sys, and proc. – Kelley Jun 01 '12 at 21:30
  • Updated the answer with some things to check to ensure changes were correctly made. If you see your Ubuntu entries in grub.cfg, but still nothing in the menu, I'm not sure what may be happening :/ – roadmr Jun 01 '12 at 21:33
  • After mounting everything, and running chroot and getting the #/ prompt, I try running "sudo update-grub". I get "sudo: unable to resolve host unbuntu" but then it goes ahead and generates grub.cfg. It lists the various kernels it finds ("Found linux image: /boot/vmlinuz-3.2.0-24-generic" for one), but it doesn't really seem to update it, as when I reboot I still have no Ubuntu in the Grub menu. – Kelley Jun 01 '12 at 21:36
  • look at the verification step, before unmounting /mnt, go into /mnt/boot/grub and ensure that grub.cfg was created there, it contains entries for Ubuntu kernels and that other grub files are present. Let me know. – roadmr Jun 01 '12 at 21:41
  • Sorry to drag you into chat, but a message appeared saying the discussion was too long. I'll try not to keep you. The only Grub files there are grub.cfg and grubenv. I don't see any entries in grub.cfg for any kernel entries. (I also posted this in chat.) – Kelley Jun 01 '12 at 21:55
  • 2
    You Sir, are a life-saver :) Thanks for this. Worked like a charm. – ereOn Nov 03 '12 at 08:47
  • This saved my life! By happen chance sda5 was my ubuntu partition. Could you please explain, or point me to, how one determines which sda# would be the correct partition? – Ifrit Jun 28 '13 at 17:54
  • @Jay: The way to know for sure is to mount each partition of type Linux and search for files you know should be in / (such as bin/ or sbin). It really depends on how you partitioned your HD when installing. If you chose basic partitioning, a sure-fire way is to run (from the Live CD, right before mounting sda5) this command: sudo fdisk -l /dev/sda. Partitions are shown on the very left. The root partition is usually the biggest (look at Blocks column) with type Linux (look at the System column at the very right). – roadmr Jul 02 '13 at 19:26
  • this worked perfectly for me. mounting /dev was the missing thing! – alle_meije Sep 17 '14 at 13:45
  • You need sudo mount --bind /run /mnt/run as well to fix the error /run/lock/lvm: mkdir failed: Too many levels of symbolic links – Bryan Larsen Aug 08 '15 at 15:49
  • Does anyone know if this same scenario would work when plugging the main hard drive of the computer I want to update grub (say computer A) as an external drive onto another computer (computer B), and executing all these commands from B before putting hard drive back in A? I cannot use neither my keyboard nor my screen on computer A and I'd like to update grub to boot automatically into ubuntu. – Vince Mar 21 '16 at 22:24
  • Note that for people using UEFI, you need to mount the uefi partition into /boot/efi, like sudo mount /dev/sda1 /boot/efi. And eventually if you adden't any entry installed before (or anyway you can always do that without any problem), you can run grub-install /dev/sda before the grub-update. – tobiasBora May 23 '18 at 16:58
  • Some more details about this technique, esp. about setups with software RAID, LVM and bcache, are here in the Ubuntu community wiki: Grub2 → Installing → Reinstalling Grub2 → via chroot. – tanius Apr 30 '19 at 13:01
  • @Kelley, this should be the accepted answer... – lucasart Aug 22 '20 at 08:02
  • no need to unmount. make sure you open Multiple root terminals and just reboot from another terminal thats not chroot – allanlaal Aug 03 '21 at 06:11
  • Maybe this is lost on the many comments already present, but I had to use grub-install /dev/sdX and grub-install --recheck /dev/sdX before update-grub following https://howtoubuntu.org/how-to-repair-restore-reinstall-grub-2-with-a-ubuntu-live-cd Thanks for this answer it set me on the right track to finish my move from HDD to SSD. Thanks! – llrs Sep 12 '21 at 10:34
  • I didn't see any .mod files but it still worked without mounting /boot separately. – Michael Herrmann Apr 29 '22 at 11:15
17

Boot from a Live CD.

Hit Alt+Ctrl+T to open terminal and run following commands:

sudo mount /dev/sda5 /mnt

Install the GRUB2 boot loader:

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

That’s /dev/sda — the hard disk itself, not the Ubuntu partition – /dev/sda5.

Unmount the Ubuntu partition and restart the computer like so:

sudo umount /dev/sda5 ; sudo reboot

If you have more than one OS installed, re-detect OSes like so:

sudo update-grub

That’s it!

Basharat Sialvi
  • 24,046
  • 8
  • 62
  • 82
  • 1
    flagged mine for deletion - no point in the same thing twice – 23 93 26 35 19 57 3 89 Jun 01 '12 at 20:52
  • Thanks, but that doesn't work. It reinstalls Grub, but when I reboot it still hasn't been updated, so my Ubuntu installation still doesn't show up in Grub. Somehow I need to update Grub before I reboot, like from the LiveCD. – Kelley Jun 01 '12 at 20:54
  • @Kelley Are you sure Ubuntu installation is on sda5? You can check it by running sudo fdisk -l. Ubuntu installation will have a * after /dev/sdxx. – Basharat Sialvi Jun 01 '12 at 20:59
  • Yes, it's on sda5. However, the * is after sda2, which is my Windows partition (and the last partition into which I've been able to boot, since it's the only choice on my Grub menu). – Kelley Jun 01 '12 at 21:07
  • 1
    Chroot. Please read other answers. – ish Jun 01 '12 at 21:09
  • @BasharatSial : Why would you install grub onto a Windows partition? – ish Jun 01 '12 at 21:20
  • And we got, which was expected: /usr/sbin/grub-probe:error:failed to get canonical path of /cow.. – Artfaith Mar 08 '23 at 10:40
3

In 2022, you probably have an EFI system that requires a couple more steps.

Boot from a Live CD and open the terminal.

Mount the partition (e.g. /dev/sda5) with your original OS to /mnt:

sudo mount /dev/sda5 /mnt

In case you are using a SSD disk, the device name starts with nvme, e.g. /dev/nvme0n1.

Find out which partition has the EFI system:

fdisk -l /dev/sda | grep EFI
# example output:
# /dev/sda1           2048     309247    307200   150M EFI System

Mount the EFI system partition (e.g. /dev/sda1) to /mnt/boot/efi:

sudo mount /dev/sda1 /mnt/boot/efi

Install GRUB according to @basharat-sialvi's instructions:

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

Finally, reboot the system.

mrts
  • 311
1

This solution is the exact same as roadmr's answer except much easier.

  1. Boot into AntiX Live CD.
  2. Menu>Applications>System Tools>Boot Repair
  3. Select 'Repair GRUB Configuration file' (this option does run update-grub)
  4. Select the drive/partition where /boot resides
  5. Reboot when its finished.

In my particular case, 'Reinstall GRUB Bootloader' fixed it for me.. I have a dual-boot windows/linux setup. I used Macrium 7 to create a clone of a drive to a file. Then restore that file to a new drive. After I restored it to the new drive, it would boot to a black screen with a blinking cursor in the upper left.

Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
1

My solution to that problem was:

  1. download supergrub2disk from http://www.supergrubdisk.org/category/download/supergrub2diskdownload/super-grub2-disk-stable/
  2. put it on the pendrive http://www.supergrubdisk.org/put-super-grub2-disk-into-an-usb-pendrive-as-an-iso-image-from-windows/
  3. boot computer from pendrive with supergrub2disk on it
  4. log in to your Ubuntu
  5. get Boot-Repair for Ubuntu from https://help.ubuntu.com/community/Boot-Repair

Maybe it's not the fastest solution but for me it was the easiest one.

jmarceli
  • 406
  • 4
  • 8
0

The combined instructions of roadmr and Basharat Sialvi can be found here: https://askubuntu.com/a/88432/293759

Instructions for chainloader and multiboot commands of Grub2 are at Community Help Wiki.

user293759
  • 11
  • 3
-2

Thanks for all the great help! However, in the end nothing seemed to work, and since I had a separate /home partition, I was able to reinstall Ubuntu without losing any data. I still have to reinstall programs and do some configuring, but everything seems good at this point.

Kelley
  • 33,062
  • 1
    Glad to know you solved it somehow, but chrooting should have worked, and that, i consider the best solution. – Mahesh Jun 02 '12 at 01:12
  • 1
    Yes. I don't know why it didn't work, but the answer was great (and I upvoted it). You guys provided lots of help, and perhaps the answers will help someone else get through this problem. – Kelley Jun 04 '12 at 15:03