Linux Kernel Update
The Linux kernel is the core of the system. Regular updates provide security vulnerability fixes and new hardware support. On Ubuntu, you can update the kernel with apt upgrade; on RHEL / AlmaLinux systems, use dnf update kernel. After updating, use the needs-restarting command to determine whether a reboot is required. You can also remove old kernels that are no longer needed with apt autoremove or dnf remove to free up disk space.
Syntax
# ===============================================
# Ubuntu / Debian
# ===============================================
# -----------------------------------------------
# Update the package list
# -----------------------------------------------
# apt update
# → Refreshes the repository package list
# → Always run this before updating the kernel
# Example: sudo apt update
# -----------------------------------------------
# Update all packages including the kernel
# -----------------------------------------------
# apt upgrade
# → Updates all installed packages (including the kernel)
# → Does not remove any existing packages
# apt full-upgrade
# → Updates with permission to remove existing packages when dependencies change
# → Use this for major kernel version upgrades
# -----------------------------------------------
# Check whether a reboot is needed (Ubuntu)
# -----------------------------------------------
# needs-restarting
# → Shows processes that need restarting after an update, and whether a kernel reboot is required
# → Included in the dnf-utils package (the yum-utils equivalent is not needed on Ubuntu)
# → On Ubuntu, check /var/run/reboot-required to determine if a reboot is needed
# Example: cat /var/run/reboot-required
# -----------------------------------------------
# Remove old kernels (Ubuntu)
# -----------------------------------------------
# apt autoremove
# → Removes all old kernel packages that are no longer needed
# → Reboot and confirm the new kernel is running before executing this
# Example: sudo apt autoremove
# dpkg --list 'linux-image-*'
# → Lists all installed kernel image packages
# → Use this to check the currently running kernel before removing old ones
# ===============================================
# RHEL / AlmaLinux / CentOS Stream
# ===============================================
# -----------------------------------------------
# Update the kernel
# -----------------------------------------------
# dnf update kernel
# → Updates only the kernel package
# Example: sudo dnf update kernel
# dnf update
# → Updates the entire system (including the kernel)
# Example: sudo dnf update
# -----------------------------------------------
# Check whether a reboot is needed (RHEL)
# -----------------------------------------------
# needs-restarting -r
# → Determines whether a full system reboot is required
# → An exit code of 1 means a reboot is needed
# Example: sudo needs-restarting -r
# needs-restarting -r -v
# → Shows the reason why a reboot is required in detail
# Example: sudo needs-restarting -r -v
# needs-restarting -s
# → Lists services that need to be restarted
# Example: sudo needs-restarting -s
# -----------------------------------------------
# Check the default kernel
# -----------------------------------------------
# grubby --default-kernel
# → Shows the path to the default kernel used on the next boot
# Example: grubby --default-kernel
# grubby --default-index
# → Shows the GRUB entry number of the default kernel
# -----------------------------------------------
# Remove old kernels (RHEL)
# -----------------------------------------------
# dnf remove kernel-{version}
# → Removes the specified old kernel
# → Check the currently running kernel with uname -r before removing
# Example: sudo dnf remove kernel-5.14.0-362.8.1.el9_3.x86_64
# dnf autoremove
# → Removes all unnecessary kernel-related packages at once
Command Reference
| Command | Description |
|---|---|
uname -r | Displays the currently running kernel version. Use this to compare versions before and after an update. |
apt update && apt upgrade | On Ubuntu, refreshes the package list and then updates all packages including the kernel. |
apt full-upgrade | Performs a full system update on Ubuntu with permission to remove existing packages. Use this for major version upgrades. |
apt autoremove | Removes old kernel packages from Ubuntu that are no longer needed after a reboot. |
dpkg --list 'linux-image-*' | Lists all installed kernel image packages on Ubuntu. Use this to verify which kernels are installed before removing old ones. |
cat /var/run/reboot-required | Checks whether a reboot is required after a kernel update on Ubuntu. If the file exists, a reboot is needed. |
dnf update kernel | Updates only the kernel package on RHEL-based systems. |
dnf update | Updates the entire system (including the kernel) on RHEL-based systems. |
dnf autoremove | Removes all unnecessary old kernel-related packages on RHEL-based systems. |
needs-restarting -r | Determines whether a full system reboot is required on RHEL-based systems. An exit code of 1 means a reboot is needed. |
needs-restarting -r -v | Displays detailed information about why a reboot is required. Shows which package updates are requiring the reboot. |
needs-restarting -s | Lists services that need to be restarted. You may be able to restart individual services instead of rebooting the whole system. |
grubby --default-kernel | Displays the path to the default kernel used on the next boot. Use this to confirm the correct kernel is set after an update. |
grubby --default-index | Displays the GRUB entry number of the default kernel. |
reboot | Reboots the system to activate the new kernel. You can also use shutdown -r now for the same effect. |
Examples
Updating the kernel on Ubuntu
# ----------------------------------------------- # Steps to update the kernel on Ubuntu # ----------------------------------------------- # Check the kernel version before updating uname -r # Refresh the package list sudo apt update # Update all packages including the kernel sudo apt upgrade # Check whether a reboot is required # If the file exists, a reboot is needed cat /var/run/reboot-required # View details about which packages require a reboot cat /var/run/reboot-required.pkgs # Reboot the system sudo reboot
Run the following command:
$ uname -r 6.5.0-26-generic $ sudo apt update && sudo apt upgrade -y (package updates are applied) $ cat /var/run/reboot-required *** System restart required *** $ sudo reboot (after reboot) $ uname -r 6.5.0-28-generic
The following example demonstrates this:
# ----------------------------------------------- # Remove old kernels after rebooting # ----------------------------------------------- # Check the currently running kernel (do not remove this version) uname -r # List all installed kernel packages dpkg --list 'linux-image-*' # Automatically remove old kernels that are no longer needed sudo apt autoremove
Run the following command:
$ uname -r 6.5.0-28-generic $ dpkg --list 'linux-image-*' Desired=Unknown/Install/Remove/Purge/Hold | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad) ||/ Name Version Architecture Description +++-============================-===================-============-============================ ii linux-image-6.5.0-26-generic 6.5.0-26.26 amd64 Linux kernel image for 6.5.0 ii linux-image-6.5.0-28-generic 6.5.0-28.28 amd64 Linux kernel image for 6.5.0 $ sudo apt autoremove (old kernels such as linux-image-6.5.0-26-generic are removed)
Updating the kernel on RHEL / AlmaLinux
# ----------------------------------------------- # Steps to update the kernel on RHEL-based systems # ----------------------------------------------- # Check the kernel version before updating uname -r # Update only the kernel package (use dnf update to update the whole system) sudo dnf update kernel # Check the default kernel for the next boot grubby --default-kernel # Reboot the system to activate the new kernel sudo reboot
Running these commands produces the following output:
$ uname -r 5.14.0-362.8.1.el9_3.x86_64 $ sudo dnf update kernel Dependencies resolved. ======================================================================== Package Arch Version Repository Size ======================================================================== Installing: kernel x86_64 5.14.0-427.13.1.el9_4 baseos 8.4 M kernel-core x86_64 5.14.0-427.13.1.el9_4 baseos 42 M kernel-modules x86_64 5.14.0-427.13.1.el9_4 baseos 36 M (output truncated) Complete! $ grubby --default-kernel /boot/vmlinuz-5.14.0-427.13.1.el9_4.x86_64 $ sudo reboot (after reboot) $ uname -r 5.14.0-427.13.1.el9_4.x86_64
The following example demonstrates this:
# ----------------------------------------------- # Check reboot status and remove old kernels (RHEL) # ----------------------------------------------- # Install the needs-restarting command # (included in the dnf-utils package) sudo dnf install dnf-utils # Determine whether a full system reboot is required # An exit code of 1 means a reboot is needed sudo needs-restarting -r # Show detailed reasons why a reboot is required sudo needs-restarting -r -v # List services that need to be restarted # You may be able to restart only services and avoid a full system reboot sudo needs-restarting -s
Run the following command:
$ sudo needs-restarting -r -v Core libraries or services have been updated since boot-up: * kernel Reboot is required to fully utilize these updates. $ sudo needs-restarting -s auditd.service NetworkManager.service sshd.service
The following example demonstrates this:
# ----------------------------------------------- # Remove old kernels after rebooting (RHEL) # ----------------------------------------------- # Check the currently running kernel uname -r # List installed kernel packages dnf list installed kernel # Remove the specified old kernel # Do not remove the currently running kernel shown by uname -r sudo dnf remove kernel-5.14.0-362.8.1.el9_3.x86_64 # Or use autoremove to remove unnecessary packages all at once sudo dnf autoremove
Run the following command:
$ uname -r 5.14.0-427.13.1.el9_4.x86_64 $ dnf list installed kernel Installed Packages kernel.x86_64 5.14.0-362.8.1.el9_3 @baseos kernel.x86_64 5.14.0-427.13.1.el9_4 @baseos $ sudo dnf remove kernel-5.14.0-362.8.1.el9_3.x86_64 Dependencies resolved. ======================================================================== Package Arch Version Repository Size ======================================================================== Removing: kernel x86_64 5.14.0-362.8.1.el9_3 @baseos 0 kernel-core x86_64 5.14.0-362.8.1.el9_3 @baseos 68 M (output truncated) Complete!
Overview
Updating the kernel is an essential task for maintaining system security. On Ubuntu-based systems, use apt to regularly update all packages including the kernel. After updating, check for the presence of /var/run/reboot-required to determine whether a reboot is needed. On RHEL-based systems, use dnf to update the kernel, and use the needs-restarting command to check whether a reboot is required. Running needs-restarting -s gives you a list of services that need restarting, which may allow you to avoid a full system reboot. Because updating the kernel leaves old kernels in place, you can save disk space by removing old kernels after confirming the system has booted successfully on the new one. For starting and stopping services, see the systemctl page as well.
If you find any errors or copyright issues, please contact us.