Get Linux Version From Command Line

7 min read

Introduction

Learning how to get Linux version from command line is a fundamental skill for every user, from beginners exploring the OS to seasoned administrators managing complex server environments. The command line offers a fast, scriptable, and precise way to retrieve distribution details, kernel information, and release identifiers without relying on graphical tools. In this guide, you will discover the most reliable commands, understand the underlying data they return, and explore common troubleshooting scenarios. By the end of the article, you will be able to confidently run commands such as uname -a, lsb_release -a, and custom scripts that parse version information for automation or documentation purposes.

Steps to Get Linux Version from Command Line

1. Use uname for Kernel and System Information

The uname command is the most universal tool available on virtually every Unix‑like system, including Linux.

uname -a
  • -a displays all information in one line: Linux (kernel name), hostname (network node name), kernel-release (kernel version), kernel-version (full kernel version), machine (hardware architecture), and processor (CPU type).
  • Example output: Linux server01 5.15.0‑90‑generic #100‑Ubuntu SMP Tue Nov 14 15:20:00 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux.

This command is ideal when you need a quick overview of the kernel and hardware compatibility No workaround needed..

2. Retrieve Distribution‑Specific Details with lsb_release

If you need details about the specific Linux distribution (e.g., Ubuntu, Debian, CentOS), the lsb_release command is the standard choice Surprisingly effective..

lsb_release -a
  • -a prints all fields: Distributor ID, Description, Release, Codename, and Build Number.
  • Typical output for Ubuntu 22.04:
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.3 LTS
Release:        22.04
Codename:       jammy

Note: lsb_release is part of the LSB (Linux Standard Base) utilities and may not be installed by default on minimal systems. On Debian‑based distributions, you can install it with sudo apt-get install lsb-release; on Red Hat‑based systems, use sudo yum install redhat-lsb or sudo dnf install redhat-lsb Not complicated — just consistent. Surprisingly effective..

3. Fallback to /etc/os-release or /etc/issue

When lsb_release is missing, the version information is still stored in plain‑text files Not complicated — just consistent. Simple as that..

cat /etc/os-release
  • This file contains key‑value pairs such as NAME="Ubuntu", VERSION="22.04 (Jammy Jellyfish)", ID=ubuntu, and VERSION_ID="22.04".
  • For a quick one‑liner that mimics lsb_release -a, you can use:
awk -F= '/^ID=/ {dist=$2} /^VERSION_ID=/ {ver=$2} END {print dist" "ver}' /etc/os-release
  • Older systems may store info in /etc/issue or /etc/issue.net. Example:
cat /etc/issue

4. Combine Information with Custom Scripts

For automation, you can write a small script that gathers multiple sources and formats the output. Below is a Bash snippet that prints a concise version string:

#!/bin/bash
# Get Linux version from command line – combined script
KERNEL=$(uname -r)
DISTRO=$(awk -F= '/^NAME=/ {gsub(/"/,"",$2); print $2}' /etc/os-release)
VERSION=$(awk -F= '/^VERSION_ID=/ {gsub(/"/,"",$2); print $2}' /etc/os-release)
echo "$DISTRO $VERSION (Kernel $KERNEL)"

Running this script on an Ubuntu system yields something like: Ubuntu 22.04 (Kernel 5.15.0‑90‑generic).

5. Verify and Troubleshoot

  • Command not found: If lsb_release is missing, install the appropriate package as described above.
  • Empty output: Some minimal containers strip /etc/os-release. In such cases, uname -a remains reliable.
  • Inconsistent data: Different distributions may store version info in slightly different formats; always cross‑check with cat /etc/os-release when precision is required.

Scientific Explanation

How Command‑Line Tools Retrieve Version Information

At the core of these utilities lies the Linux kernel’s procfs and sysfs filesystems, which expose system metadata without requiring direct hardware access.

  • uname is a direct wrapper around the kernel’s uname() system call. This call returns static information compiled into the kernel at boot time, such as the kernel release string and machine architecture. Because it is a kernel function, uname works even when user‑space utilities are not fully installed Still holds up..

  • lsb_release reads the standardized files defined by the Linux Standard Base, primarily /etc/lsb-release (generated at distribution build time) and /etc/os-release. These files are plain text, making them easy to parse with simple shell commands.

  • The /etc/os-release file follows the freedesktop.org specification (often referred to as the os-release specification). It provides a machine‑readable key‑value format that is both human‑friendly and script‑friendly. The specification ensures consistent naming across distributions, which is why tools like lsb_release and graphical “About” dialogs can reliably display the same information.

Understanding these mechanisms helps administrators diagnose why a particular command might fail (e.g., missing LSB utilities) and decide which source of truth is most appropriate for their use case.

FAQ

Q1: What is the difference between uname -a and lsb_release -a?
A1: uname -a reports kernel‑level

kernel‑level details such as the kernel release, build information, hostname, and hardware architecture. Plus, it tells you nothing about the Linux distribution (Ubuntu, Fedora, Debian, etc. ) or its user‑space release version. lsb_release -a, on the other hand, queries the LSB and os-release files to report the distributor ID, description, release number, codename, and LSB compatibility version. In short: uname answers “which kernel am I running?” while lsb_release answers “which distribution is installed?

Q2: Which command is safest for automation scripts?
A2: Parsing /etc/os-release directly is usually the most dependable choice. It follows the freedesktop.org standard, exists on nearly all modern distributions, and does not require an external package such as lsb_release. Combine it with uname -r if you need both distribution and kernel details. Avoid relying solely on lsb_release in minimal containers or embedded images where the LSB package may be absent.

Q3: Why does my container show little or no version information?
A3: Containers

Containers often share the host’s kernel, so uname reflects the host kernel version, not a container‑specific one. , ubuntu:22.g.So in such environments, the only reliable identifiers are the image tag or digest used at pull time (e. Meanwhile, the container image may be a minimal “distroless” or “scratch” build that omits /etc/os-release, /etc/lsb-release, and the lsb_release binary entirely. 04 or sha256:…) and any labels baked into the image manifest via LABEL instructions in the Dockerfile.

Q4: How do I programmatically determine the distribution ID and version in a shell script?
A4: Source /etc/os-release and read the standardized variables:

if [ -f /etc/os-release ]; then
    . /etc/os-release
    echo "Distro: $ID"
    echo "Version: $VERSION_ID"
    echo "Pretty Name: $PRETTY_NAME"
else
    echo "os-release not found; falling back to uname"
    uname -srm
fi

This approach requires no external dependencies, works in POSIX‑compliant shells, and respects the freedesktop.org specification. Avoid parsing lsb_release output or grepping /etc/*release files with ad‑hoc regexes, as formats vary across distributions and releases.

Q5: Can I rely on /proc/version for kernel details?
A5: /proc/version exposes the same kernel build string returned by uname -v (compiler version, build user, timestamp, and config flags). It is useful for quick inspection via cat /proc/version, but for scripting, uname -r (kernel release) and uname -v (kernel version/build info) are the stable, documented interfaces. The procfs entry is primarily a human‑readable convenience.


Conclusion

Navigating Linux version information requires distinguishing between kernel identity and distribution identity—two orthogonal layers that are often conflated. In practice, the kernel, queried via uname or /proc, tells you what is running at the hardware abstraction level. The distribution metadata, standardized in /etc/os-release and optionally exposed by lsb_release, tells you who packaged the user‑space environment you are interacting with That's the whole idea..

For strong automation, default to sourcing /etc/os-release; it is ubiquitous, standardized, and dependency‑free. Reserve uname for kernel‑specific logic (module compatibility, hardware capability checks) and treat lsb_release as a convenient interactive wrapper rather than a scripting dependency. In containerized or minimal environments, remember that the “version” is increasingly defined by the image reference and labels rather than by files inside the filesystem Small thing, real impact..

By understanding the provenance and scope of each source, you eliminate guesswork, write portable scripts, and diagnose environment mismatches with confidence.

Brand New Today

Just In

Cut from the Same Cloth

Similar Stories

Thank you for reading about Get Linux Version From Command Line. 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