https://www.golinuxcloud.com/grubby-command-examples/
1. grubby command to add new kernel entry
Assuming you have built your own kernel which is installed on the server and you wish to add a custom entry for this new kernel. So you can use below syntax:
grubby --add-kernel=new_kernel --title="entry_title" --initrd="new_initrd" --copy-default
Here I have used --copy-default to copy all the kernel arguments from our default kernel to this new kernel entry. To add your own custom kernel arguments you can use:
grubby --add-kernel=new_kernel --title="entry_title" --initrd="new_initrd" --args=kernel_args
To get the list of available boot entries
# ls -l /boot/loader/entries/*
-rw-r--r--. 1 root root 408 Nov 13 2019 /boot/loader/entries/d88fa2c7ff574ae782ec8c4288de4e85-0-rescue.conf
-rw-r--r-- 1 root root 366 Aug 15 16:03 /boot/loader/entries/d88fa2c7ff574ae782ec8c4288de4e85-4.18.0-193.1.2.el8_2.x86_64.conf
-rw-r--r-- 1 root root 371 Aug 15 16:07 /boot/loader/entries/d88fa2c7ff574ae782ec8c4288de4e85-4.18.0-193.14.3.el8_2.x86_64.conf
-rw-r--r-- 1 root root 336 Aug 15 15:12 /boot/loader/entries/d88fa2c7ff574ae782ec8c4288de4e85-4.18.0-193.el8.x86_64.conf
In this example I will create a new boot entry for 4.18.0-193.el8.x86_64 kernel
# grubby --grub2 --add-kernel=/boot/vmlinuz-4.18.0-193.el8.x86_64 --title="Red Hat Enterprise 8 Test" --initrd=/boot/initramfs-4.18.0-193.el8.x86_64.img --copy-default
Verify the newly added boot entry:
# ls -l /boot/loader/entries/*
-rw-r--r--. 1 root root 408 Nov 13 2019 /boot/loader/entries/d88fa2c7ff574ae782ec8c4288de4e85-0-rescue.conf
-rw-r--r-- 1 root root 366 Aug 15 16:03 /boot/loader/entries/d88fa2c7ff574ae782ec8c4288de4e85-4.18.0-193.1.2.el8_2.x86_64.conf
-rw-r--r-- 1 root root 371 Aug 15 16:07 /boot/loader/entries/d88fa2c7ff574ae782ec8c4288de4e85-4.18.0-193.14.3.el8_2.x86_64.conf
-rw-r--r-- 1 root root 287 Aug 16 15:17 /boot/loader/entries/d88fa2c7ff574ae782ec8c4288de4e85-4.18.0-193.el8.x86_64.0~custom.conf
-rw-r--r-- 1 root root 287 Aug 16 15:29 /boot/loader/entries/d88fa2c7ff574ae782ec8c4288de4e85-4.18.0-193.el8.x86_64.conf
Now you can change the default kernel and reboot your server to verify the changes.
2. Remove kernel entry
IMPORTANT NOTE:
Use this command with caution as it will remove the boot entry of the provided kernel. If you remove kernel entry of incorrect kernel, your system may fail to boot and then you will have to boot into single user mode to fix your broken server
# grubby --remove-kernel=old_kernel
OR you can also use
grubby --remove-kernel=menu_index
3. Add new kernel arguments
To add new arguments in GRUB2 use:
grubby --update-kernel=current_kernel --args="kernel_args"
For example, I will disable IPv6 in this command:
# grubby --update-kernel=/boot/vmlinuz-$(uname -r) --args="ipv6.disable=1"
I have used uname -r to get the release information of my default kernel. It is easier this way rather than providing the kernel version. Although if you plan to add new arguments to some kernel other than default then you must provide the kernel version manually.
Now verify your changes.
# 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
To add new kernel argument to all the available kernel
grubby --update-kernel=ALL --args="kernel_args"
4. Remove kernel arguments
Similar to add we can also remove additional kernel arguments from kernel boot entries using:
grubby --update-kernel=current_kernel --remove-args="kernel_args"
In the last example I had disabled ipv6 so now I will remove this kernel argument
# grubby --update-kernel=/boot/vmlinuz-$(uname -r) --remove-args="ipv6.disable=1"
Verify the boot entry of the default kernel to make sure the entry is removed
# grep ipv6 /boot/loader/entries/d88fa2c7ff574ae782ec8c4288de4e85-$(uname -r).conf
Similarly to remove kernel entry from all the kernels
grubby --update-kernel=ALL --args="kernel_args"
5. Remove and Add kernel arguments
We can combine both remove and add kernel arguments with grubby command.
grubby --remove-args="kernel-args" --args="kernel_args"
In this example I am removing "quiet" and adding "console=ttys0" argument in the same grubby command
# grubby --update-kernel=/boot/vmlinuz-$(uname -r) --remove-args="quiet" --args="console=ttsy0"
Verify your 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
6. List installed kernels
To list all the installed kernel you can use:
# grubby --info=ALL | grep ^kernel | sort -u
kernel="/boot/vmlinuz-4.18.0-193.14.3.el8_2.x86_64"
kernel="/boot/vmlinuz-4.18.0-193.1.2.el8_2.x86_64"
kernel="/boot/vmlinuz-4.18.0-193.el8.x86_64"
kernel="/boot/vmlinuz-0-rescue-d88fa2c7ff574ae782ec8c4288de4e85"
7. Get more information on kernel boot entries
To get more details on the installed kernel we can use grubby --info. This will give you information such as index number, boot entry id, arguments applied to the kernel etc
# grubby --info="/boot/vmlinuz-$(uname -r)"
index=1
kernel="/boot/vmlinuz-4.18.0-193.1.2.el8_2.x86_64"
args="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"
root="/dev/mapper/rhel-root"
initrd="/boot/initramfs-4.18.0-193.1.2.el8_2.x86_64.img $tuned_initrd"
title="Red Hat Enterprise Linux (4.18.0-193.1.2.el8_2.x86_64) 8.2 (Ootpa)"
id="d88fa2c7ff574ae782ec8c4288de4e85-4.18.0-193.1.2.el8_2.x86_64"
8. List path of the default kernel
A default kernel is the kernel version which will be activated post reboot. It is possible that if you have installed/removed any kernel then your active kernel version will be different compared to default kernel version
To get the default kernel version use:
# grubby --default-kernel
/boot/vmlinuz-4.18.0-193.el8.x86_64
9. List index of the default kernel
Now to get the index value of the default kernel use grubby --default-index. For example below output is from my server:
# grubby --default-index
2
10. List default kernel title
The kernel title is the output which you see in the GRUB2 menu while rebooting the Linux server. So to check the title of your default kernel use:
# grubby --default-title
Red Hat Enterprise Linux (4.18.0-193.1.2.el8_2.x86_64) 8.2 (Ootpa)
11. Set default kernel using kernel path
To change the default kernel i.e. the kernel to be activated post reboot using kernel path:
# grubby --set-default="/boot/vmlinuz-4.18.0-193.1.2.el8_2.x86_64"
The default is /boot/loader/entries/d88fa2c7ff574ae782ec8c4288de4e85-4.18.0-193.1.2.el8_2.x86_64.conf with index 1 and kernel /boot/vmlinuz-4.18.0-193.1.2.el8_2.x86_64
So now kernel-4.18.0-193.1.2.el8_2 is set as default boot to be activated post reboot.
12. Set default kernel using index
Instead of using the kernel vmlinuz entry we can also provide the index number to change the default kernel.
To get the index number of all the installed kernels:
# grubby --info=ALL | grep -E "^kernel|^index"
index=0
kernel="/boot/vmlinuz-4.18.0-193.14.3.el8_2.x86_64"
index=1
kernel="/boot/vmlinuz-4.18.0-193.1.2.el8_2.x86_64"
index=2
kernel="/boot/vmlinuz-4.18.0-193.el8.x86_64"
index=3
kernel="/boot/vmlinuz-4.18.0-193.el8.x86_64"
index=4
kernel="/boot/vmlinuz-0-rescue-d88fa2c7ff574ae782ec8c4288de4e85"
So now you have the mapping index number of each kernel which is installed on your server. Now we can use --set-default-index=