How to Check the Linux Distribution and Kernel Version

How to Check the Linux Distribution and Kernel Version

Reading time1 min
#Linux#Sysadmin#LinuxVersion#Kernel#Ubuntu#RHEL

How to Check the Linux Distribution and Kernel Version

"Which Linux version is this?" can mean two different things: the distribution release (Ubuntu 24.04, RHEL 9.6, Debian 13) or the kernel version (6.8, 5.14). They come from different places, and mixing them up is a common source of wrong answers. This post covers both, plus distro-specific shortcuts and script-friendly forms.

Distribution version: /etc/os-release

Every current distribution with systemd ships /etc/os-release. It is the one command that works everywhere.

cat /etc/os-release

On Ubuntu 24.04 the interesting lines look like this:

PRETTY_NAME="Ubuntu 24.04.3 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.3 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian

On RHEL 9 you get ID="rhel", ID_LIKE="fedora" and VERSION_ID="9.6". ID identifies the distro, ID_LIKE the family it is compatible with, and VERSION_ID is the version number to compare against.

hostnamectl: OS, kernel and architecture at once

hostnamectl

On systemd systems it prints the operating system, kernel and architecture together, plus hardware vendor and model where available. It is the quickest way to answer both questions in one go when you are logged in to an unfamiliar machine.

lsb_release

lsb_release -a      # full info
lsb_release -rs     # release only, e.g. 24.04
lsb_release -cs     # codename only, e.g. noble

It is installed by default on Ubuntu and is common in Debian and Ubuntu scripts, especially lsb_release -cs in APT repository lines. It is not installed by default on RHEL-family systems or minimal container images, so do not rely on it in portable scripts. Reading /etc/os-release gives the same data with no extra package.

Distro-specific files

Older systems and some scripts still use these:

cat /etc/redhat-release      # RHEL, CentOS, AlmaLinux, Rocky, Fedora
cat /etc/system-release      # RHEL family and Amazon Linux
cat /etc/debian_version      # Debian point release, e.g. 13.1
cat /etc/lsb-release         # Ubuntu
cat /etc/issue               # login banner, often includes the release

/etc/redhat-release prints a single line such as Red Hat Enterprise Linux release 9.6 (Plow). On a system too old to have /etc/os-release (CentOS 6 and earlier), this file is the main source.

/etc/issue is a login banner, not a version database. Admins often customize it, so treat it as a hint only.

On Ubuntu, /etc/debian_version exists too, but it shows the Debian base (for example trixie/sid), not the Ubuntu version.

You can also ask the package manager which release package is installed:

rpm -q redhat-release        # RHEL
rpm -qf /etc/os-release      # any RPM distro: which package owns the file
dpkg -S /etc/os-release      # same on Debian/Ubuntu

Kernel version

uname -r          # running kernel release, e.g. 6.8.0-85-generic
uname -a          # everything: kernel, hostname, build date, architecture
uname -m          # architecture: x86_64, aarch64
cat /proc/version # kernel version plus the compiler it was built with

uname -r is the one to use. In 6.8.0-85-generic, 6.8.0 is the upstream kernel version, 85 is Ubuntu's package revision, and generic is the kernel flavor. A RHEL 9 kernel looks like 5.14.0-570.12.1.el9_6.x86_64: Red Hat keeps the 5.14 base for the whole major release and backports fixes, so the base number alone says little about which fixes you have. Compare the full string against the vendor's advisories.

Running kernel vs installed kernels

After a kernel update, the new kernel is installed but not running until you reboot. uname -r shows what is running. To see what is installed:

dpkg --list 'linux-image-*' | grep '^ii'    # Debian/Ubuntu
rpm -q kernel                               # RHEL family
ls /lib/modules                             # any distro

To check whether a reboot is pending:

cat /var/run/reboot-required                # Ubuntu/Debian, exists only if needed
sudo dnf needs-restarting -r                # RHEL family (dnf-plugins-core)

Containers and WSL

Inside a container, /etc/os-release describes the container image, but uname -r shows the host kernel, because containers share it. A Debian image on an Ubuntu host reports Debian as the OS and an Ubuntu kernel. That is expected.

Under WSL 2, the kernel string contains microsoft-standard-WSL2, and /etc/os-release shows whatever distro you installed.

In scripts

Source /etc/os-release instead of parsing lsb_release output. The file is valid shell syntax by design.

#!/usr/bin/env bash
. /etc/os-release

case "$ID" in
  ubuntu|debian)
    echo "Debian family: $PRETTY_NAME"
    ;;
  rhel|centos|almalinux|rocky|fedora)
    echo "RHEL family: $PRETTY_NAME (major ${VERSION_ID%%.*})"
    ;;
  *)
    echo "Other: $PRETTY_NAME (like: ${ID_LIKE:-none})"
    ;;
esac

echo "Kernel: $(uname -r)"

${VERSION_ID%%.*} strips everything after the first dot, so 9.6 becomes 9. Use it when you need the major version only. To avoid polluting the current shell, run it in a subshell: (. /etc/os-release; echo "$VERSION_ID").

Many machines at once

ssh user@203.0.113.10 'grep PRETTY_NAME /etc/os-release; uname -r'

ansible all -m ansible.builtin.setup \
  -a 'filter=ansible_distribution*'

Ansible facts include ansible_distribution, ansible_distribution_version and ansible_kernel, which is the easiest way to inventory a fleet.

On a desktop, the same information is under Settings, then About (or System, then About, depending on the GNOME version).

Quick reference

cat /etc/os-release              # distro name and version, any modern distro
hostnamectl                      # OS, kernel, architecture
lsb_release -a                   # Ubuntu/Debian, if installed
cat /etc/redhat-release          # RHEL family
cat /etc/debian_version          # Debian point release
uname -r                         # running kernel
uname -m                         # architecture
rpm -q kernel                    # installed kernels, RHEL family
dpkg --list 'linux-image-*'      # installed kernels, Debian/Ubuntu