How To Get Os Version In Linux

How To Get Os Version In Linux

Reading time1 min
#Linux#Sysadmin#CommandLine#os-release#lsb_release

Mastering Linux OS Version Detection: Command-Line Techniques That Go Beyond Basics

Forget just running uname -a—discover the nuanced methods to extract precise OS versions across diverse Linux distributions, empowering sysadmins and developers to tailor their approach with surgical precision.


Accurately identifying the Linux OS version is critical for system troubleshooting, compatibility checks, and security assessments. When you need to ensure your software or script behaves correctly across different environments, a deeper understanding of how to detect the distro version can save hours of guesswork or outages.

In this post, we’ll move beyond the typical uname command and explore practical, reliable ways to get exact OS version details from the command line, covering mainstream and lesser-known distros alike.

Why Basic Commands Aren’t Enough

The infamous (and extremely common) command:

uname -a

provides kernel info but not detailed distribution or version info:

Linux myhost 5.11.0-27-generic #29-Ubuntu SMP Fri Jul 9 23:20:15 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
  • It shows the kernel version (5.11.0-27-generic) but not if this is Ubuntu 20.04 LTS or Debian 10.
  • For many real-world scripts and system maintenance, knowing the exact distribution and version is crucial.

Practical Commands to Detect Linux OS Version

1) /etc/os-release: The Modern Standard

Most recent Linux distros include a standardized /etc/os-release file containing detailed info like name, ID, and version.

cat /etc/os-release

Sample output on Ubuntu 20.04:

NAME="Ubuntu"
VERSION="20.04.4 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.4 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
...

Recommended way to extract just the version:

source /etc/os-release
echo "$NAME $VERSION"
# Output: Ubuntu 20.04.4 LTS (Focal Fossa)

This file is usually present in:

  • Ubuntu
  • Debian (since Jessie)
  • Fedora
  • CentOS Stream 8+
  • Arch derivatives (with slightly varying contents)

2) lsb_release Command: Distribution Information Utility

If installed, lsb_release offers standardized distro detection.

lsb_release -a

Example output:

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.4 LTS
Release:        20.04
Codename:       focal

If lsb_release isn’t installed, you can add it on Debian/Ubuntu with:

sudo apt-get install lsb-release -y

or on CentOS/Fedora:

sudo dnf install redhat-lsb-core -y   # Fedora/CentOS 8+
sudo yum install redhat-lsb-core -y   # CentOS 7/

3) Distribution-Specific Files for Legacy Systems

Older distros sometimes have distro-specific release files in /etc/.

Some common ones:

FileNotes
/etc/redhat-releaseRed Hat Enterprise Linux / CentOS
/etc/centos-releaseCentOS-specific
/etc/debian_versionDebian
/etc/SuSE-releaseopenSUSE/SUSE

Example reading Red Hat/CentOS release info:

cat /etc/redhat-release
# CentOS Linux release 7.9.2009 (Core)

When /etc/os-release is missing or incomplete, these files help fill in gaps.


4) Using hostnamectl for System Info

On systems running systemd (most modern distros), hostnamectl can display some OS info:

hostnamectl | grep "Operating System"
# Operating System: Ubuntu 20.04 LTS

Summarizing Checks in a Script

Here’s a basic bash snippet that reliably outputs the Linux distribution name and version regardless of variation across systems:

#!/bin/bash

if [ -f /etc/os-release ]; then
    source /etc/os-release
    echo "$NAME $VERSION"
elif command -v lsb_release >/dev/null 2>&1; then
    lsb_release -d | cut -f2-
elif [ -f /etc/centos-release ]; then 
    cat /etc/centos-release 
elif [ -f /etc/redhat-release ]; then 
    cat /etc/redhat-release 
elif [ -f /etc/debian_version ]; then 
    echo "Debian $(cat /etc/debian_version)"
else 
    echo "Unknown Linux distribution"
fi

Simply save this as detect_linux_version.sh, give it execute permission (chmod +x detect_linux_version.sh), and run it anytime you need precise distro details.


Conclusion

Mastering Linux OS version detection goes beyond just knowing your kernel number from uname. Using standardized files like /etc/os-release, utilities like lsb_release, or checking legacy files ensures you gather accurate distro/version data vital for automation scripts, troubleshooting pipelines, or compliance systems.

In rapidly evolving environments with countless Linux flavors, nurturing your OS detection toolkit will empower better decisions—whether you're managing hundreds of servers or coding platform-aware applications.

Next time you’re debugging a script that fails mysteriously on some machines, or prepping software dependencies — don’t rely on guesswork! Go deep with these commands and achieve surgical precision in your Linux system knowledge.


Happy sysadmin-ing!


If you enjoyed this deep dive or want more practical Linux tricks delivered periodically, subscribe below or drop your comments!