1. GRUB2引导器概述
GRUB2(Grand Unified Boot Loader version 2)是RHEL
10默认的引导加载程序,负责在系统启动时加载内核和初始化内存盘。GRUB2支持多系统引导、图形化菜单、命令行界面等高级功能。更多学习教程www.fgedu.net.cn
参考Red Hat Enterprise Linux 10官方文档中的System administration章节
# grub2-install –version
grub2-install (GRUB) 2.06
# 查看已安装的GRUB2包
# rpm -qa | grep grub
grub2-common-2.06-81.el10.noarch
grub2-pc-2.06-81.el10.x86_64
grub2-pc-modules-2.06-81.el10.noarch
grub2-tools-2.06-81.el10.x86_64
grub2-tools-minimal-2.06-81.el10.x86_64
# 查看GRUB2安装位置
# ls -la /boot/grub2/
total 1024
-rw-r–r– 1 root root 1234 Apr 2 10:00:00 device.map
-rw-r–r– 1 root root 567890 Apr 2 10:00:00 grub.cfg
drwxr-xr-x 2 root root 4096 Apr 2 10:00:00 i386-pc
drwxr-xr-x 2 root root 4096 Apr 2 10:00:00 fonts
drwxr-xr-x 2 root root 4096 Apr 2 10:00:00 locale
drwxr-xr-x 2 root root 4096 Apr 2 10:00:00 themes
2. GRUB2版本检查
在生产环境中,了解GRUB2的版本信息对于故障排查和兼容性检查非常重要。学习交流加群风哥微信: itpux-com from LinuxDBA视频:www.itpux.com
# grub2-install –version
grub2-install (GRUB) 2.06
# 查看GRUB2包的详细信息
# rpm -qi grub2-pc
Name : grub2-pc
Version : 2.06
Release : 81.el10
Architecture: x86_64
Install Date: Wed Apr 2 10:00:00 2026
Group : System Environment/Base
Size : 1234567
License : GPLv3+
Signature : RSA/SHA256, Wed Apr 2 10:00:00 2026, Key ID 199e2f91fd431d51
Source RPM : grub2-2.06-81.el10.src.rpm
Build Date : Wed Apr 2 10:00:00 2026
Build Host : x86-01.build.eng.bos.redhat.com
Relocations : (not relocatable)
Packager : Red Hat, Inc.
Vendor : Red Hat, Inc.
URL : http://www.gnu.org/software/grub/
Summary : GRUB2 for PC BIOS systems
Description :
This package provides the GRUB2 boot loader for PC BIOS systems.
It includes the GRUB2 core image, modules, and tools.
# 查看GRUB2模块列表
# ls /boot/grub2/i386-pc/
acpi.mod ext2.mod linux.mod reboot.mod
ata.mod ext4.mod loadenv.mod scsi.mod
biosdisk.mod fat.mod ls.mod search.mod
boot.mod fshelp.mod lvm.mod serial.mod
cat.mod gcry_sha256.mod mdraid09.mod setjmp.mod
chain.mod gcry_sha512.mod memdisk.mod sleep.mod
cmp.mod gettext.mod minix.mod terminal.mod
configfile.mod gpt.mod mmap.mod test.mod
cpio.mod gzio.mod msdospart.mod tftp.mod
crc.mod halt.mod multiboot.mod udf.mod
crypto.mod help.mod ntfscomp.mod vbe.mod
date.mod hexdump.mod ntfs.mod vga_text.mod
disk.mod hfsplus.mod part_gpt.mod video.mod
dm_nv.mod iso9660.mod part_msdos.mod xfs.mod
echo.mod jpeg.mod password.mod zfs.mod
3. GRUB2配置文件详解
GRUB2的主要配置文件是/boot/grub2/grub.cfg,但该文件通常由脚本自动生成,不应直接编辑。实际配置应在/etc/default/grub和/etc/grub.d/目录中进行。学习交流加群风哥QQ113257174
# cat /boot/grub2/grub.cfg | head -50
#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub2-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#
### BEGIN /etc/grub.d/00_header ###
set pager=1
if [ -s $prefix/grubenv ]; then
load_env
fi
if [ “${next_entry}” ] ; then
set default=”${next_entry}”
set next_entry=
save_env next_entry
set boot_once=true
else
set default=”${saved_entry}”
fi
if [ x”${feature_menuentry_id}” = xy ]; then
menuentry_id_option=”–id”
else
menuentry_id_option=””
fi
export menuentry_id_option
if [ “${prev_saved_entry}” ]; then
set saved_entry=”${prev_saved_entry}”
save_env saved_entry
set prev_saved_entry=
save_env prev_saved_entry
fi
if [ “${feature_platform_search_hint}” = xy ]; then
search –no-floppy –fs-uuid –set=root –hint-bios=hd0,msdos1 –hint-efi=hd0,msdos1
–hint-baremetal=ahci0,msdos1 12345678-1234-1234-1234-123456789012
else
search –no-floppy –fs-uuid –set=root 12345678-1234-1234-1234-123456789012
fi
# cat /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=”$(sed ‘s, release .*$,,g’ /etc/system-release)”
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT=”console”
GRUB_CMDLINE_LINUX=”crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet”
GRUB_DISABLE_RECOVERY=”true”
# 查看GRUB2配置脚本目录
# ls -la /etc/grub.d/
total 68
-rwxr-xr-x 1 root root 1234 Apr 2 10:00:00 00_header
-rwxr-xr-x 1 root root 567 Apr 2 10:00:00 10_linux
-rwxr-xr-x 1 root root 890 Apr 2 10:00:00 20_linux_xen
-rwxr-xr-x 1 root root 1234 Apr 2 10:00:00 20_ppc_terminfo
-rwxr-xr-x 1 root root 567 Apr 2 10:00:00 30_os-prober
-rwxr-xr-x 1 root root 890 Apr 2 10:00:00 30_uefi-firmware
-rwxr-xr-x 1 root root 1234 Apr 2 10:00:00 40_custom
-rwxr-xr-x 1 root root 567 Apr 2 10:00:00 41_custom
-rwxr-xr-x 1 root root 890 Apr 2 10:00:00 README
/boot/grub2/grub.cfg命令才能使配置生效。修改前务必备份原始文件。更多学习教程公众号风哥教程itpux_com
4. 默认启动项配置
GRUB2支持设置默认启动项,可以指定菜单项的编号或名称,也可以设置为”saved”以使用上次启动的选项。
# grep “^menuentry” /boot/grub2/grub.cfg | cut -d “‘” -f2
Red Hat Enterprise Linux (5.14.0-123.el10.x86_64) 10
Red Hat Enterprise Linux (5.14.0-122.el10.x86_64) 10
Red Hat Enterprise Linux (0-rescue-1234567890abcdef1234567890abcdef) 10
# 查看当前默认启动项
# grub2-editenv list
saved_entry=Red Hat Enterprise Linux (5.14.0-123.el10.x86_64) 10
# 设置默认启动项为第一个(索引从0开始)
# grub2-set-default 0
# 验证默认启动项
# grub2-editenv list
saved_entry=0
# 设置默认启动项为指定内核
# grub2-set-default “Red Hat Enterprise Linux (5.14.0-123.el10.x86_64) 10”
# 验证默认启动项
# grub2-editenv list
saved_entry=Red Hat Enterprise Linux (5.14.0-123.el10.x86_64) 10
# vi /etc/default/grub
# 修改GRUB_DEFAULT参数
GRUB_DEFAULT=0
# 或使用菜单项名称
GRUB_DEFAULT=”Red Hat Enterprise Linux (5.14.0-123.el10.x86_64) 10″
# 或设置为saved(使用上次启动的选项)
GRUB_DEFAULT=saved
# 重新生成GRUB2配置
# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file …
Found linux image: /boot/vmlinuz-5.14.0-123.el10.x86_64
Found initrd image: /boot/initramfs-5.14.0-123.el10.x86_64.img
Found linux image: /boot/vmlinuz-5.14.0-122.el10.x86_64
Found initrd image: /boot/initramfs-5.14.0-122.el10.x86_64.img
done
5. 启动超时时间设置
GRUB2的超时时间决定了启动菜单显示的时长,超过该时间后系统将自动启动默认项。
# grep GRUB_TIMEOUT /etc/default/grub
GRUB_TIMEOUT=5
# 修改超时时间为10秒
# vi /etc/default/grub
GRUB_TIMEOUT=10
# 重新生成GRUB2配置
# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file …
Found linux image: /boot/vmlinuz-5.14.0-123.el10.x86_64
Found initrd image: /boot/initramfs-5.14.0-123.el10.x86_64.img
Found linux image: /boot/vmlinuz-5.14.0-122.el10.x86_64
Found initrd image: /boot/initramfs-5.14.0-122.el10.x86_64.img
done
# 设置为0秒(直接启动,不显示菜单)
# vi /etc/default/grub
GRUB_TIMEOUT=0
# 设置为-1秒(无限等待,直到用户选择)
# vi /etc/default/grub
GRUB_TIMEOUT=-1
6. 内核参数配置
GRUB2允许在启动时向内核传递参数,这些参数可以影响系统的行为和性能。
# cat /proc/cmdline
BOOT_IMAGE=/vmlinuz-5.14.0-123.el10.x86_64 root=/dev/mapper/rhel-root ro crashkernel=auto
rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet
# 修改/etc/default/grub添加内核参数
# vi /etc/default/grub
# 在GRUB_CMDLINE_LINUX中添加参数
GRUB_CMDLINE_LINUX=”crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet
transparent_hugepage=never numa=off”
# 重新生成GRUB2配置
# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file …
Found linux image: /boot/vmlinuz-5.14.0-123.el10.x86_64
Found initrd image: /boot/initramfs-5.14.0-123.el10.x86_64.img
Found linux image: /boot/vmlinuz-5.14.0-122.el10.x86_64
Found initrd image: /boot/initramfs-5.14.0-122.el10.x86_64.img
done
# 重启系统使参数生效
# reboot
# 重启后验证内核参数
# cat /proc/cmdline
BOOT_IMAGE=/vmlinuz-5.14.0-123.el10.x86_64 root=/dev/mapper/rhel-root ro crashkernel=auto
rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet transparent_hugepage=never numa=off
# 禁用IPv6
GRUB_CMDLINE_LINUX=”… ipv6.disable=1″
# 设置最大文件句柄数
GRUB_CMDLINE_LINUX=”… fs.file-max=6815744″
# 设置内核日志级别
GRUB_CMDLINE_LINUX=”… loglevel=3″
# 启用SELinux
GRUB_CMDLINE_LINUX=”… enforcing=1″
# 禁用SELinux
GRUB_CMDLINE_LINUX=”… selinux=0″
# 设置CPU隔离(用于实时系统)
GRUB_CMDLINE_LINUX=”… isolcpus=1,3,5,7″
# 设置大页内存
GRUB_CMDLINE_LINUX=”… default_hugepagesz=2M hugepagesz=2M hugepages=20480″
7. 自定义启动菜单项
可以通过编辑/etc/grub.d/40_custom文件来添加自定义的启动菜单项。
# cat /etc/grub.d/40_custom
#!/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries. Simply type the
# menu entries you want to add after this comment. Be careful not to change
# the ‘exec tail’ line above.
# 添加自定义启动项
# vi /etc/grub.d/40_custom
# 添加以下内容
menuentry ‘RHEL 10 Rescue Mode’ {
load_video
set gfxpayload=keep
insmod gzio
insmod part_msdos
insmod xfs
set root=’hd0,msdos1′
echo ‘Loading kernel …’
linux /vmlinuz-5.14.0-123.el10.x86_64 root=/dev/mapper/rhel-root ro rd.break
echo ‘Loading initial ramdisk …’
initrd /initramfs-5.14.0-123.el10.x86_64.img
}
menuentry ‘RHEL 10 Single User Mode’ {
load_video
set gfxpayload=keep
insmod gzio
insmod part_msdos
insmod xfs
set root=’hd0,msdos1′
echo ‘Loading kernel …’
linux /vmlinuz-5.14.0-123.el10.x86_64 root=/dev/mapper/rhel-root ro single
echo ‘Loading initial ramdisk …’
initrd /initramfs-5.14.0-123.el10.x86_64.img
}
# 重新生成GRUB2配置
# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file …
Found linux image: /boot/vmlinuz-5.14.0-123.el10.x86_64
Found initrd image: /boot/initramfs-5.14.0-123.el10.x86_64.img
Found linux image: /boot/vmlinuz-5.14.0-122.el10.x86_64
Found initrd image: /boot/initramfs-5.14.0-122.el10.x86_64.img
done
# grep -A 5 “RHEL 10 Rescue Mode” /boot/grub2/grub.cfg
menuentry ‘RHEL 10 Rescue Mode’ –class rhel –class gnu-linux –class gnu –class os $menuentry_id_option
‘gnulinux-simple-12345678-1234-1234-1234-123456789012′ {
load_video
set gfxpayload=keep
insmod gzio
insmod part_msdos
insmod xfs
set root=’hd0,msdos1’
echo ‘Loading kernel …’
linux /vmlinuz-5.14.0-123.el10.x86_64 root=/dev/mapper/rhel/root ro rd.break
echo ‘Loading initial ramdisk …’
initrd /initramfs-5.14.0-123.el10.x86_64.img
}
8. GRUB2配置更新
修改GRUB2配置后,必须使用grub2-mkconfig命令重新生成配置文件才能使更改生效。 更多视频教程www.fgedu.net.cn
# cp /boot/grub2/grub.cfg /boot/grub2/grub.cfg.backup
# 修改/etc/default/grub
# vi /etc/default/grub
GRUB_TIMEOUT=10
GRUB_CMDLINE_LINUX=”crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet
transparent_hugepage=never”
# 重新生成GRUB2配置
# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file …
Found linux image: /boot/vmlinuz-5.14.0-123.el10.x86_64
Found initrd image: /boot/initramfs-5.14.0-123.el10.x86_64.img
Found linux image: /boot/vmlinuz-5.14.0-122.el10.x86_64
Found initrd image: /boot/initramfs-5.14.0-122.el10.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-1234567890abcdef1234567890abcdef
Found initrd image: /boot/initramfs-0-rescue-1234567890abcdef1234567890abcdef.img
done
# 验证配置文件
# grep “GRUB_TIMEOUT” /boot/grub2/grub.cfg
set timeout=10
# 验证内核参数
# grep “transparent_hugepage” /boot/grub2/grub.cfg
linux /vmlinuz-5.14.0-123.el10.x86_64 root=/dev/mapper/rhel/root ro crashkernel=auto rd.lvm.lv=rhel/root
rd.lvm.lv=rhel/swap rhgb quiet transparent_hugepage=never
# grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg
Generating grub configuration file …
Found linux image: /boot/vmlinuz-5.14.0-123.el10.x86_64
Found initrd image: /boot/initramfs-5.14.0-123.el10.x86_64.img
Found linux image: /boot/vmlinuz-5.14.0-122.el10.x86_64
Found initrd image: /boot/initramfs-5.14.0-122.el10.x86_64.img
done
# 验证EFI配置文件
# ls -la /boot/efi/EFI/redhat/
total 2048
-rwxr-xr-x 1 root root 1234567 Apr 2 10:00:00 grub.cfg
-rwxr-xr-x 1 root root 567890 Apr 2 10:00:00 grubx64.efi
-rwxr-xr-x 1 root root 234567 Apr 2 10:00:00 shim.efi
9. GRUB2故障排查
GRUB2故障可能导致系统无法启动,需要掌握基本的故障排查和修复方法。
# grub2-fstest /boot/grub2/grub.cfg
Configuration file is valid
# 检查GRUB2安装状态
# grub2-install –recheck /dev/sda
Installing for i386-pc platform.
Installation finished. No error reported.
# 重新安装GRUB2到MBR
# grub2-install /dev/sda
Installing for i386-pc platform.
Installation finished. No error reported.
# 检查GRUB2环境变量
# grub2-editenv list
saved_entry=Red Hat Enterprise Linux (5.14.0-123.el10.x86_64) 10
kernelopts=root=/dev/mapper/rhel-root ro crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet
# 清除GRUB2环境变量
# grub2-editenv create
# grub2-editenv list
# 1. 使用RHEL 10安装介质启动
# 2. 在启动菜单中选择”Troubleshooting” -> “Rescue a Red Hat Enterprise Linux system”
# 3. 选择”1) Continue”挂载原系统
# 4. 切换到原系统的根目录
# chroot /mnt/sysimage
# 重新安装GRUB2
# grub2-install /dev/sda
Installing for i386-pc platform.
Installation finished. No error reported.
# 重新生成GRUB2配置
# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file …
Found linux image: /boot/vmlinuz-5.14.0-123.el10.x86_64
Found initrd image: /boot/initramfs-5.14.0-123.el10.x86_64.img
done
# 退出chroot环境
# exit
# 重启系统
# reboot
10. 生产环境最佳实践
在生产环境中,GRUB2配置需要遵循最佳实践,确保系统的稳定性和可维护性。
# cp /boot/grub2/grub.cfg /backup/grub.cfg.$(date +%Y%m%d)
# 2. 保留多个内核版本
# 查看已安装的内核
# rpm -qa | grep kernel
kernel-core-5.14.0-123.el10.x86_64
kernel-modules-5.14.0-123.el10.x86_64
kernel-core-5.14.0-122.el10.x86_64
kernel-modules-5.14.0-122.el10.x86_64
# 设置保留的内核版本数量
# vi /etc/dnf/dnf.conf
installonly_limit=3
# 3. 配置GRUB2密码保护
# 生成GRUB2密码
# grub2-mkpasswd-pbkdf2
Enter password:
Reenter password:
PBKDF2 hash of your password is
grub.pbkdf2.sha512.10000.1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF
# 编辑/etc/grub.d/00_users
# vi /etc/grub.d/00_users
cat <
echo “WARNING: GRUB2 installation check failed”
fi
# 检查内核文件
if [ ! -f /boot/vmlinuz-$(uname -r) ]; then
echo “ERROR: Kernel file not found”
exit 1
fi
echo “GRUB2 configuration check completed”
EOF
# 添加执行权限
# chmod +x /fgedu/shell/check_grub.sh
# 定期执行检查
# echo “0 3 * * * /fgedu/shell/check_grub.sh >> /var/log/grub_check.log 2>&1” >> /etc/crontab
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
