内核管理
Enterprise Linux 的内核管理包括安装、选择默认版本、清理旧内核以及从第三方仓库安装更新的内核。
查看内核信息
Section titled “查看内核信息”查看当前运行的内核
Section titled “查看当前运行的内核”uname -r查看详细内核信息
Section titled “查看详细内核信息”uname -a列出所有已安装的内核
Section titled “列出所有已安装的内核”rpm -qa kernel-core | sort -V使用 dnf 查看已安装内核
Section titled “使用 dnf 查看已安装内核”dnf list installed kernel-core查看可用的内核更新
Section titled “查看可用的内核更新”dnf check-update kernel使用 grubby 管理引导
Section titled “使用 grubby 管理引导”grubby 是管理 GRUB 引导项的标准命令行工具。
查看所有引导项
Section titled “查看所有引导项”grubby --info=ALL查看当前默认内核
Section titled “查看当前默认内核”grubby --default-kernel查看默认内核的索引号
Section titled “查看默认内核的索引号”grubby --default-index设置默认内核
Section titled “设置默认内核”grubby --set-default /boot/vmlinuz-5.14.0-362.8.1.el9_3.x86_64grubby --set-default-index 1为特定内核添加引导参数
Section titled “为特定内核添加引导参数”grubby --update-kernel /boot/vmlinuz-$(uname -r) --args="nouveau.modeset=0"移除内核引导参数
Section titled “移除内核引导参数”grubby --update-kernel /boot/vmlinuz-$(uname -r) --remove-args="quiet"为所有内核添加参数
Section titled “为所有内核添加参数”grubby --update-kernel ALL --args="crashkernel=auto"当新内核导致系统异常时,可以快速切换到旧版本。
方法一:GRUB 菜单选择
Section titled “方法一:GRUB 菜单选择”重启系统时,在 GRUB 菜单中选择旧版本内核启动。如果 GRUB 菜单不显示,在启动时按住 Shift 键或按 Esc 键。
方法二:使用 grubby 切换
Section titled “方法二:使用 grubby 切换”# 查看可用内核grubby --info=ALL | grep -E "^kernel|^index"
# 设置旧版本为默认grubby --set-default /boot/vmlinuz-<旧版本号>
# 重启生效reboot方法三:使用 dnf 回滚
Section titled “方法三:使用 dnf 回滚”# 查找内核安装的事务 IDdnf history list | grep kernel
# 撤销该事务dnf history undo <事务ID> -y查看 installonly_limit 配置
Section titled “查看 installonly_limit 配置”installonly_limit 控制保留多少个内核版本。
grep installonly_limit /etc/dnf/dnf.conf默认值通常为 3,表示最多保留 3 个内核版本。
修改 installonly_limit
Section titled “修改 installonly_limit”# 修改为保留 2 个内核(最少建议保留 2 个)echo "installonly_limit=2" >> /etc/dnf/dnf.conf手动删除旧内核
Section titled “手动删除旧内核”dnf remove kernel-core-<版本号> -y使用 package-cleanup 清理旧内核
Section titled “使用 package-cleanup 清理旧内核”dnf install -y dnf-plugins-corepackage-cleanup --oldkernels --count=2清理旧内核后重建 GRUB
Section titled “清理旧内核后重建 GRUB”grub2-mkconfig -o /boot/grub2/grub.cfg对于 UEFI 系统:
grub2-mkconfig -o /boot/efi/EFI/almalinux/grub.cfg从 ELRepo 安装新版本内核
Section titled “从 ELRepo 安装新版本内核”ELRepo 提供两种追踪分支的内核:
- kernel-ml — Mainline 稳定版(最新特性)
- kernel-lt — Long Term 长期支持版(更保守)
安装 ELRepo 仓库
Section titled “安装 ELRepo 仓库”rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.orgdnf install -y https://www.elrepo.org/elrepo-release-9.el9.elrepo.noarch.rpm对于 EL 8 系统:
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.orgdnf install -y https://www.elrepo.org/elrepo-release-8.el8.elrepo.noarch.rpm查看可用的 ELRepo 内核
Section titled “查看可用的 ELRepo 内核”dnf --enablerepo=elrepo-kernel list available | grep kernel安装 Mainline 内核
Section titled “安装 Mainline 内核”dnf --enablerepo=elrepo-kernel install -y kernel-ml安装 Long Term 内核
Section titled “安装 Long Term 内核”dnf --enablerepo=elrepo-kernel install -y kernel-lt设置 ELRepo 内核为默认
Section titled “设置 ELRepo 内核为默认”grubby --default-kernel# 如果不是新内核,手动设置grubby --set-default /boot/vmlinuz-<ELRepo内核版本>安装配套的内核工具
Section titled “安装配套的内核工具”dnf --enablerepo=elrepo-kernel install -y kernel-ml-devel kernel-ml-headersdnf --enablerepo=elrepo-kernel install -y kernel-lt-devel kernel-lt-headers内核参数调优
Section titled “内核参数调优”查看当前内核参数
Section titled “查看当前内核参数”sysctl -a临时修改内核参数
Section titled “临时修改内核参数”sysctl -w net.core.somaxconn=65535永久修改内核参数
Section titled “永久修改内核参数”cat > /etc/sysctl.d/99-custom.conf << 'EOF'# 网络优化net.core.somaxconn = 65535net.ipv4.tcp_max_syn_backlog = 65535
# 内存管理vm.swappiness = 10vm.dirty_ratio = 40vm.dirty_background_ratio = 10EOF
# 立即生效sysctl --system内核模块管理
Section titled “内核模块管理”列出已加载的模块
Section titled “列出已加载的模块”lsmod查看模块信息
Section titled “查看模块信息”modinfo <模块名>加载和卸载模块
Section titled “加载和卸载模块”modprobe <模块名>modprobe -r <模块名>设置开机自动加载模块
Section titled “设置开机自动加载模块”echo "<模块名>" > /etc/modules-load.d/<模块名>.conf黑名单禁用模块
Section titled “黑名单禁用模块”echo "blacklist <模块名>" > /etc/modprobe.d/blacklist-<模块名>.conf