Best way to find the OS name and version on a Unix/Linux platform
Categories:
Mastering OS Identification: Finding Unix/Linux Name and Version

Discover the most effective commands and methods to accurately identify the operating system name and version across various Unix and Linux distributions.
Identifying the operating system name and version is a fundamental task for system administrators, developers, and anyone working with Unix-like systems. Whether you're troubleshooting, installing software, or ensuring compatibility, knowing your OS specifics is crucial. This article explores the most common and reliable methods to retrieve this information, covering a wide range of Linux distributions and Unix variants.
The uname
Command: A Universal Starting Point
The uname
command is a standard utility available on virtually all Unix and Linux systems. It provides basic system information, including the kernel name, hostname, kernel release, kernel version, and machine hardware name. While it doesn't directly give you the distribution name, it's an excellent first step to understand the underlying kernel.
uname -a
Display all system information using uname
The output of uname -a
typically looks like this:
Linux myhostname 5.15.0-76-generic #83~20.04.1-Ubuntu SMP Mon Jun 19 14:19:46 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
From this, you can deduce the kernel name (Linux
), hostname (myhostname
), kernel release (5.15.0-76-generic
), and architecture (x86_64
). However, the distribution name (e.g., Ubuntu, CentOS) is not explicitly stated.
uname -srm
to show only the kernel name, release, and machine hardware name. This is often sufficient for basic checks.Identifying Linux Distribution and Version
For Linux distributions, the most reliable way to find the distribution name and version is by checking specific files in the /etc
directory. The os-release
file (or its predecessors) has become the de facto standard for this information.
flowchart TD A[Start: Need OS Info?] --> B{Is /etc/os-release present?} B -- Yes --> C[Read /etc/os-release] C --> D[Extract NAME, VERSION_ID, PRETTY_NAME] B -- No --> E{Is /etc/lsb-release present?} E -- Yes --> F[Read /etc/lsb-release] F --> G[Extract DISTRIB_ID, DISTRIB_RELEASE] E -- No --> H{Check distribution-specific files?} H -- Yes --> I[e.g., /etc/redhat-release, /etc/debian_version] H -- No --> J[Fall back to uname -a] D --> K[End: OS Info Found] G --> K I --> K J --> K
Decision flow for identifying Linux distribution and version
Using /etc/os-release
(Recommended for Modern Linux)
Most modern Linux distributions adhere to the systemd
specification, which includes the /etc/os-release
file. This file contains distribution identification data and is the most consistent source of information.
cat /etc/os-release
Display content of the os-release
file
Example output from Ubuntu:
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"
BUILD_ID=20230801
UBUNTU_CODENAME=jammy
This file provides comprehensive details like NAME
, VERSION_ID
, and PRETTY_NAME
, which are exactly what you need.
/etc/os-release
using grep
and awk
or by sourcing the file in a shell script, as it follows a simple key-value format.grep -E '^(NAME|VERSION_ID|PRETTY_NAME)=' /etc/os-release
Extract specific fields from os-release
Using /etc/lsb-release
(Older Linux Distributions)
Some older Linux distributions, or those not fully adopting systemd
's os-release
standard, might use /etc/lsb-release
. This file is part of the Linux Standard Base (LSB) specification.
cat /etc/lsb-release
Display content of the lsb-release
file
Example output:
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.6 LTS"
Distribution-Specific Files
Before the widespread adoption of os-release
, many distributions had their own unique files for version information. These are still present on some systems and can serve as a fallback.
Red Hat/CentOS/Fedora
cat /etc/redhat-release
or
cat /etc/centos-release
or
cat /etc/fedora-release
Debian
cat /etc/debian_version
SuSE/OpenSUSE
cat /etc/SuSE-release
Identifying Traditional Unix Systems
For traditional Unix systems like Solaris, AIX, or HP-UX, the methods differ slightly, though uname
remains a common starting point.
Solaris/illumos
cat /etc/release
or
uname -v
AIX
oslevel -s
or
uname -v
HP-UX
uname -r
or
swlist -l product | grep -i "OS Environment"
FreeBSD
freebsd-version
or
uname -r
A Robust Script for OS Detection
To handle the various possibilities, especially in automated scripts, it's best to create a function or script that checks for these files in a specific order.
#!/bin/bash
get_os_info() {
if [ -f /etc/os-release ]; then
. /etc/os-release
echo "OS Name: ${PRETTY_NAME:-${NAME} ${VERSION_ID}}"
echo "ID: ${ID}"
echo "Version ID: ${VERSION_ID}"
elif [ -f /etc/lsb-release ]; then
. /etc/lsb-release
echo "OS Name: ${DISTRIB_DESCRIPTION:-${DISTRIB_ID} ${DISTRIB_RELEASE}}"
echo "ID: ${DISTRIB_ID}"
echo "Version ID: ${DISTRIB_RELEASE}"
elif [ -f /etc/redhat-release ]; then
echo "OS Name: $(cat /etc/redhat-release)"
echo "ID: redhat-based"
elif [ -f /etc/debian_version ]; then
echo "OS Name: Debian $(cat /etc/debian_version)"
echo "ID: debian"
elif [ -f /etc/SuSE-release ]; then
echo "OS Name: $(cat /etc/SuSE-release | head -n 1)"
echo "ID: suse-based"
else
echo "OS Name: Unknown Linux/Unix"
echo "Kernel: $(uname -srm)"
fi
}
get_os_info
A shell script to robustly detect OS name and version
This script prioritizes /etc/os-release
, then falls back to lsb-release
, and finally checks for distribution-specific files before resorting to uname
for generic Unix/Linux identification. This approach provides the most accurate and detailed information available on the system.