How to Use GRUB Rescue to Fix Linux

Jordan Gloor / How-To Geek

If your GRUB shells shows a “grub>” prompt, you can use the ls, set, linux, and initrd commands to fix your Linux system. If instead you see a “grub rescue>” prompt, use set, insmod, linux, and initrd commands to boot your PC.

If your Linux computer can’t boot into its operating system, you’re likely to find yourself staring at a prompt in the GRUB shell. Follow these steps to repair your broken boot processes.

When Your Bootloader Doesn’t Boot

There are very few computer problems that make your heart sink as deeply and rapidly as a failure to boot. Without a working operating system, your computer isn’t really a computer.

It can be a very opaque problem, with no easy way to drill down into the root cause. Without access to Linux, you can’t use any of the usual diagnostic tools and processes you’d use to investigate the problem. And without determining what the issue is, how can you fix it?

On Linux, GRUB is the software that controls the boot process. If you have multiple operating systems installed, it let’s you pick the one you want to boot into.

GRUB is controlled by its configuration. It relies on other supporting files too, in order to perform its functions. If any of these are missing or become corrupted, GRUB won’t work. Changes to the hard drives and partitions of your computer need to be reflected in GRUB’s configuration. If this isn’t the case, GRUB’s instructions won’t make sense, and your boot process won’t work.

As a side note, what we all call GRUB is actually GRUB 2. This is an advanced version of GRUB that was released back in 2012. Amongst other changes, it changed the way the “grub.cfg” file was created, and let you boot from removable media.

If GRUB cannot load an operating system, you’ll be left staring at a prompt in the GRUB shell. This is a minimalist environment that will let you perform a handful of actions. Done correctly, however, these steps can resurrect your computer.

There are two different GRUB shells. Depending on the root cause of the problem, you might find yourself at a GRUB shell prompt:

grub>

or at a GRUB rescue shell prompt:

grub rescue>

The GRUB rescue shell is even more restricted than the GRUB shell, but it still packs enough punch to get your computer to boot. Once your computer has booted, you’re in a much better position to cure the root cause.

Using the GRUB Shell

The GRUB shell is a command line interface that lets you use a small subset of Linux commands. If you find yourself here instead of at your usual login screen, it means your boot process is messed up.

The GRUB shell prompt

The commands you have access to are not the same as the usual Linux versions. For example, the GRUB shell ls command isn’t the Bash ls command. It has very little in common with it. We can see this by using the --help option.

ls --help

Using the --help option with the GRUB shell version of ls

It lists five options only. That’s a far cry from the dozens of options supported by ls in Bash. Nevertheless, even with our somewhat muted palette of commands, we can still rectify the situation.

At the prompt, type ls and hit “Enter.”

ls

The output from the GRUB shell version of ls, showing drives and partitions

On this computer, it lists three items.

  • (hd0) is the first hard drive.
  • (hd0,gpt1) is the first partition on the first hard drive.
  • (hd0,gpt2) is the second partition on the first hard drive.

The “gpt” label stands for GUID partition table. We need to include the brackets “()” when we reference a partition, but we can omit the label.

To look at the partitions in turn, we’ll use ls.

ls (hd0,1)
ls (hd0,2)

Using the GRUB shell version of ls to identify the Linux partition

We’ve found that the second partition has a Linux file system on it. We’ll investigate a little further by looking at the root directory of that partition.

ls (hd0,2)/

Using the GRUB shell version of ls to examine a particular partition

Clearly, this is the Linux partition, with the boot directory on it. This is where the GRUB files, Linux boot images, and other boot files are stored.

On this computer, we’ll need to reference partition (hd0,2) when we issue our commands. On your computer you’d use whatever partition you find your Linux installation on.

We can use the cat command to look text files by appending the directory path and filename to the partition name, like this:

cat (hd0,2)/etc/issue

Looking at the version of Linux using the cat command in the GRUB shell

On multi-boot installations, this is a useful way to verify you’re on the right partition. It’s also great for looking inside configuration files.

Here, we’re using the ls command with the -l (long) and -h (human readable sizes) options to list the files in the “/boot” directory.

ls -lh (hd0,2)/boot

Listing the files in the /boot directory using the GURB version of ls

To get this computer to boot, we need to tell GRUB that our newly discovered Linux partition holds the boot files.

set root=(hd0,2)

We need to specify which Linux kernel to boot from. We do this with the linux command. Note that we also have to provide the root directory using the usual Linux hard drive and partition naming scheme.

linux /boot/vmlinuz-6.2.0-20-generic root=/dev/sda2

On our example computer, we’re using (hd0) which is the first hard drive. This is called sda in the usual Linux nomenclature. We found that the second partition is the Linux partition, so we need to specify sda2.

Your machine might be different. For example, if your Linux partition is reported as (hd2,1) that means partition one on the third hard drive, or sdc1.

We also need to specify which RAM-based file system image to use when the computer boots. This must use the same numerical component as the Linux kernel we’re using.

initrd /boot/initrd.img-6.2.0-20-generic

Setting the boot parameters inthe GRUB shell

Now, to boot our system, type “boot” and hit “Enter.”

This should boot your computer up into Linux. That’s got us running again, but we still need to fix GRUB so we don’t need to do this every time we start our computer.

We’ll run update-grub. This will scan our system, identify operating systems, and create a new GRUB config. We need to run this as the root user.

sudo update-grub

Running update-grub to rebuild the grub.cfg file

Rarely, you might need to reinstall the GRUB bootloader too, using grub-install.

sudo grub-install /dev/sda

Note you don’t need to specify which partition to install on, just which drive. In normal installations, it’s the same drive your Linux is on.

The proof of the pudding comes with the next reboot. Your computer should reboot normally.

RELATED: How to Configure the GRUB2 Boot Loader’s Settings

Using the GRUB Rescue Shell

Depending on the nature of the boot problem, you might not get dropped into the GRUB shell. You might be left in the GRUB rescue mode. If that’s the case, your prompt will look like this:

grub rescue>

The commands we need to issue in this shell are different. We need to use ls to find the Linux partition, just as if you were using the GRUB shell. Having identified the Linux partition, you can go ahead and enter these commands.

Remember to use the drive identifiers and kernel version numbers that are correct for your computer. The insmod command loads kernel modules.

set prefix=(hd0,2)/boot/grub
set root=(hd0,2)
insmod normal
insmod linux
linux /boot/vmlinuz-6.2.0-20-generic root=/dev/sda2
initrd /boot/initrd.img-6.2.0-20-generic

Setting the boot paramters in the GRUB rescue shell

You can then type “boot” and hit “Enter” to reboot your computer. Once it has rebooted and you have logged in, update GRUB with the update-grub command.

Other Possible Remedies

If the issue turns out to be missing boot files, such as the “vmlinuz” and “initrd.img” files, you can boot your computer from a Live DVD or bootable Linux USB flash drive, and copy the missing files into place.

Note that the “vmlinuz” and “initrd.img” files must have the same version numbers in them, because they work as a matched pair.

RELATED: How to Create a Bootable Linux Live USB on Your Mac




Source link