# GRUB

Notes on grub, because it's grand :)

To apply *any* of these changes, make sure to run the following command after saving the configurations.

```bash
sudo update-grub
```

When you open the `/etc/defaults/grub` file for editing in the next section, the header comment will remind you of this. Don't overlook it!

```bash
sudo head /etc/default/grub

# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
#   info -f grub -n 'Simple configuration'
```

You can run the `info -f grub -n 'Simple configuration'` command to view the GRUB manual, starting at the `Simple configuration` section, or you can [view the same manual from your web browser](https://www.gnu.org/software/grub/manual/grub/grub.html#Simple-configuration) - it's up to you.

#### /etc/defaults/grub

Create a backup of your configuration before making any edits, so you can restore the original configuration if things get out of hand.

```bash
sudo cp /etc/defaults/grub /etc/defaults/grub.bak
```

The settings below will save last selected boot option as the default for next boot. To override it, simply reboot and select a different option.

``` 
GRUB_DEFAULT=saved
GRUB_SAVEDEFAULT=true
GRUB_TIMEOUT_STYLE=menu
GRUB_TIMEOUT=5
```

#### GRUB Console

You may one day need to boot from a GRUB console, and to do this you'll need to select your kernel and initrd (Initialize Ram Disk) image manually. This might sound intimidating, but it really is not that bad and if you know where your MBR is located you should be able to accomplish this in just a few commands.

The file we want to use for our linux kernel on this machine is `vmlinuz-5.15.0-kali3-amd64` - yours might have a slightly different name, but it should begin with `vmlinuz`

The file we want to use for our `initrd` image is named `initrd.img-5.15.0-kali3-amd64`, and again yours should be similar and begin with `initrd`.

Both of these files should exist in the `/boot/` directory of the MBR

On a Kali VM GRUB looks like this during boot. Simply interrupt the boot by pressing the up or down arrow on your keyboard to prevent the timeout from triggering and the system will not automatically proceed, it will wait for your input.

[![](https://knoats.com/uploads/images/gallery/2022-03/scaled-1680-/image-1648242373058.png)](https://knoats.com/uploads/images/gallery/2022-03/image-1648242373058.png)

Now we can press `c` to enter the GRUB console, and you can practice booting from GRUB manually. You will see the console below.

[![](https://knoats.com/uploads/images/gallery/2022-03/scaled-1680-/image-1648242526966.png)](https://knoats.com/uploads/images/gallery/2022-03/image-1648242526966.png)

In the next image, we ran the `ls` command, and seen the available filesystems output to the console. Since we are in a GRUB console, we have not yet set our root filesystem. If you have a lot of storage devices attached to your machine, this list might be much longer. In any case, the MBR should exist on the first sector of the drive that has your OS installed on it. First find the drive you want to boot from, then we can check the `/boot/` directory within the first partiton of this drive for the kernel and image file we need to boot the system.

For this output, it's clearly the `(hd0)` device since it is the only device available. Note that `(hd0)` is the device itself, and not the first sector. To view the contents in the first sector of the `(hd0)` device before we set it to be our root filesystem, we can run `ls (hd0,1)/`. You can leave out the `mdos` section, or you can type it out - both will have the same result. On some systems `mdos` will be replaced with `gpt`, but the steps will be the same.

[![](https://knoats.com/uploads/images/gallery/2022-03/scaled-1680-/image-1648242680604.png)](https://knoats.com/uploads/images/gallery/2022-03/image-1648242680604.png)

Notice when we ran `ls (hd0,1)/boot/` we can see the two files we need! Both `vmlinuz-5.15.0-kali3-amd64` and `initrd.img-5.15.0-kali3-amd64` exist in the `(hd0,1)/boot/` directory.

You may have noticed that the `ls /` command output the files `vmlinuz` and `initrd.img` - that's because some systems create symlinks to the last used kernel and initrd images in the root directory of the filesystem. You can use these if you're sure they're what you want, or you can just use the full path to the files under the `/boot/` directory.

We continue by setting the root filesystem to the `(hd0,1)` device with the following commands

```bash
grub> set root=(hd0,1)
```

Now you'll notice that running `ls /boot/` points us to the directory with the files we need, and at this point we are almost ready to boot the system.

[![](https://knoats.com/uploads/images/gallery/2022-03/scaled-1680-/image-1648243631796.png)](https://knoats.com/uploads/images/gallery/2022-03/image-1648243631796.png)

Next, we run the following commands to set the linux kernel and `initrd` image, then we boot the system by running the `boot` command. The system will boot normally and you'll be asked to login.

```bash
grub> linux /boot/vmlinuz-5.15.0-kali3-amd64 root=/dev/sda1
grub> initrd /boot/initrd.img-5.15.0-kali3-amd64
grub> boot
```

[![](https://knoats.com/uploads/images/gallery/2022-03/scaled-1680-/image-1648243769693.png)](https://knoats.com/uploads/images/gallery/2022-03/image-1648243769693.png)

[![](https://knoats.com/uploads/images/gallery/2022-03/scaled-1680-/image-1648243783217.png)](https://knoats.com/uploads/images/gallery/2022-03/image-1648243783217.png)

[Linux - Rescue GRUB](https://www.linux.com/training-tutorials/how-rescue-non-booting-grub-2-linux/)