When working with Red Hat Enterprise Linux systems, you'll often need to verify the exact OS version for compatibility checks, troubleshooting, or documentation purposes. Here are several reliable commands that work without root privileges:
# Method 1: Using /etc/redhat-release
cat /etc/redhat-release
# Sample output:
# Red Hat Enterprise Linux Server release 7.9 (Maipo)
If the standard method doesn't work or you need more detailed information:
# Method 2: Using lsb_release (if installed)
lsb_release -d
# Method 3: Parsing system-release file
cat /etc/system-release
# Method 4: Checking os-release (modern systems)
cat /etc/os-release | grep PRETTY_NAME
For scripting purposes, you might want to extract just the major and minor version numbers:
# Bash script example to extract version components
rhel_version=$(cat /etc/redhat-release)
if [[ $rhel_version =~ release[[:space:]]+([0-9]+)\.([0-9]+) ]]; then
major=${BASH_REMATCH[1]}
minor=${BASH_REMATCH[2]}
echo "RHEL ${major}.${minor}"
fi
Some systems might have different file structures. Here's how to handle them:
# For RHEL 8+ and CentOS Stream:
cat /etc/redhat-release || cat /etc/centos-release
# For containerized environments:
if [ -f /.dockerenv ]; then
echo "Running in container:"
grep -E '^VERSION=' /etc/os-release
fi
The version files in /etc/ are world-readable by design, as this information is considered non-sensitive but essential for many system operations and user applications. This contrasts with other system information that might require elevated privileges.
As a Linux system administrator or developer, you often need to verify the Red Hat Enterprise Linux (RHEL) version for compatibility checks or troubleshooting. Here are several reliable methods that don't require root access:
# Method 1: Using /etc/redhat-release
cat /etc/redhat-release
# Example output:
# Red Hat Enterprise Linux Server release 7.9 (Maipo)
When the standard method isn't available, try these alternatives:
# Method 2: Using lsb_release (if installed)
lsb_release -d
# Method 3: Parsing /etc/os-release
grep PRETTY_NAME /etc/os-release
# Method 4: Using hostnamectl (on newer systems)
hostnamectl | grep "Operating System"
For scripting purposes, you might need to extract just the version numbers:
# Get major version
cat /etc/redhat-release | sed -E 's/.*release ([0-9]+).*/\1/'
# Get full version including minor
cat /etc/redhat-release | sed -E 's/.*release ([0-9.]+).*/\1/'
# Alternative using awk
awk -F'[ .]' '{print $7"."$8}' /etc/redhat-release
While not the same as RHEL version, kernel version can be useful for debugging:
uname -r
# Example output: 3.10.0-1160.45.1.el7.x86_64
For containerized environments or minimal installations where standard files might be missing:
# Check for alternative release files
[ -f /etc/centos-release ] && cat /etc/centos-release
[ -f /etc/system-release ] && cat /etc/system-release
# Using rpm query
rpm -q --qf "%{VERSION}" redhat-release