Check Installed Linux Kernel in Command Line | 7 Easy Steps

Posted on

Check Installed Linux Kernel in Command Line | 7 Easy Steps

Check Installed Linux Kernel in Command Line | 7 Easy Steps

In this guide, we’ll explore How to Check Installed Linux Kernel in Command Line. Discover how to display the installed Linux kernels from the command line interface (CLI).

A kernel is the fundamental core of Linux. It’s responsible for managing all major activities within the operating system. Composed of various modules, the kernel interacts directly with the underlying hardware. It provides the necessary abstraction layer, hiding the complexities of low-level hardware details from system and application programs. Understanding How to Check Installed Linux Kernel in Command Line is crucial for system administrators and developers.

You can readily use Linux commands to list your installed Linux Kernels. To achieve this, follow the steps outlined below, which demonstrate how it works across different Linux distributions including RHEL, Debian, Ubuntu, Arch Linux, etc. Let’s dive into How to Check Installed Linux Kernel in Command Line.

1. Check the Current Linux Kernel Version

If you need to determine your currently running kernel version, you can use the following command:

# uname -r
# uname -mrs

The uname command is a standard Unix command that prints system information. The -r option specifically displays the kernel release version, while -mrs provides machine architecture, kernel release, and system name.

Check the Current Linux Kernel Version

2. Find Linux Kernel on Centos / RHEL / RedHat / Fedora

On these Linux distributions, you can use the following commands to list your installed kernels using the RPM package manager:

rpm -qa kernel

The rpm -qa kernel command queries the RPM database (-qa means query all packages) and filters the results to only show packages with "kernel" in their name.

Example output:

Find Linux Kernel on Centos / RHEL / RedHat / Fedora

Alternatively, you can use yum or dnf, the package managers for these distributions, to check your installed kernels:

# yum list installed kernel
Or
# dnf list installed kernel

These commands list all installed packages and filter the output to show only those named "kernel".

You can follow this link to see the differences between YUM and DNf.

3. Find Linux Kernel on Debian / Ubuntu

If you are an Ubuntu or Debian Linux user, you can easily use the following command to list your installed kernels:

dpkg --list | grep linux-image

The dpkg --list command lists all installed Debian packages. The output is then piped to grep linux-image, which filters the list to show only packages with "linux-image" in their name. These are typically the kernel image packages.

Example Output:

Find Linux Kernel on Debian / Ubuntu

4. Find Linux Kernel on Arch Linux

If you are an Arch Linux user, to list your installed kernels, you can use the following command:

<code>pacman -Q | grep linux</code>

The pacman -Q command queries the Pacman database for all installed packages. The output is then piped to grep linux, which filters the list to show only packages with "linux" in their name.

5. Find Linux Kernel on OpenSUSE Linux

To see the installed kernels on OpenSUSE, you can use the commands below:

# rpm -qa | grep -i kernel
Or
# zypper search -i kernel

The first command, rpm -qa | grep -i kernel, is similar to the RHEL/CentOS command, querying the RPM database and filtering for packages containing "kernel" (case-insensitive). The second command, zypper search -i kernel, uses the zypper package manager to search for installed packages containing "kernel" (case-insensitive).

6. Find Installed Linux Kernels that are not in the Package Manager

You can list your kernels that aren’t in the package manager by locating them in the /lib/modules/ directory using the ls command:

ls -l /lib/modules/

The /lib/modules/ directory contains kernel modules for each installed kernel. This command lists the contents of this directory, showing a directory for each kernel version. Kernels installed outside the package manager (e.g., manually compiled kernels) will often have their modules stored here.

7. Find Custom Compiled Linux Kernel

vmlinuz is the name of the Linux kernel executable. vmlinuz is a compressed Linux kernel, and it is capable of loading the operating system into memory so that the computer becomes usable and application programs can be run.

To find the custom-compiled kernel, you can use the following command:

sudo find /boot/ -iname "vmlinuz*"

This command searches the /boot/ directory for files whose names start with "vmlinuz" (case-insensitive). The /boot/ directory typically contains the kernel images used to boot the system.

It is also the kernel file but in a non-compressed and non-bootable format. To list files in /boot/ run the ls command:

<code>ls -l /boot/</code>

The Linux kernel is compiled by running the following command:

<code>sudo make install</code>

Alternative Solutions for Checking Installed Kernels

While the methods above are effective, here are two alternative approaches for checking installed Linux kernels:

1. Using grubby (for systems using GRUB bootloader):

grubby is a command-line tool for managing GRUB bootloader configurations. It can be used to list all available kernels that GRUB knows about, including those that might not be actively managed by the package manager. This is particularly useful when dealing with custom kernels or kernels installed outside the standard package management system.

Explanation:

grubby reads the GRUB configuration files (typically located in /boot/grub2/grub.cfg or similar) and extracts information about the kernels that are available for booting. It provides a convenient way to list these kernels along with their associated boot parameters.

Code Example:

grubby --info=ALL

This command will output a detailed list of all kernels configured in GRUB, including their kernel version, initrd image, and boot parameters. The output will show the ‘index’ number for each, the ‘kernel’ line indicates the vmlinuz path.

2. Examining /proc/cmdline at Boot Time:

While /proc/cmdline reflects the currently running kernel’s command-line arguments, it can be combined with other tools to infer available kernels. By examining the filesystem referenced in the root= argument of /proc/cmdline, you can then list the kernels available in that root filesystem’s /boot directory.

Explanation:

This method leverages the fact that the root= argument in /proc/cmdline specifies the root filesystem used by the currently running kernel. By mounting that filesystem (if it’s not already mounted) and then listing the contents of its /boot directory, you can identify the kernel images available for booting. This is particularly useful in multi-boot environments or when investigating kernels on a system with multiple root filesystems.

Code Example:

# Get the root filesystem from /proc/cmdline
ROOTFS=$(cat /proc/cmdline | awk '{for(i=1;i<=NF;i++) {if($i ~ /^root=/){print substr($i,6)}}}')

# Check if the root filesystem is already mounted
if ! mount | grep -q "$ROOTFS"; then
  # Create a mount point
  mkdir -p /mnt/temp_root

  # Mount the root filesystem
  mount "$ROOTFS" /mnt/temp_root

  # List the kernels in the /boot directory
  ls -l /mnt/temp_root/boot/vmlinuz*

  # Unmount the filesystem
  umount /mnt/temp_root
  rmdir /mnt/temp_root
else
  # The root filesystem is already mounted, likely as /
  ls -l /boot/vmlinuz*
fi

This script first extracts the root filesystem from /proc/cmdline. Then, it checks if that filesystem is already mounted. If not, it creates a temporary mount point, mounts the filesystem, lists the kernel images in the /boot directory, and then unmounts the filesystem and removes the mount point. If the filesystem is already mounted (likely as /), it simply lists the kernel images in the /boot directory. This provides a more robust approach to finding kernels even if they are not directly managed by package managers or bootloader configurations.

Conclusion

At this point, you have learned How to Check Installed Linux Kernel in Command Line. Finding installed Linux kernels helps manage system stability, security, and compatibility. It allows updating, troubleshooting, or removing old kernels as needed. Using the methods described, including grubby and examining /proc/cmdline, you can reliably identify the available kernels on your Linux system.

Hope you enjoy it. Please subscribe to us on Facebook, Instagram, and YouTube.

You may also like to read these articles:

How To Upgrade Linux Kernel on Centos 7

Upgrade Linux Kernel on Rocky Linux 8

Upgrade Linux Kernel on Ubuntu 22.04

Linux kernel 6.14 Features and Release Date

Remove Old Linux Kernels on AlmaLinux

Leave a Reply

Your email address will not be published. Required fields are marked *