Linux切换内核版本

Linux切换内核版本

有些软件会对Linux内核的版本有要求。为此我们需要安装并切换到指定的内核版本。本文以5.4.0-26-generic为例。

参考:LayerStack Tutorials - LayerStack - How to change the default Boot Kernel on Linux Cloud Servers

  • 下载指定版本
sudo apt-get install -y linux-image-${_target_kernel_}
sudo apt-get install -y linux-headers-${_target_kernel_}
  • 然后查看可用的内核版本
cat /boot/grub/grub.cfg | grep -E 'menuentry.+class.+class.+menuentry_id_option'

结果:

menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-53b6e16f-1415-457e-85fe-9805a4c3e773' {
        menuentry 'Ubuntu, with Linux 5.15.0-69-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-69-generic-advanced-53b6e16f-1415-457e-85fe-9805a4c3e773' {
        menuentry 'Ubuntu, with Linux 5.15.0-69-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-69-generic-recovery-53b6e16f-1415-457e-85fe-9805a4c3e773' {
        menuentry 'Ubuntu, with Linux 5.15.0-60-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-60-generic-advanced-53b6e16f-1415-457e-85fe-9805a4c3e773' {
        menuentry 'Ubuntu, with Linux 5.15.0-60-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-60-generic-recovery-53b6e16f-1415-457e-85fe-9805a4c3e773' {
        menuentry 'Ubuntu, with Linux 5.15.0-58-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-58-generic-advanced-53b6e16f-1415-457e-85fe-9805a4c3e773' {
        menuentry 'Ubuntu, with Linux 5.15.0-58-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-58-generic-recovery-53b6e16f-1415-457e-85fe-9805a4c3e773' {
        menuentry 'Ubuntu, with Linux 5.4.0-26-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.4.0-26-generic-advanced-53b6e16f-1415-457e-85fe-9805a4c3e773' {
        menuentry 'Ubuntu, with Linux 5.4.0-26-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.4.0-26-generic-recovery-53b6e16f-1415-457e-85fe-9805a4c3e773' {

可以看到,里面一共包含两个带5.4.0-26-generic的版本。其中一个是recovery mode。我们不要选择recovery mode,选择正常的那个。

  • 然后修改系统的默认内核版本
sudo vi /etc/default/grub

可以看到其中的

GRUB_DEFAULT="1> 4"

我们把1> x中的x改为我们想要的版本。x就是在上面那个大表中你想要的版本是第几个。这里注意千万不要弄错!这里我具体地列出每个版本对应的编号

0 Ubuntu, with Linux 5.15.0-69-generic
1 Ubuntu, with Linux 5.15.0-69-generic (recovery mode)
2 Ubuntu, with Linux 5.15.0-60-generic
3 Ubuntu, with Linux 5.15.0-60-generic (recovery mode)
4 Ubuntu, with Linux 5.15.0-58-generic
5 Ubuntu, with Linux 5.15.0-58-generic (recovery mode)
6 Ubuntu, with Linux 5.4.0-26-generic
7 Ubuntu, with Linux 5.4.0-26-generic (recovery mode)

既然我想要的版本是5.4.0,那么这里就应该改成

GRUB_DEFAULT="1> 6"
  • 最后,更新配置并重启
sudo update-grub
sudo reboot

如果配置错了怎么办?

很不巧,我在配置默认内核版本的时候犯傻了。我把x改成了5,导致系统以一个recovery mode重启了。

由于我使用的是远程服务器,而recovery mode中并没有ssh服务,导致重启之后我就再也无法从远程访问服务器了。

于是没办法只能拜托师兄去机房手动重启服务器。我不知道具体的过程,但应该与grub2 - How can I boot with an older kernel version? - Ask Ubuntu差不多。

大致就是重启的时候,按住shift键,进入grub界面,在里面手动选择想要进入的内核版本即可。

但是手动启动后,默认的内核版本还是错的,此时仍然需要执行上面的流程,将默认版本改对,再次重启。

posted @ 2023-04-20 11:03  CQzhangyu  阅读(889)  评论(0编辑  收藏  举报