How To Get Os Version In Linux

7 min read

Knowing the exact version of your Linux operating system is a fundamental skill for system administrators, developers, and everyday users alike. Whether you are troubleshooting a driver issue, checking software compatibility, applying security patches, or simply satisfying curiosity, the ability to quickly identify your distribution, release number, and kernel version saves valuable time. Because Linux exists in countless flavors—from Ubuntu and Fedora to Arch and Alpine—there is no single universal button, but there are several reliable commands and files that work across almost every environment.

This is the bit that actually matters in practice.

The Quickest Universal Method: /etc/os-release

The most reliable and modern way to get OS version in Linux is reading the /etc/os-release file. Which means this file is part of the systemd standard and is present on virtually all contemporary distributions, including those that do not use systemd as their init system. It provides a structured, machine-readable format that includes the distribution name, version ID, codename, and pretty name for display purposes.

To view it, simply use the cat command:

cat /etc/os-release

The output will look similar to this:

NAME="Ubuntu"
VERSION="22.04.3 LTS (Jammy Jellyfish)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 22.04.3 LTS"
VERSION_ID="22.04"
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 perfect for logs or UI display.
  • VERSION_ID: The numeric version (e.g., "22.04"), ideal for scripting and version comparisons.
  • ID: The lowercase distribution identifier (e.g., ubuntu, fedora, debian), crucial for writing portable shell scripts.

If you only need a specific value, you can source the file in a script or use grep combined with cut or awk. As an example, to grab just the version ID:

source /etc/os-release && echo $VERSION_ID

Using the lsb_release Command

For years, the Linux Standard Base (LSB) provided the lsb_release command as the de facto standard for querying distribution information. While it is not installed by default on minimal installations of Arch Linux, Alpine, or some RHEL-based minimal spins, it remains widely available on Ubuntu, Debian, Linux Mint, and openSUSE.

Counterintuitive, but true Simple, but easy to overlook..

Run the following to see a clean summary:

lsb_release -a

The -a flag prints all available information: Distributor ID, Description, Release number, and Codename. If you only need the description line (which is often the most readable), use -d:

lsb_release -d

Output example:

Description:    Ubuntu 22.04.3 LTS

Pros: Human-readable output, consistent flags across supported distros. Cons: Not installed everywhere; requires the lsb-release package.

The hostnamectl Command for systemd Systems

On any system running systemd (which is the vast majority of modern Linux servers and desktops), hostnamectl offers a polished overview of the system identity. It combines the OS version with the kernel version and hardware architecture in a single, clean output.

This is the bit that actually matters in practice.

Type:

hostnamectl

You will see a structured list:

   Static hostname: myserver
         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)

It's arguably the best command for a quick system audit because it answers "What OS?", "What Kernel?Plus, ", and "What Architecture? " simultaneously.

Checking Distribution-Specific Release Files

Before /etc/os-release became the standard, distributions used their own specific files in /etc/. On top of that, many of these still exist for backward compatibility. Checking these files is useful if you are on a very old system (pre-2016) or if /etc/os-release is somehow missing Still holds up..

Common files include:

  • /etc/redhat-release: Red Hat, CentOS, Fedora, Rocky, AlmaLinux.
  • /etc/arch-release: Arch Linux (usually an empty file, existence implies Arch).
  • /etc/debian_version: Debian and derivatives (often just contains the version number like "12.5").
  • /etc/SuSE-release: Older openSUSE/SLES versions. On top of that, * /etc/alpine-release: Alpine Linux. * /etc/gentoo-release: Gentoo.

You can check them all at once with a wildcard:

cat /etc/*release
cat /etc/*version

Be aware that parsing these programmatically is fragile because the format varies wildly between vendors. Always prefer /etc/os-release for scripting.

Identifying the Kernel Version vs. OS Version

A common point of confusion for beginners is the difference between the Operating System version (distribution release) and the Kernel version. On the flip side, the OS version refers to the collection of userland tools, package manager, and libraries (e. g., Ubuntu 24.04). On top of that, the Kernel version refers to the core Linux kernel managing hardware (e. g., 6.Practically speaking, 8. 0).

To check the running kernel, use uname:

uname -r

Output example: 5.15.0-91-generic

For a full kernel breakdown including architecture and compile date:

uname -a

Why this matters: You might be running Debian 12 (Bookworm) with a backported Kernel 6.5, or Ubuntu 22.04 LTS running a Kernel 5.15 (original) or 6.5 (HWE stack). Knowing both numbers is critical for driver compatibility (like NVIDIA or ZFS modules) and vulnerability assessment (e.g., Dirty Pipe, StackRot).

Getting Version Info in Scripts (Programmatic Approach)

When writing Bash, Python, or Ansible playbooks, you need structured data, not pretty text. The standard approach in Bash is sourcing /etc/os-release.

Bash Example:

#!/bin/bash
if [ -f /etc/os-release ]; then
    . /etc/os-release
    echo "Distro: $ID"
    echo "Version: $VERSION_ID"
    echo "Codename: $VERSION_CODENAME"
else
    echo "Cannot detect OS version."
    exit 1
fi

Python Example (using the distro module): The platform module in the standard library is deprecated for this purpose. The external distro package (often pre-installed or available via pip) is the industry standard.

import distro

print(f"Name: {distro.name()}")

```python
print(f"Name: {distro.name()}")
print(f"Version: {distro.version()}")
print(f"ID: {distro.id()}")
print(f"Like: {distro.id_like()}")
print(f"Codename: {distro.codename()}")

If the distro package is not available, you can fall back to parsing /etc/os-release directly in Python:

import os

def get_os_info():
    info = {}
    if os.exists("/etc/os-release"):
        with open("/etc/os-release") as f:
            for line in f:
                if "=" in line:
                    key, value = line.Now, split("=", 1)
                    # Strip surrounding quotes if present
                    if value. path.Still, strip(). startswith('"') and value.

os_info = get_os_info()
print(f"Distro: {os_info.So get('ID', 'unknown')}")
print(f"Version: {os_info. get('VERSION_ID', 'unknown')}")
print(f"Codename: {os_info.

### Alternative Command‑Line Tools

* **`lsb_release`** – part of the `lsb-release` package on many distributions; `lsb_release -a` prints distributor, description, release, and codename.  
* **`hostnamectl`** – on systems running `systemd`, `hostnamectl` includes a “Operating System” line that mirrors `/etc/os-release`.  
* **`/etc/issue`** – sometimes contains a pre‑login banner with the distro name and version, but its format is not standardized and should be avoided for reliable parsing.

### Why Structured Data Matters

Scripts that rely on brittle string matching (e.Practically speaking, , grepping for “Ubuntu” in `/etc/os-release`) can break when a distribution changes its naming scheme or when a derivative introduces a new `ID_LIKE` value. g.By using the key/value pairs defined in the `os-release` specification—or a well‑maintained library like `distro`—you guarantee compatibility across current and future releases.

### Putting It All Together

A solid version‑detection function might look like this:

```bash
get_os_info() {
    if [ -f /etc/os-release ]; then
        . /etc/os-release
        printf '%s\n' "ID=$ID" "VERSION_ID=$VERSION_ID" "VERSION_CODENAME=$VERSION_CODENAME"
    elif command -v lsb_release >/dev/null 2>&1; then
        lsb_release -a
    else
        echo "Unable to determine OS version." >&2
        return 1
    fi
}

Call get_os_info in your provisioning scripts, configuration management tasks, or diagnostic tools, and you’ll have a reliable foundation for decisions about package repositories, kernel module compatibility, or security‑patch applicability.


Conclusion
While the humble uname -r tells you which kernel is running, understanding the operating system layer requires consulting /etc/os-release (or its legacy equivalents). For interactive checks, cat /etc/*release offers a quick glance, but any automated workflow should source the structured fields provided by os-release or use a dedicated library such as Python’s distro. By distinguishing OS version from kernel version and relying on standardized data, you avoid fragile parsing, ensure cross‑distribution compatibility, and make your scripts resilient to the ever‑evolving Linux landscape Nothing fancy..

Out Now

Out Now

These Connect Well

Related Reading

Thank you for reading about How To Get 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