For modern Fedora/RHEL-based systems, these commands reveal the distro version:
# Method 1: Check release file cat /etc/os-release # Method 2: RedHat-specific (works for RHEL/CentOS/Fedora) cat /etc/redhat-release # Method 3: Using hostnamectl (systemd systems) hostnamectl | grep "Operating System"
When you need precise version matching against legacy systems:
# For Fedora Core 4 detection: if grep -q "Fedora Core release 4" /etc/redhat-release; then echo "This is Fedora Core 4" fi # For RHEL/CentOS version check: rpm -q --qf '%{VERSION}' redhat-release || \ rpm -q --qf '%{VERSION}' centos-release
Here's a robust bash function for version detection:
function get_linux_flavor() { if [[ -f /etc/os-release ]]; then . /etc/os-release echo "$PRETTY_NAME" elif [[ -f /etc/redhat-release ]]; then cat /etc/redhat-release elif [[ -f /etc/centos-release ]]; then cat /etc/centos-release else echo "Unknown distribution" fi }
For containerized environments or minimal installs:
# Check available package manager if command -v dnf >/dev/null 2>&1; then echo "Fedora/RHEL 8+ detected" elif command -v yum >/dev/null 2>&1; then echo "Legacy RHEL/Fedora detected" fi
For scripting version checks:
# Compare versions in scripts current_version=$(rpm -E %fedora || rpm -E %rhel) if [[ $current_version -lt 30 ]]; then echo "Older than Fedora 30/RHEL 8" fi
The most reliable method is to examine the
/etc/os-release
file, which is standardized across modern Linux distributions:cat /etc/os-release
Example output for Fedora 38:
NAME="Fedora Linux" VERSION="38 (Workstation Edition)" ID=fedora VERSION_ID=38 VERSION_CODENAME="" PLATFORM_ID="platform:f38" PRETTY_NAME="Fedora Linux 38 (Workstation Edition)"
For older systems or specific cases, these commands can help:
Using hostnamectl
hostnamectl | grep -i "operating system"
Checking Red Hat Release Files
cat /etc/redhat-release cat /etc/fedora-release cat /etc/centos-release
Example output for CentOS 7:
CentOS Linux release 7.9.2009 (Core)
Here's a bash function to identify your distro:
function get_distro() { if [ -f /etc/os-release ]; then . /etc/os-release echo "$PRETTY_NAME" elif [ -f /etc/redhat-release ]; then cat /etc/redhat-release elif [ -f /etc/fedora-release ]; then cat /etc/fedora-release elif [ -f /etc/centos-release ]; then cat /etc/centos-release else echo "Unknown distribution" fi } get_distro
For RPM-based systems, you can query package versions:
rpm -q fedora-release rpm -q centos-release rpm -q redhat-release
How to Identify Linux Distribution Version via Command Line (Fedora/RHEL/CentOS)
2 views