跳转到内容

内核管理

Enterprise Linux 的内核管理包括安装、选择默认版本、清理旧内核以及从第三方仓库安装更新的内核。

查看当前内核版本
uname -r
查看完整内核信息
uname -a
列出已安装的内核包
rpm -qa kernel-core | sort -V
通过 dnf 列出内核
dnf list installed kernel-core
检查内核更新
dnf check-update kernel

grubby 是管理 GRUB 引导项的标准命令行工具。

列出所有可用内核引导项
grubby --info=ALL
查看默认引导内核
grubby --default-kernel
查看默认内核索引
grubby --default-index
按路径设置默认内核
grubby --set-default /boot/vmlinuz-5.14.0-362.8.1.el9_3.x86_64
按索引设置默认内核(0 为最新)
grubby --set-default-index 1
为指定内核添加启动参数
grubby --update-kernel /boot/vmlinuz-$(uname -r) --args="nouveau.modeset=0"
移除指定内核的启动参数
grubby --update-kernel /boot/vmlinuz-$(uname -r) --remove-args="quiet"
为所有内核添加全局启动参数
grubby --update-kernel ALL --args="crashkernel=auto"

当新内核导致系统异常时,可以快速切换到旧版本。

重启系统时,在 GRUB 菜单中选择旧版本内核启动。如果 GRUB 菜单不显示,在启动时按住 Shift 键或按 Esc 键。

切换默认内核到旧版本
# 查看可用内核
grubby --info=ALL | grep -E "^kernel|^index"
# 设置旧版本为默认
grubby --set-default /boot/vmlinuz-<旧版本号>
# 重启生效
reboot
通过 dnf 历史回滚内核更新
# 查找内核安装的事务 ID
dnf history list | grep kernel
# 撤销该事务
dnf history undo <事务ID> -y

installonly_limit 控制保留多少个内核版本。

查看当前 installonly_limit
grep installonly_limit /etc/dnf/dnf.conf

默认值通常为 3,表示最多保留 3 个内核版本。

设置保留的内核数量
# 修改为保留 2 个内核(最少建议保留 2 个)
echo "installonly_limit=2" >> /etc/dnf/dnf.conf
删除指定的旧内核
dnf remove kernel-core-<版本号> -y
保留最近 2 个内核,删除其余的
dnf install -y dnf-plugins-core
package-cleanup --oldkernels --count=2
重新生成 GRUB 配置
grub2-mkconfig -o /boot/grub2/grub.cfg

对于 UEFI 系统:

重建 UEFI 引导的 GRUB 配置
grub2-mkconfig -o /boot/efi/EFI/almalinux/grub.cfg

ELRepo 提供两种追踪分支的内核:

  • kernel-ml — Mainline 稳定版(最新特性)
  • kernel-lt — Long Term 长期支持版(更保守)
导入 ELRepo GPG 密钥并安装仓库
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
dnf install -y https://www.elrepo.org/elrepo-release-9.el9.elrepo.noarch.rpm

对于 EL 8 系统:

安装 ELRepo(EL 8)
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
dnf install -y https://www.elrepo.org/elrepo-release-8.el8.elrepo.noarch.rpm
列出 ELRepo 中可用的内核
dnf --enablerepo=elrepo-kernel list available | grep kernel
安装 kernel-ml
dnf --enablerepo=elrepo-kernel install -y kernel-ml
安装 kernel-lt
dnf --enablerepo=elrepo-kernel install -y kernel-lt
将新安装的 ELRepo 内核设为默认
grubby --default-kernel
# 如果不是新内核,手动设置
grubby --set-default /boot/vmlinuz-<ELRepo内核版本>
安装 kernel-ml 配套工具
dnf --enablerepo=elrepo-kernel install -y kernel-ml-devel kernel-ml-headers
安装 kernel-lt 配套工具
dnf --enablerepo=elrepo-kernel install -y kernel-lt-devel kernel-lt-headers
查看所有 sysctl 参数
sysctl -a
临时调整参数(重启后失效)
sysctl -w net.core.somaxconn=65535
创建持久化的内核参数配置
cat > /etc/sysctl.d/99-custom.conf << 'EOF'
# 网络优化
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
# 内存管理
vm.swappiness = 10
vm.dirty_ratio = 40
vm.dirty_background_ratio = 10
EOF
# 立即生效
sysctl --system
查看已加载的内核模块
lsmod
查看指定模块的详细信息
modinfo <模块名>
手动加载内核模块
modprobe <模块名>
卸载内核模块
modprobe -r <模块名>
配置开机自动加载模块
echo "<模块名>" > /etc/modules-load.d/<模块名>.conf
禁止加载指定模块
echo "blacklist <模块名>" > /etc/modprobe.d/blacklist-<模块名>.conf