How To View Os Version In Linux

8 min read

Knowing exactly which Linux distribution and kernel you are running is a fundamental skill for any system administrator, developer, or enthusiast. Day to day, whether you are troubleshooting a driver issue, checking software compatibility, or applying security patches, the ability to quickly and accurately view OS version in Linux saves valuable time and prevents configuration errors. Because Linux exists in dozens of flavors—from Ubuntu and Debian to RHEL, Arch, and Alpine—there is no single universal command that works identically across every environment. This guide covers the most reliable methods to identify your distribution name, version number, and kernel release, ensuring you have the right information regardless of the system you are managing And it works..

The Standard Modern Method: /etc/os-release

If you are using a relatively modern Linux distribution (essentially anything using systemd, which includes Ubuntu 16.But 04+, Debian 8+, CentOS/RHEL 7+, Fedora, Arch, and openSUSE), the definitive source for operating system identification is the /etc/os-release file. That said, this file is part of the freedesktop. org specification and provides a standardized, machine-parseable format That's the whole idea..

To view the contents, simply use the cat command:

cat /etc/os-release

You will see output similar to this:

PRETTY_NAME="Ubuntu 22.04.3 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.3 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy

Key fields to note:

  • PRETTY_NAME: The human-readable string ideal for display purposes.
  • NAME: The distribution ID (e.g., "Ubuntu", "Fedora", "Debian").
  • VERSION_ID: The numeric version (e.g., "22.04", "11", "39"). This is the most reliable field for scripting logic.
  • ID_LIKE: Indicates the upstream lineage (e.g., debian, rhel, fedora). This helps determine package management compatibility (apt vs. dnf/yum).

For scripting, you can source this file directly in Bash to access variables like $VERSION_ID and $ID without parsing text output manually.

The Universal Tool: hostnamectl

The hostnamectl command is part of systemd and provides a clean, human-readable summary of the system identity. It queries the same data source as /etc/os-release but formats it beautifully for the terminal Easy to understand, harder to ignore..

Run the command without arguments:

hostnamectl

Sample output:

   Static hostname: web-server-01
         Icon name: computer-vm
           Chassis: vm
        Machine ID: a1b2c3d4e5f6...
           Boot ID: f6e5d4c3b2a1...
    Virtualization: kvm
  Operating System: Ubuntu 22.04.3 LTS
            Kernel: Linux 5.15.0-91-generic
      Architecture: x86-64
 Hardware Vendor: QEMU
  Hardware Model: Standard PC (Q35 + ICH9, 2009)
Firmware Version: 1.16.2-debian-1.16.2-1

This is arguably the fastest way to check Linux version details because it displays the Operating System (distro + version) and the Kernel version side-by-side. It also reveals virtualization details and hardware architecture, which are crucial for cloud environments.

Checking the Kernel Version Specifically

Sometimes you only need the Linux kernel version—perhaps to verify if a specific kernel module is supported or to check for a known vulnerability (CVE). The uname command is the classic tool for this The details matter here. Took long enough..

Basic Kernel Release

uname -r

Output example: 5.15.0-91-generic

Full Kernel Details

uname -a

This prints all information: kernel name, network node hostname, kernel release, kernel version, machine hardware name, processor type, hardware platform, and operating system.

Using /proc/version

For a raw look at the kernel build information, including the GCC version used to compile it and the build timestamp:

cat /proc/version

Output example: Linux version 5.15.0-91-generic (buildd@lcy02-amd64-063) (gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #101-Ubuntu SMP Tue Nov 14 13:32:19 UTC 2023

Distribution-Specific Version Files (Legacy & Minimal Systems)

Before the standardization of /etc/os-release, almost every distribution maintained its own version file in /etc/. Now, you will still encounter these on older systems (like CentOS 6, Ubuntu 14. 04) or minimal container images (like Alpine Linux or scratch Docker images) where systemd and the os-release package might not be installed Worth keeping that in mind. Less friction, more output..

Here are the common files you should check if the standard methods fail:

Distribution Family File Path Example Command
Red Hat / CentOS / Fedora / Rocky / Alma /etc/redhat-release cat /etc/redhat-release
Debian / Ubuntu / Mint /etc/debian_version cat /etc/debian_version
Alpine Linux /etc/alpine-release cat /etc/alpine-release
Arch Linux /etc/arch-release ls /etc/arch-release (usually empty, existence implies Arch)
Gentoo /etc/gentoo-release cat /etc/gentoo-release
openSUSE / SLES /etc/SuSE-release cat /etc/SuSE-release
Slackware /etc/slackware-version cat /etc/slackware-version

Pro Tip: You can list all version-related files in /etc/ at once using a wildcard:

cat /etc/*-release /etc/*_version /etc/*version 2>/dev/null

This command suppresses "Permission denied" or "No such file" errors and dumps whatever identification files exist.

Using lsb_release (The LSB Standard)

The Linux Standard Base (LSB) project created the lsb_release command to provide a consistent interface across distributions. While it is not installed by default on minimal installations of RHEL/CentOS or Arch, it is standard on Ubuntu, Debian, Linux Mint, and openSUSE Surprisingly effective..

To see all available information:

lsb_release -a

Output:

No LSB modules are available.
Worth adding: distributor ID: Ubuntu
Description:    Ubuntu 22. 04.3 LTS
Release:        22.

To get just the description (useful for

scripts or MOTD banners) use the `-d` flag:
```bash
lsb_release -d

Output:

Description:    Ubuntu 22.04.3 LTS

Other useful flags include -i (Distributor ID), -r (Release number), -c (Codename), and -s (Short output for all fields, ideal for parsing). If lsb_release is missing, it can usually be installed via the lsb-release package (Debian/Ubuntu), redhat-lsb (RHEL/CentOS/Fedora), or lsb-release (Arch/openSUSE) Turns out it matters..

Using hostnamectl (The systemd Standard)

On any modern distribution running systemd (which includes RHEL 7+, CentOS 7+, Fedora, Ubuntu 16.Plus, 04+, Debian 8+, Arch, and openSUSE), hostnamectl provides a clean, structured output that combines OS identification with kernel and hardware details. This is often the single best command for a comprehensive overview Not complicated — just consistent..

Real talk — this step gets skipped all the time.

hostnamectl

Output:

   Static hostname: web-server-01
         Icon name: computer-vm
           Chassis: vm
        Machine ID: a1b2c3d4e5f67890
           Boot ID: f0e1d2c3b4a59687
    Virtualization: kvm
  Operating System: Ubuntu 22.04.Because of that, 3 LTS
            Kernel: Linux 5. Still, 15. 0-91-generic
      Architecture: x86-64
 Hardware Vendor: QEMU
  Hardware Model: Standard PC (i440FX + PIIX, 1996)

Note the specific fields: Operating System (parsed from /etc/os-release), Kernel (from uname), and Architecture. You can query specific fields using --property (e.g., hostnamectl --property=Operating-System) for scripting Most people skip this — try not to..

Checking Within Containers and Specialized Environments

Docker / Podman Containers

Inside a container, /etc/os-release remains the most reliable source. Still, the kernel version (uname -r) will reflect the host's kernel, not the container's userland. If you are inside a minimal "scratch" or distroless image, even cat or bash might be missing. In those cases, you must inspect the image metadata from the host:

# From the host
docker image inspect  --format '{{.Config.Labels}}'
# Or check the Dockerfile history
docker history 

Windows Subsystem for Linux (WSL)

WSL 1 and WSL 2 both report the Linux kernel version via uname -r, but the "Operating System" in hostnamectl or /etc/os-release reflects the installed Linux distribution (Ubuntu, Debian, Alpine, etc.), not Windows. To detect WSL specifically, check for the Microsoft kernel tag:

uname -r | grep -i microsoft
# Or check the proc filesystem
grep -i microsoft /proc/version

Programmatic Detection (For Scripts & Automation)

When writing portable shell scripts, Ansible playbooks, or Go/Rust/Python tools, avoid parsing human-readable output. Use these structured approaches:

Shell (Portable /etc/os-release Parsing)

The os-release file is designed to be sourced. This is the POSIX-compliant way to get variables like $ID, $VERSION_ID, $PRETTY_NAME, and $ID_LIKE:

# Source the file to import variables into current shell
. /etc/os-release
echo "Distro: $ID"
echo "Version: $VERSION_ID"
echo "Pretty Name: $PRETTY_NAME"
echo "Upstream/Like: $ID_LIKE"  # e.g., "debian", "rhel fedora", "arch"

Python (Standard Library)

Python 3.10+ includes platform.freedesktop_os_release() which parses /etc/os-release safely.

import platform
info = platform.freedesktop_os_release()
print(f"ID: {info.get('ID')}")
print(f"Version: {info.get('VERSION_ID')}")
print(f"Name: {info.get('PRETTY_NAME')}")

For older Python versions, the distro package (pip install distro) is the de-facto standard replacement for the deprecated platform.linux_distribution() And that's really what it comes down to..

Go (Golang)

Use the github.com/knqyf263/go-osrelease or github.com/moby/sys/mountinfo libraries, or simply parse the key-value pairs manually, as the format is strictly defined Worth keeping that in mind..

Ansible

Ansible gathers this automatically as Facts (ansible_facts.distribution, ansible_facts.distribution_version, ansible_facts.os_family). No extra commands are needed

The Takeaway: A Layered Approach

The journey to accurately identify a Linux distribution is less about a single, universal command and more about understanding the layers of abstraction between your software and the operating system kernel. The most strong strategy is a layered one Not complicated — just consistent..

For the vast majority of cases, /etc/os-release is your primary and preferred source of truth. Its standardized format makes it simple to parse programmatically and provides the most accurate information about the userland distribution. Always start here.

Even so, a truly resilient tool or script must be aware of the environment it's running in. This means having fallbacks for when /etc/os-release is missing (like in a minimal container) and understanding that certain contexts, like containers and WSL, present unique challenges. In these cases, combining clues from the kernel version, filesystem inspection, and image metadata is necessary.

When all is said and done, the goal is to move beyond fragile, human-oriented commands and embrace structured, machine-readable data. By leveraging the methods outlined here—from sourcing os-release in a shell script to using dedicated libraries in Python or Go—you can build automation that is not only portable but also intelligently aware of its environment. This ensures your software behaves correctly, whether it's running on a traditional server, inside a Docker container, or within the Windows Subsystem for Linux That's the part that actually makes a difference..

Just Went Up

Just Landed

If You're Into This

You Might Also Like

Thank you for reading about How To View Os Version In Linux. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home