why unix | RBL service | netrs | please | ripcalc | linescroll
rhel things

rhel things

There are different ways to identify which version of RHEL, CentOs, Rocky, ScientificLinux, OracleLinux. Lets go through them, but first, if you just want a reliable way of getting your EL version, look at the provider of /etc/redhat-release, then queryformat the version of that:

$ rpm --query --file /etc/redhat-release
fedora-release-common-35-0.7.noarch

$ rpm --query --queryformat '%{VERSION}\n' fedora-release-common-35-0.7.noarch
35

centos 6:

RPM=`rpm --query --file /etc/redhat-release`
RHEL=`rpm --query --queryformat '%{VERSION}\n' "$RPM"`
echo "$RHEL"
6

oraclelinux 6:

RPM=`rpm --query --file /etc/redhat-release`
RHEL=`rpm --query --queryformat '%{VERSION}\n' "$RPM"`
echo "$RHEL"
6Server

centos 7:

RPM=`rpm --query --file /etc/redhat-release`
RHEL=`rpm --query --queryformat '%{VERSION}\n' "$RPM"`
echo "$RHEL"
7

oraclelinux 7:

RPM=`rpm --query --file /etc/redhat-release`
RHEL=`rpm --query --queryformat '%{VERSION}\n' "$RPM"`
echo "$RHEL"
7.9

centos 8:

RPM=`rpm --query --file /etc/redhat-release`
RHEL=`rpm --query --queryformat '%{VERSION}\n' "$RPM"`
echo "$RHEL"
8.3

oraclelinux 8:

RPM=`rpm --query --file /etc/redhat-release`
RHEL=`rpm --query --queryformat '%{VERSION}\n' "$RPM"`
echo "$RHEL"
8.4

If you want to include one level:

echo "$RHEL" | egrep '^8([^0-9]|\.)' >/dev/null
if test $? -eq 0; then
    echo "EL8"
fi

echo "$RHEL" | egrep '^7([^0-9]|\.)' >/dev/null
if test $? -eq 0; then
    echo "EL7"
fi

echo "$RHEL" | egrep '^6([^0-9]|\.)' >/dev/null
if test $? -eq 0; then
    echo "EL6"
fi

Lets look at some of the other ways, and explain why not.

$RPM

Looking for just the 'el' part of the RPM variable can be problematic, there's no guarantee that 'el' will be present if the package is from another vendor that doesn't use the RH naming scheme:

oraclelinux-release-6Server-10.0.2.x86_64

We can see by looking where the version is, but finding that in a reliable and simple way could become tedious.

uname

uname doesn't give you the EL level, only the kernel, which may be custom.

hostnamectl

It isn't guaranteed to be present, and in some cases, such as containers, is likely not to be able to run.