如何在RHEL 8 Linux中使用grub2-editenv和grubby更新GRUB2

时间:2020-02-23 14:40:38  来源:igfitidea点击:

" RHEL 8中的引导加载程序Grand Unified Boot Loader(GRUB2)与RHEL 7中的GRUB2不同。
"在本文中,我将共享不同的命令来更新GRUB2并在RHEL 8 Linux中设置内核命令行参数。

重要的提示:

对Red Hat Enterprise Linux 8 Beta的更新导致在发出grub2-mkconfig -o/boot/grub2/grub.cfg时不再包含/etc/default/grub更改。
似乎那里设置的至少某些选项现在被静默忽略。

在本文中,我将在RHEL 8 Linux上使用GRUB2(ipv6.disable)禁用IPv6,以演示在RHEL 8 Linux主机上更新GRUB2的步骤。

使用grub2-editenv更新GRUB2

实际上,推荐使用grub2-editenv实用程序来更改这些变量。
结果,可以使用以下内容。
添加一个额外的参数:

[root@rhel-8 ~]# grub2-editenv - list | grep kernelopts
kernelopts=root=/dev/mapper/rhel-root ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet

其中复制粘贴上面命令中的内容,并将我们希望添加的其他内核参数添加到GRUB2.

[root@rhel-8 ~]# grub2-editenv - set "kernelopts=root=/dev/mapper/rhel-root ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet ipv6.disable=1"

另外,我们也可以选择一种更短,更不易出错的方法,如下所示

[root@rhel-8 ~]# grub2-editenv - set "$(grub2-editenv - list | grep kernelopts) ipv6.disable=1"

其中$(grub2-editenv list | grep kernelopts)将自动选择现有的kernelopts,并将添加添加的内核命令行参数。

验证新添加的输出

[root@rhel-8 ~]# grub2-editenv - list | grep kernelopts
kernelopts=root=/dev/mapper/rhel-root ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet ipv6.disable=1

接下来,重新引导节点并验证配置,以确保更改是持久的

[root@rhel-8 ~]# grub2-editenv - list | grep kernelopts
kernelopts=root=/dev/mapper/rhel-root ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet ipv6.disable=1

如我们所见,在GRUB2命令行中禁用了IPv6.

[root@rhel-8 ~]# cat /proc/cmdline
BOOT_IMAGE=(hd0,msdos1)/vmlinuz-4.18.0-80.el8.x86_64 root=/dev/mapper/rhel-root ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet ipv6.disable=1

与从GRUB2的内核命令行中删除参数或者参数类似,请使用以下语法

# grub2-editenv - set "$(grub2-editenv - list | grep kernelopts | sed -e 's/<arg>//')"

例如,其中要删除ipv6.disable参数,我们将使用:

# grub2-editenv - set "$(grub2-editenv - list | grep kernelopts | sed -e 's/ipv6.disable=[01]$//')"

最后,重新引导系统以使更改生效。

使用grub2-mkconfig更新GRUB2

仍然可以使用较旧的方法来实现此行为,但是必须首先取消设置现有的" kernelopts"值:

[root@rhel-8 ~]# grep GRUB_CMDLINE_LINUX /etc/default/grub
GRUB_CMDLINE_LINUX="crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet"

其中我将在GRUB2配置文件/etc/default/grub中更新ipv6.disable = 1

[root@rhel-8 ~]# grep GRUB_CMDLINE_LINUX /etc/default/grub
GRUB_CMDLINE_LINUX="crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet ipv6.disable=1"

但是,如我们所见,现有的kernelopts反映了旧的GRUB2条目,因此,如果我们重新启动节点或者重建GRUB2,则新的更改将不会反映在节点上。

[root@rhel-8 ~]# grub2-editenv - list | grep kernelopts
kernelopts=root=/dev/mapper/rhel-root ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet

因此,我们需要取消设置kernelopts,这是此处的添加步骤:

[root@rhel-8 ~]# grub2-editenv - unset kernelopts

接下来重建grub配置

[root@rhel-8 ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
done

接下来重新验证kernelopts

[root@rhel-8 ~]# grub2-editenv - list | grep kernelopts
kernelopts=root=/dev/mapper/rhel-root ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet ipv6.disable=1

因此,我们的更改按预期可见。

使用grubby更新GRUB2

grubby是一个实用程序,用于处理特定于Bootloader的配置文件。

我们也可以使用grubby更改默认的引导项,以及从GRUB2菜单项添加/删除参数。

[root@rhel-8 ~]# grubby --info DEFAULT
index=0
kernel="/boot/vmlinuz-4.18.0-80.el8.x86_64"
args="ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet $tuned_params"
root="/dev/mapper/rhel-root"
initrd="/boot/initramfs-4.18.0-80.el8.x86_64.img $tuned_initrd"
title="Red Hat Enterprise Linux (4.18.0-80.el8.x86_64) 8.0 (Ootpa)"
id="f653c3662e81432aa484cd1639a04047-4.18.0-80.el8.x86_64"

将我们想要添加的参数添加到内核命令行菜单

[root@rhel-8 ~]# grubby --args ipv6.disable=1 --update-kernel DEFAULT

接下来验证GRUB2的配置

[root@rhel-8 ~]# grubby --info DEFAULT
index=0
kernel="/boot/vmlinuz-4.18.0-80.el8.x86_64"
args="ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet $tuned_params ipv6.disable=1"
root="/dev/mapper/rhel-root"
initrd="/boot/initramfs-4.18.0-80.el8.x86_64.img $tuned_initrd"
title="Red Hat Enterprise Linux (4.18.0-80.el8.x86_64) 8.0 (Ootpa)"
id="f653c3662e81432aa484cd1639a04047-4.18.0-80.el8.x86_64"

接下来重启节点以激活更改

[root@rhel-8 ~]# cat /proc/cmdline
BOOT_IMAGE=(hd0,msdos1)/vmlinuz-4.18.0-80.el8.x86_64 root=/dev/mapper/rhel-root ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet ipv6.disable=1