Saturday, December 28, 2024
Google search engine
HomeSecurity & TestingModify grub.cfg configurations on Linux using grubby

Modify grub.cfg configurations on Linux using grubby

.tdi_3.td-a-rec{text-align:center}.tdi_3 .td-element-style{z-index:-1}.tdi_3.td-a-rec-img{text-align:left}.tdi_3.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_3.td-a-rec-img{text-align:center}}

After installing multiple Kernel versions on your system, you may find yourself in a situation where you need to modify your GRUB configurations. Grubby is a command-line tool used to display and update information for the lilo, elilo, zipl, yaboot, and grub bootloaders. It is normally used to make changes to the grub.cfg file. Grubby can be used to:

  • Modify kernel boot entries
  • Check and change default kernel
  • List active installed kernel
  • Adding/removing arguments from a GRUB2 menu entry

Boot entries are defined as a collection of options stored within a configuration file normally tied to a particular kernel version.

This guide aims to demonstrate how to modify grub.cfg configurations on Linux using grubby.

.tdi_2.td-a-rec{text-align:center}.tdi_2 .td-element-style{z-index:-1}.tdi_2.td-a-rec-img{text-align:left}.tdi_2.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_2.td-a-rec-img{text-align:center}}

Getting Started

For this guide, it is recommended that you have as many boot entries as your system has installed Kernels.

Check the boot entry file that contains information about your Kernel version, the initial ramdisk image, and the kernel opts environment variable e.t.c normally stored at /boot/loader/entries/ as below.

$ sudo ls -1 /boot/loader/entries/
723b266ce27e45698c2c0e30032ea83f-0-rescue.conf
723b266ce27e45698c2c0e30032ea83f-4.18.0-305.17.1.el8_4.x86_64.conf 

Se

# cat /boot/loader/entries/723b266ce27e45698c2c0e30032ea83f-4.18.0-305.17.1.el8_4.x86_64.conf 
title Red Hat Enterprise Linux (4.18.0-305.17.1.el8_4.x86_64) 8.4 (Ootpa)
version 4.18.0-305.17.1.el8_4.x86_64
linux /vmlinuz-4.18.0-305.17.1.el8_4.x86_64
initrd /initramfs-4.18.0-305.17.1.el8_4.x86_64.img $tuned_initrd
options $kernelopts $tuned_params
id rhel-20210830114656-4.18.0-305.17.1.el8_4.x86_64
grub_users $grub_users
grub_arg --unrestricted
grub_class kernel

As seen from the output, there are several details stored in the boot entry configuration file.

Modify grub.cfg configurations on Linux using grubby.

Now we will see several options for how you can modify your Grub using this grubby tool.

1. Add a new kernel entry

First, get the list of all installed Kernels.

$ sudo grubby --info=ALL | grep ^kernel
kernel="/boot/vmlinuz-4.18.0-348.2.1.el8_5.x86_64"
kernel="/boot/vmlinuz-4.18.0-348.2.1.el8_5.x86_64"
kernel="/boot/vmlinuz-4.18.0-305.19.1.el8_4.x86_64"
kernel="/boot/vmlinuz-4.18.0-305.17.1.el8_4.x86_64"
kernel="/boot/vmlinuz-0-rescue-723b266ce27e45698c2c0e30032ea83f"

In case you want to add a custom entry to your existing Boot options, use the syntax below:

sudo grubby --add-kernel=new_kernel \
  --title="entry_title" \
  --initrd="new_initrd" \
  --copy-default

The –copy-default flag here is used to copy all the default kernel arguments to this new kernel entry. You can add your own custom Kernel arguments as below:

sudo grubby --add-kernel=new_kernel \
  --title="entry_title" \
  --initrd="new_initrd" \
  --args=kernel_args

For example, I will create a new boot entry Linux_Test_4.18.0-348.2.1.el8_5.x86_64 as below:

$ sudo grubby --grub2 \
  --add-kernel=/boot/vmlinuz-4.18.0-348.2.1.el8_5.x86_64 \
  --title="Linux_Test_Kernel" \
  --initrd=/boot/initramfs-4.18.0-348.2.1.el8_5.x86_64.img \
  --copy-default

The kernel entry will be added as below.

-rw-r--r--. 1 root root 395 Oct 14 05:41 /boot/loader/entries/723b266ce27e45698c2c0e30032ea83f-0-rescue.conf
-rw-r--r--. 1 root root 371 Oct 25 05:26 /boot/loader/entries/723b266ce27e45698c2c0e30032ea83f-4.18.0-305.17.1.el8_4.x86_64.conf
-rw-r--r--. 1 root root 371 Oct 25 05:29 /boot/loader/entries/723b266ce27e45698c2c0e30032ea83f-4.18.0-305.19.1.el8_4.x86_64.conf
-rw-r--r--. 1 root root 303 Dec 22 03:23 /boot/loader/entries/723b266ce27e45698c2c0e30032ea83f-4.18.0-348.2.1.el8_5.x86_64.0~custom.conf
-rw-r--r--. 1 root root 366 Nov 22 06:15 /boot/loader/entries/723b266ce27e45698c2c0e30032ea83f-4.18.0-348.2.1.el8_5.x86_64.conf

2. Change the Default Boot Entry

Begin by listing the default Kernel as below.

$ sudo grubby --default-kernel
/boot/vmlinuz-4.18.0-348.2.1.el8_5.x86_64

You can also find out the index number of the default kernel.

$ sudo grubby --default-index
1

Find out the kernel title:

$ sudo grubby --default-title
Red Hat Enterprise Linux (4.18.0-305.19.1.el8_4.x86_64) 8.4 (Ootpa)

There are two methods you can use to make the persistent change to the default kernel.

Method 1: Using the kernel path:

sudo grubby --set-default /boot/vmlinuz-4.18.0-305.19.1.el8_4.x86_64

Sample Output:

The default is /boot/loader/entries/723b266ce27e45698c2c0e30032ea83f-4.18.0-305.19.1.el8_4.x86_64.conf with index 2 and kernel /boot/vmlinuz-4.18.0-305.19.1.el8_4.x86_64

In the above code, I am switching from the default vmlinuz-4.18.0-348.2.1.el8_5.x86_64 to vmlinuz-4.18.0-305.19.1.el8_4.x86_64 as my new default kernel.

Method 2: Using the kernel index:

Find all the installed kernels’ indexes

$ sudo grubby --info=ALL | grep -E "^kernel|^index"
index=0
kernel="/boot/vmlinuz-4.18.0-348.2.1.el8_5.x86_64"
index=1
kernel="/boot/vmlinuz-4.18.0-348.2.1.el8_5.x86_64"
index=2
kernel="/boot/vmlinuz-4.18.0-305.19.1.el8_4.x86_64"
index=3
kernel="/boot/vmlinuz-4.18.0-305.17.1.el8_4.x86_64"
index=4
kernel="/boot/vmlinuz-0-rescue-723b266ce27e45698c2c0e30032ea83f"

Now proceed and make your desired change:

$ sudo grubby --set-default-index=3
The default is /boot/loader/entries/723b266ce27e45698c2c0e30032ea83f-4.18.0-305.17.1.el8_4.x86_64.conf with index 3 and kernel /boot/vmlinuz-4.18.0-305.17.1.el8_4.x86_64

Apply the changes by rebooting your system.

sudo reboot -i

The default kernel will be automatically selected as below.

How To Modify grub.cfg configurations on Linux using grubby

The information on all menu entries can be listed using the command:

$ sudo grubby --info=ALL
index=0
kernel="/boot/vmlinuz-4.18.0-348.2.1.el8_5.x86_64"
args="ro crashkernel=auto resume=/dev/mapper/cl-swap rd.lvm.lv=cl/root rd.lvm.lv=cl/swap $tuned_params"
root="/dev/mapper/cl-root"
initrd="/boot/initramfs-4.18.0-348.2.1.el8_5.x86_64.img"
title="Linux_Test_Kernel"
id="723b266ce27e45698c2c0e30032ea83f-4.18.0-348.2.1.el8_5.x86_64.0~custom"
index=1
kernel="/boot/vmlinuz-4.18.0-348.2.1.el8_5.x86_64"
args="ro crashkernel=auto resume=/dev/mapper/cl-swap rd.lvm.lv=cl/root rd.lvm.lv=cl/swap $tuned_params"
root="/dev/mapper/cl-root"
initrd="/boot/initramfs-4.18.0-348.2.1.el8_5.x86_64.img $tuned_initrd"
title="Red Hat Enterprise Linux (4.18.0-348.2.1.el8_5.x86_64) 8.5 (Ootpa)"
id="723b266ce27e45698c2c0e30032ea83f-4.18.0-348.2.1.el8_5.x86_64"
index=2
kernel="/boot/vmlinuz-4.18.0-305.19.1.el8_4.x86_64"
args="ro crashkernel=auto resume=/dev/mapper/cl-swap rd.lvm.lv=cl/root rd.lvm.lv=cl/swap $tuned_params"
root="/dev/mapper/cl-root"
initrd="/boot/initramfs-4.18.0-305.19.1.el8_4.x86_64.img $tuned_initrd"
title="Red Hat Enterprise Linux (4.18.0-305.19.1.el8_4.x86_64) 8.4 (Ootpa)"
id="723b266ce27e45698c2c0e30032ea83f-4.18.0-305.19.1.el8_4.x86_64"
..........

You can as well view the GRUB menu for a specific kernel.

$ sudo grubby --info /boot/vmlinuz-4.18.0-305.19.1.el8_4.x86_64
index=2
kernel="/boot/vmlinuz-4.18.0-305.19.1.el8_4.x86_64"
args="ro crashkernel=auto resume=/dev/mapper/cl-swap rd.lvm.lv=cl/root rd.lvm.lv=cl/swap $tuned_params"
root="/dev/mapper/cl-root"
initrd="/boot/initramfs-4.18.0-305.19.1.el8_4.x86_64.img $tuned_initrd"
title="Red Hat Enterprise Linux (4.18.0-305.19.1.el8_4.x86_64) 8.4 (Ootpa)"
id="723b266ce27e45698c2c0e30032ea83f-4.18.0-305.19.1.el8_4.x86_64"

3. Remove kernel entry

In case you want to remove a certain Kernel entry use the syntax.

$ sudo grubby --remove-kernel=old_kernel

Alternatively:

sudo grubby --remove-kernel=menu_index

NOTE: Removing a kernel entry of an incorrect kernel may make your system fail to boot and therefore required, to boot into single-user mode to fix this error.

4. Add new kernel arguments

You can add arguments to your kernel using the syntax:

sudo grubby --update-kernel=current_kernel --args="kernel_args"

Practically, let’s say I want to disable IPV6 on my default kernel;

sudo grubby --update-kernel=/boot/vmlinuz-$(uname -r) --args="ipv6.disable=1"

You can verify the made changes as below:

# grep ipv6 /boot/loader/entries/d88fa2c7ff574ae782ec8c4288de4e85-$(uname -r).conf
options root=/dev/mapper/rhel-root ro resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet biosdevname=0 net.ifnames=0 enforcing=0 $tuned_params ipv6.disable=1

A new argument can as well be added to all available kernels:

sudo grubby --update-kernel=ALL --args="kernel_args"

5. Remove kernel arguments

The kernel arguments can as well be removed from the kernel boot entries. Normally the syntax used is as below:

grubby --update-kernel=current_kernel --remove-args="kernel_args"

Now to remove the IPV6 disable argument previously added to the default kernel above use:

sudo grubby --update-kernel=/boot/vmlinuz-$(uname -r) --remove-args="ipv6.disable=1"

You can remove arguments for all Kernel entries as below:

sudo grubby --update-kernel=ALL --remove-args="kernel_args"

6. Remove and Add kernel arguments

Kernel arguments can be removed and added using Grubby with a single command with the below syntax:

sudo grubby --remove-args="kernel-args" --args="kernel_args"

For example, I will remove the IPv6 disable and add a new argument console=ttys0 as below.

$ sudo grubby --update-kernel=/boot/vmlinuz-$(uname -r) \
  --remove-args="ipv6.disable=1" \
  --args="console=ttsy0"

Verify the made changes:

$ grep console /boot/loader/entries/d88fa2c7ff574ae782ec8c4288de4e85-$(uname -r).conf
options root=/dev/mapper/rhel-root ro resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb biosdevname=0 net.ifnames=0 enforcing=0 $tuned_params console=ttsy0

The end!

Closing Thoughts:

That was enough learning! We have systematically walked through how to modify grub.cfg configurations on Linux using grubby. We can all agree that this tool is super cool. I hope you enjoyed it.

See more:

.tdi_4.td-a-rec{text-align:center}.tdi_4 .td-element-style{z-index:-1}.tdi_4.td-a-rec-img{text-align:left}.tdi_4.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_4.td-a-rec-img{text-align:center}}

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments