I have updated this answer by @xiaojueguan.
Changes in comparison with that answer:
- add a variable to define the kernel version to use;
- use
curl instead of wget;
- get the oldest kernel version Grub entry name programmatically;
- update the Grub config programmatically;
- disable upgrading the Linux kernel packages.
# Choose kernel version to install
kernel_version='6.5.0'
Install the old kernel
curl -sL https://raw.githubusercontent.com/pimlie/ubuntu-mainline-kernel.sh/master/ubuntu-mainline-kernel.sh | bash -s -- -i "v$kernel_version"
Get the Grub entry name
Note: Do verify the value of grub_entry_name. The following command gets
the oldest kernel version Grub entry name that is not a recovery mode.
Example value: Ubuntu, with Linux 6.5.0-45-generic
grub_entry_name="$(grep -Po "menuentry '\KUbuntu, [^(']+" /boot/grub/grub.cfg | sort -V | head -1)"
Update grub
sed -i "s/^\sGRUB_DEFAULT=.$/GRUB_DEFAULT='Advanced options for Ubuntu>$grub_entry_name'/" /etc/default/grub
update-grub
Disable the kernel package upgrade
apt-mark hold $(dpkg --get-selections | grep -Po "^linux[^\t]+${grub_entry_name##* }")
Reboot system
reboot
Check the currently booted Linux kernel version
uname -r