Situatie
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.
Solutie
When Your Bootloader Doesn’t Boot
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 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
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
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)
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)/
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
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
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
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
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.
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
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.
Leave A Comment?