环境介绍
| Ubuntu24/4U/12G/120G/NAT |
| 172.16.186.148/24 |
| |
| |
| rambo@test1:~$ uname -a |
| Linux test1.lab.example.com 6.8.0-35-generic #35-Ubuntu SMP PREEMPT_DYNAMIC Mon May 20 15:51:52 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux |
| |
| |
| |
| 本次因为使用的是最新的发行版和最新的内核,所以出现了问题,但过程没问题 |
| |
| |
前期安装
| # 安装编译内核所需的软件包和工具 |
| rambo@test1:~$ sudo apt update |
| rambo@test1:~$ sudo apt install -y build-essential libncurses-dev bison flex libssl-dev libelf-dev bc git |
| 释义: |
| build-essential:基本的编译工具和环境 |
| libncurses-dev:用于配置内核的menuconfig或其他配置工具 |
| bison 和 flex:用于解析配置文件和生成代码 |
| libssl-dev:提供加密库的开发文件 |
| libelf-dev:提供 ELF 文件处理库的开发文件 |
| bc:用于内核配置时的数学计算 |
下载内核源代码并编译
| rambo@test1:~$ wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.9.4.tar.xz |
| |
| # 配置内核 |
| rambo@test1:~$ tar -xvf linux-6.9.4.tar.xz |
| rambo@test1:~$ cd linux-6.9.4/ |
| rambo@test1:~/linux-6.9.4$ make defconfig # 使用默认配置文件 |
| 注:如果需要进行更详细的配置,可以运行 make menuconfig 或 make xconfig 来打开配置界面 |
| |
| |
| # 编译内核 |
| rambo@test1:~/linux-6.9.4$ make -j$(nproc) # 使用所有 CPU 核心并行编译 |
| |
| # 安装编译好的内核模块 |
| rambo@test1:~/linux-6.9.4$ sudo make modules_install |
| |
| # 安装新编译的内核 |
| rambo@test1:~/linux-6.9.4$ sudo make install |
| 注:这将会更新启动加载程序(GRUB)的配置并安装新的内核映像文件 |
| INSTALL /boot |
| run-parts: executing /etc/kernel/postinst.d/initramfs-tools 6.9.4 /boot/vmlinuz-6.9.4 |
| update-initramfs: Generating /boot/initrd.img-6.9.4 |
| I: The initramfs will attempt to resume from /dev/sda3 |
| I: (UUID=d6f791dd-533b-41ab-94e8-77d270186fa3) |
| I: Set the RESUME variable to override this. |
| run-parts: executing /etc/kernel/postinst.d/unattended-upgrades 6.9.4 /boot/vmlinuz-6.9.4 |
| run-parts: executing /etc/kernel/postinst.d/update-notifier 6.9.4 /boot/vmlinuz-6.9.4 |
| run-parts: executing /etc/kernel/postinst.d/xx-update-initrd-links 6.9.4 /boot/vmlinuz-6.9.4 |
| I: /boot/initrd.img is now a symlink to initrd.img-6.9.4 |
| run-parts: executing /etc/kernel/postinst.d/zz-update-grub 6.9.4 /boot/vmlinuz-6.9.4 |
| Sourcing file `/etc/default/grub' |
| Generating grub configuration file ... |
| Found linux image: /boot/vmlinuz-6.9.4 |
| Found initrd image: /boot/initrd.img-6.9.4 |
| Found linux image: /boot/vmlinuz-6.8.0-35-generic |
| Found initrd image: /boot/initrd.img-6.8.0-35-generic |
| Found memtest86+x64 image: /memtest86+x64.bin |
| Warning: os-prober will not be executed to detect other bootable partitions. |
| Systems on them will not be added to the GRUB boot configuration. |
| Check GRUB_DISABLE_OS_PROBER documentation entry. |
| Adding boot menu entry for UEFI Firmware Settings ... |
| done |
| |
打包并更新内核
| # 编译并打包内核为 .deb 包 |
| rambo@test1:~/linux-6.9.4$ sudo apt-get install -y debhelper dh-virtualenv |
| rambo@test1:~/linux-6.9.4$ make -j$(nproc) bindeb-pkg |
| 注:这将会在上级目录中生成内核的 .deb 安装包,包括内核映像文件和相关的模块文件 |
| |
| # 在上级目录中会找到生成的 .deb 包 |
| rambo@test1:~/linux-6.9.4$ ls -al ../linux-*.deb |
| -rw-r--r-- 1 rambo rambo 9253982 6月 14 17:25 ../linux-headers-6.9.4_6.9.4-3_amd64.deb |
| -rw-r--r-- 1 rambo rambo 13980520 6月 14 17:24 ../linux-image-6.9.4_6.9.4-3_amd64.deb |
| -rw-r--r-- 1 rambo rambo 1401168 6月 14 17:24 ../linux-libc-dev_6.9.4-3_amd64.deb |
| |
| # 安装内核的 .deb 包 |
| rambo@test1:~/linux-6.9.4$ sudo dpkg -i ../linux-*.deb |
| |
| # 更新 GRUB 配置 |
| rambo@test1:~/linux-6.9.4$ sudo update-grub |
| |
| |
| rambo@test1:~/linux-6.9.4$ dpkg -l | grep 6.9.4 |
| ii linux-headers-6.9.4 6.9.4-2 amd64 Linux kernel headers for 6.9.4 on amd64 |
| ii linux-image-6.9.4 6.9.4-2 amd64 Linux kernel, version 6.9.4 |
| ii linux-libc-dev:amd64 6.9.4-2 amd64 Linux support headers for userspace development |
| |
| |
| |
| |
| # 重启来加载新安装的内核 |
| rambo@test1:~/linux-6.9.4$ sudo reboot |
| 注:重启后进不去系统 |
| |
| |
| |
重启无法进入系统问题
| 1. 启动到GRUB菜单 |
| 在开机时按住 Shift 键或 Esc 键(具体取决于你的系统),直到出现 GRUB 菜单。如果系统默认直接进入新内核,GRUB 菜单可能被跳过,所以需要手动进入。 |
| |
| 2. 选择旧内核启动 |
| 在 GRUB 菜单中,选择“Advanced options for Ubuntu”,然后选择你之前使用的旧内核版本(例如,内核 5.x.x)。按 Enter 键启动系统。 |
| |
| 3. 移除有问题的新内核 |
| 成功启动系统后,可以移除有问题的新内核。打开终端并执行以下命令: |
| sudo apt remove linux-image-6.9.4-* |
| sudo apt autoremove |
| |
| 4. 更新 GRUB 配置 |
| 确保 GRUB 配置文件更新正确,以便下次启动时不再尝试加载有问题的内核: |
| sudo update-grub |
| |
| 5. 检查日志文件 |
| 查看日志文件以了解系统无法启动的具体原因。以下命令可以帮助你查看系统日志: |
| journalctl -xb |
| |
| 6. 重新安装或升级内核 |
| 如果你仍然希望使用新版本内核,可以尝试从 kernel.ubuntu.com 获取正式发布的内核包并进行重新安装 |
| |
| 7. 下载并安装内核 |
| 从官方 PPA 下载所需版本的内核包: |
| wget https://kernel.ubuntu.com/~kernel-ppa/mainline/v6.9.4/amd64/linux-headers-6.9.4-xxxxxx_all.deb |
| wget https://kernel.ubuntu.com/~kernel-ppa/mainline/v6.9.4/amd64/linux-headers-6.9.4-xxxxxx_amd64.deb |
| wget https://kernel.ubuntu.com/~kernel-ppa/mainline/v6.9.4/amd64/linux-image-6.9.4-xxxxxx_amd64.deb |
| wget https://kernel.ubuntu.com/~kernel-ppa/mainline/v6.9.4/amd64/linux-modules-6.9.4-xxxxxx_amd64.deb |
| |
| sudo dpkg -i linux-headers-6.9.4-xxxxxx_all.deb linux-headers-6.9.4-xxxxxx_amd64.deb linux-image-6.9.4-xxxxxx_amd64.deb linux-modules-6.9.4-xxxxxx_amd64.deb |
| |
| 8. 再次更新 GRUB 配置 |
| 更新 GRUB 配置: |
| sudo update-grub |
| |
| 9. 重启系统 |
| 重新启动系统以查看问题是否解决: |
| sudo reboot |
| |
注意事项
| 编译和打包内核是一个耗时的过程,特别是在较慢的计算机上可能需要一段时间 |
| 在配置和编译过程中可能会遇到依赖问题或错误消息,根据实际情况解决问题 |
| 在更新内核之前,请确保备份重要数据,并且理解在此过程中可能引入的系统变化和风险 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」