Check the type and version of your Linux distribution

1.First of all

There are many distributions of Linux. The ones used in corporate systems are limited, but there are still several types. Since the judgment method differs slightly depending on the distribution, we investigated the major distributions.

1-1. TL;DR

1-2. Target environment

2. Basics of distribution determination

The basis for determining the type of Linux distribution is to look at the / etc / * relax file. The following files are typical.

-** Common to all distributions **

For example, for CentOS, look at / etc / centos-release. If you see the following, you know that it is CentOS 8.1.

/etc/centos-release


CentOS Linux release 8.1.1911 (Core)

However, there is no / etc / centos-release other than CentOS. Therefore, it is necessary to change the referenced file according to the distribution. Also, / etc / os-release and / etc / system-release are distribution-independent files, but they are inconsistent.

3. Survey of each distribution

This time, we will investigate the following distributions. The RHEL system is based on 8, but it is basically the same for both 6 and 7.

3-1. Red Hat Enterprise Linux There are three files, / etc / system-release is a symbolic link to / etc / redhat-release.

$ ls -l /etc/*release
lrwxrwxrwx. 1 root root 22 Mar 31 07:15 /etc/os-release -> ..//usr/lib/os-release
-rw-r--r--. 1 root root 45 Mar 31 07:15 /etc/redhat-release
lrwxrwxrwx. 1 root root 14 Mar 31 07:15 /etc/system-release -> redhat-release
$ cat /etc/redhat-release
Red Hat Enterprise Linux release 8.2 (Ootpa)
$ cat /etc/os-release
NAME="Red Hat Enterprise Linux"
VERSION="8.2 (Ootpa)"
ID="rhel"
ID_LIKE="fedora"
VERSION_ID="8.2"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Red Hat Enterprise Linux 8.2 (Ootpa)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:redhat:enterprise_linux:8.2:GA"
HOME_URL="https://www.redhat.com/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"

REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 8"
REDHAT_BUGZILLA_PRODUCT_VERSION=8.2
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="8.2"

3-2. CentOS

On CentOS, / etc / system-release and / etc / redhat-release are symbolic links to / etc / centos-release.

$ ls -l /etc/*release
-rw-r--r--.1 root root 38 March 13 04:15 /etc/centos-release
lrwxrwxrwx.1 root root 21 March 13 04:15 /etc/os-release -> ../usr/lib/os-release
lrwxrwxrwx.1 root root 14 March 13 04:15 /etc/redhat-release -> centos-release
lrwxrwxrwx.1 root root 14 March 13 04:15 /etc/system-release -> centos-release
$ cat /etc/centos-release
CentOS Linux release 8.1.1911 (Core)
$ cat /etc/os-release
NAME="CentOS Linux"
VERSION="8 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="8"

** Tips ** </ font> Most RHEL distributions don't need to worry too much about the one decimal place of 8.1 or 8.2. This is because even the 8.1 image (media) can be updated with Yum / DNF to be the same as the latest at that time. The repository settings are also involved in this area, but it will be a deeper story, so if you are interested, please check it yourself. </ font>

3-3. Oracle Linux

In Oracle Linux, / etc / oracle-release has been added.

$ ls -l /etc/*release
-rw-r--r--. 1 root root  32 May  5 02:08 /etc/oracle-release
-rw-r--r--. 1 root root 479 May  5 02:08 /etc/os-release
-rw-r--r--. 1 root root  45 May  5 02:08 /etc/redhat-release
lrwxrwxrwx. 1 root root  14 May  5 02:08 /etc/system-release -> oracle-release

What's interesting is the comparison with CentOS. Whereas / etc / redhat-release is a symbolic link on CentOS, it is not a symbolic link on Oracle Linux.

$ cat /etc/oracle-release
Oracle Linux Server release 8.2
$ cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="8.2"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="8.2"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Oracle Linux Server 8.2"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:8:2:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://bugzilla.oracle.com/"

ORACLE_BUGZILLA_PRODUCT="Oracle Linux 8"
ORACLE_BUGZILLA_PRODUCT_VERSION=8.2
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=8.2

Since / etc / redhat-release is not a symbolic link, it is the same as RHEL as follows.

$ cat /etc/redhat-release
Red Hat Enterprise Linux release 8.2 (Ootpa)

Such a specification would be a compatibility measure for programs that check distributions.

For driver packages provided by commercial products and HW vendors, / etc / * -release is often used to determine the type and version of the distribution. By leaving the original / etc / redhat-release, you can misidentify it as RHEL and bypass the check.

Pseudo code for version determination


if /etc/redhat-Is there a release
Version confirmation
else if /etc/centos-Is there a release
Version confirmation

3-4. Amazon Linux 2

Amazon Linux 2 does not have a file like / etc / <distribution name> -release.

$ ls -l /etc/*release
-rw-r--r-- 1 root root 212 Jun 24 21:10 /etc/os-release
-rw-r--r-- 1 root root  31 Jun 24 21:10 /etc/system-release
$ cat /etc/system-release
Amazon Linux release 2 (Karoo)

Amazon Linux is a RHEL-based distribution, but unlike CentOS and Oracle Linux, it's not a pure RHEL-compatible distribution. Therefore, the contents of / etc / os-release are also different from other RHEL distributions.

$ cat /etc/os-release
NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2"
PRETTY_NAME="Amazon Linux 2"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL="https://amazonlinux.com/"

3-5. Ubuntu

On Ubuntu there are the following files: I'm using 20.04 LTS this time, but 18.04 LTS is basically the same.

$ ls -l /etc/*release
-rw-r--r-- 1 root root 102 Apr 15 11:09 /etc/lsb-release
lrwxrwxrwx 1 root root  21 Apr 15 11:09 /etc/os-release -> ../usr/lib/os-release

$ ls -l /etc/*version
-rw-r--r-- 1 root root 13 Dec  5  2019 /etc/debian_version
-rw-r--r-- 1 root root 31 Jul 14 15:46 /etc/ec2_version
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04 LTS"
VERSION_ID="20.04"
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"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

Ubuntu often uses the / etc / lsb-release and lsb_release -a commands. RHEL distributions also have the lsb_release command, but it is often not installed by default (redhat-lsb-core package).

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04 LTS"

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04 LTS
Release:        20.04
Codename:       focal
20.04 At LTS
$ cat /etc/debian_version
bullseye/sid

18.04 At LTS
$ cat /etc/debian_version
buster/sid

ec2 seems to be unique to Amazon EC2, but it was also found in Azure VM and Oracle Cloud Infrastructure Compute.

$ cat /etc/ec2_version
Ubuntu 20.04 LTS (Focal Fossa)

3-6. Bonus (sample code)

I think it's better to use sed in practice, but here's a distribution-independent sample code that determines the type and version of a distribution.

#!/bin/bash
FILENAME=/etc/os-release
dist=`grep -e ^NAME= ${FILENAME}`
dist=${dist#*\"}
dist=${dist%*\"}
echo "DIST: " $dist

version=`grep -e ^VERSION_ID= ${FILENAME}`
version=${version#*\"}
version=${version%*\"}
version=${version%*\.*}   #Minor version removed
echo "VER: " $version

Output result


DIST:  Oracle Linux Server
VER:  8

4. Summary

  • ** Use / etc / * release to find out the distribution type and version **
  • ** You can use / etc / os-release as a file common to all distributions **

Recommended Posts