Generating a New grub.cfg on Oracle/Red Hat Linux 7

In this tutorial, we will generate a new grub.cfg.


In this example, after updating the Oracle Linux 7 to the latest OL7 updates, the menu got corrupted, and the OS was booting up to the older kernel on index 0 even though the default was set to the latest kernel on index 1. OL7/RH7 uses GRUB 2 with the grub.cfg configuration file. We will regenerate a new grub.cfg to correct this issue. All the commands should be executed as root user.


Verify that the default boot option is set to the newest kernel.


[root@lnxnode1 ~]# grubby --info DEFAULT
index=1
kernel=/boot/vmlinuz-5.4.17-2136.329.3.1.el7uek.x86_64
args="ro rootflags=subvol=root  crashkernel=auto console=tty0 console=ttyS0,115200n8 LANG=en_US.UTF-8"
root=UUID=32bad27b-a9de-4007-92f2-059ad1d50728
initrd=/boot/initramfs-5.4.17-2136.329.3.1.el7uek.x86_64.img
title=Oracle Linux Server 7.9, with Unbreakable Enterprise Kernel 5.4.17-2136.329.3.1.el7uek.x86_64

We confirmed that it actually booted up to index 0 which is incorrect.


[root@lnxnode1 ~]# uname -r
3.10.0-1160.114.2.0.1.el7.x86_64

[root@lnxnode1 ~]# grubby --info 0
index=0
kernel=/boot/vmlinuz-3.10.0-1160.114.2.0.1.el7.x86_64
args="ro rootflags=subvol=root  crashkernel=auto console=tty0 console=ttyS0,115200n8 LANG=en_US.UTF-8"
root=UUID=32bad27b-a9de-4007-92f2-059ad1d50728
initrd=/boot/initramfs-3.10.0-1160.114.2.0.1.el7.x86_64.img
title=Oracle Linux Server 7.9, with Linux 3.10.0-1160.114.2.0.1.el7.x86_64

Step 1 – We need to determine whether our machine uses /boot/grub2/grub.cfg (traditional BIOS-based) or /boot/efi/EFI/redhat/grub.cfg (UEFI). To do so, we check if /sys/firmware/efi exists. If it does not, then it’s using traditional BIOS. Otherwise, it’s using UEFI.


[root@lnxnode1 ~]# ls -l /sys/firmware/efi
ls: cannot access /sys/firmware/efi: No such file or directory

Step 2 – Since we are using traditional BIOS, we execute the following command to generate a new /boot/grub2/grub.cfg.

Note: It will remove any entries that were added manually so you might want to create a copy of the original grub.cfg before running the following command.


[root@lnxnode1 grub2]# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.4.17-2136.329.3.1.el7uek.x86_64
Found initrd image: /boot/initramfs-5.4.17-2136.329.3.1.el7uek.x86_64.img
Found linux image: /boot/vmlinuz-5.4.17-2136.328.3.el7uek.x86_64
Found initrd image: /boot/initramfs-5.4.17-2136.328.3.el7uek.x86_64.img
Found linux image: /boot/vmlinuz-5.4.17-2136.327.2.el7uek.x86_64
Found initrd image: /boot/initramfs-5.4.17-2136.327.2.el7uek.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-1160.114.2.0.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-1160.114.2.0.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-1160.108.1.0.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-1160.108.1.0.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-1160.105.1.0.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-1160.105.1.0.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-a4c31edd45a64264b41cec1f29beb3dd
Found initrd image: /boot/initramfs-0-rescue-a4c31edd45a64264b41cec1f29beb3dd.img
Done

Reboot. Then verify it’s booted to the correct kernel.


[root@lnxnode1 ~]# uname -r
5.4.17-2136.329.3.1.el7uek.x86_64