How-to Tutorials

Reverting to a Previous Linux Kernel Version

If you’ve recently installed updates on your Linux OS and as a result are not unable to boot successfully it’s possible that there is some software conflict at play. Often times servers will have their kernel version updated and go months without rebooting. This means that if there is some conflict it could be lying in wait when the next reboot takes place.

If you have a linux server that will not boot and you believe it could be due to the latest installed kernel you can correct this by reverting the kernel version.

If you have console access…

You should be able to pretty easily select an older kernel version during the boot up screen. The newest will usually already be highlighted. The bottom kernel version should be the oldest. Just try each kernel version down the list until you find one that works for you. It should look something like the following depending on your version of linux.

If you do NOT have console access…

If you're working on an AWS instance the Accessing the filesystem of an impaired AWS EC2 Instance article may be helpful. If you have attached your root filesystem to another server and are now looking to make changes to that filesystem to revert the kernel you can do so by following these instructions.

In the examples below I am assuming you mounted the filesystem at/mnt. If you have mounted it somewhere else you’ll need to swap out “/mnt” for wherever you’ve mounted your filesystem.

You will “chroot” to the root directory of the troubled filesystem.

sudo mount -o bind /dev /mnt/dev
sudo mount -o bind /dev/shm /mnt/dev/shm
sudo mount -o bind /proc /mnt/proc
sudo mount -o bind /sys /mnt/sys
sudo chroot /mnt/

RHEL, CentOS, Amazon Linux, etc

Check your grub version so you know what method to use.

rpm -q grub ; rpm -q grub2
package grub is not installed
grub2-2.02-0.65.el7.centos.2.x86_64

Grub2

In the case of the above output, grub2 is being used so I would check the currently loaded kernel version with the following command.

sudo grub2-editenv list
saved_entry=Red Hat Enterprise Linux Server (3.10.0-862.3.2.el7.x86_64) 7.5 (Maipo)

I can then check to see what kernel versions are available to boot from.

sudo awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
0 : Red Hat Enterprise Linux Server 7.5 Rescue 67b65b74d08d45be852a3e9fc6cc1f34 (3.10.0-862.3.2.el7.x86_64)
1 : Red Hat Enterprise Linux Server (3.10.0-862.3.2.el7.x86_64) 7.5 (Maipo)
2 : Red Hat Enterprise Linux Server (3.10.0-862.el7.x86_64) 7.5 (Maipo)
3 : Red Hat Enterprise Linux Server (0-rescue-87aaf1cb8ace44eab1e4e693b7ca88bf) 7.5 (Maipo)

Here I can see that option 2 would be the older version. If I wanted to set this as the default and confirm the option then I would run the following.

sudo grub2-set-default 2
sudo grub2-editenv list
saved_entry=2

Grub 0.97

If you’re using grub 0.97 then you can simply edit /boot/grub/menu.lst changing the “default=” value to the desired menu entry. In the following example the first menu entry is 0, then 1 and finally 2.

cat /boot/grub/menu.lst
# created by imagebuilder
default=0
timeout=0
hiddenmenu

# Menu Entry 0
title Amazon Linux 2018.03 (4.14.42-52.37.amzn1.x86_64)
root (hd0,0)
kernel /boot/vmlinuz-4.14.42-52.37.amzn1.x86_64 root=LABEL=/ console=tty1 console=ttyS0 selinux=0 LANG=en_US.UTF-8 KEYTABLE=us
initrd /boot/initramfs-4.14.42-52.37.amzn1.x86_64.img

# Menu Entry 1
title Amazon Linux 2018.03 (4.14.33-51.37.amzn1.x86_64)
root (hd0,0)
kernel /boot/vmlinuz-4.14.33-51.37.amzn1.x86_64 root=LABEL=/ console=tty1 console=ttyS0 selinux=0 LANG=en_US.UTF-8 KEYTABLE=us
initrd /boot/initramfs-4.14.33-51.37.amzn1.x86_64.img

# Menu Entry 2
title Amazon Linux 2017.09 (4.9.75-25.55.amzn1.x86_64)
root (hd0,0)
kernel /boot/vmlinuz-4.9.75-25.55.amzn1.x86_64 root=LABEL=/ console=tty1 console=ttyS0 selinux=0 LANG=en_US.UTF-8 KEYTABLE=us
initrd /boot/initramfs-4.9.75-25.55.amzn1.x86_64.img

If kernel version 4.9.75-25.55 was desired then you would set default=2. You can edit this file with vi or nano.

Ubuntu, Debian, etc

Once again, check your grub version.

grub-probe -V
grub-probe (GRUB) 2.02~beta2-36ubuntu3.18

Grub2

Run the following to awk command to get your menu entries. Note that there are menuentries and sub menu entries. The submenu entries are referenced as seen below.

awk -F\' '/submenu|menuentry / {if($1=="menuentry "){print m++ "    : " $2}else \
if($1=="submenu "){print m "    : " $2}else \
{print " " m ">" s++ " : " $2}}' /boot/grub/grub.cfg
0    : Ubuntu
1>   : Advanced options for Ubuntu
 1>0 : Ubuntu, with Linux 4.4.0-1022-aws
 1>1 : Ubuntu, with Linux 4.4.0-1022-aws (recovery mode)
 1>2 : Ubuntu, with Linux 3.13.0-85-generic
 1>3 : Ubuntu, with Linux 3.13.0-85-generic (recovery mode)

In order to set the older version 3.13.0-85-generic use vi or nano to edit /etc/defaults/grub to set GRUB_DEFAULT toGRUB_DEFAULT="1>2". Or use a command like this to swap the values and confirm the change. You can read more about grub submenus here.

sudo sed -i 's/GRUB_DEFAULT=.*/GRUB_DEFAULT="1>2"/g' /etc/default/grub

Finally, run the next command to commit the changes.

sudo update-grub

Conclusion

After you’ve updated the boot configurations you can now reboot and confirm the previous kernel version you selected is loaded by running uname -r.

Next Post

Previous Post

Leave a Reply

© 2020 linux bucket

All rights reserved