1. Overview

In this tutorial, we’ll learn which Linux distro is running on our computer. It has been tested on the two most popular Linux families: Debian and Red Hat Enterprise Linux (RHEL). However, it aims to work for the whole Linux ecosystem.

2. /etc/os-release File

First, we’ll describe the /etc/os-release file.

Let’s see what it looks like on a computer with Ubuntu:

$ cat /etc/os-release 
NAME="Ubuntu"
VERSION="18.04.6 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.6 LTS"
VERSION_ID="18.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=bionic
UBUNTU_CODENAME=bionic

The output follows a VARIABLE=value structure:

  • ID: distro name (ubuntu)
  • VERSION_ID: distro major version (18.04)
  • ID_LIKE: distro family (debian)
  • PRETTY_NAME: combines the distro name (Ubuntu) and full version (18.04.6 LTS) into a single variable
  • VERSION_CODENAME: codename (bionic), i.e., an alias for a release number in Debian-based distros

Let’s inspect how it looks in a distro from the RHEL family:

$ cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

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

It presents a very similar output to the one from an Ubuntu machine. Note that the PRETTY_NAME value CentOS Linux 7 (Core) slightly differs from Debian-based systems.

Also, we can exploit such VARIABLE=value structure to source the /etc/os-release file. It’ll make the variables available to the shell environment:

$ . /etc/os-release 
$ echo $ID
ubuntu
$ echo $VERSION_ID
20.04

In this example, we access the value of the OS version after sourcing /etc/os-release.

3. Other /etc/*-release Files

Apart from os-release, there might be other files with the -release suffix inside the /etc directory.

In CentOS, we find the following:

$ ls -l /etc/*-release
-rw-r--r--. 1 root root 37 nov 23  2020 /etc/centos-release
lrwxrwxrwx. 1 root root 21 feb 26  2021 /etc/os-release -> ../usr/lib/os-release
lrwxrwxrwx. 1 root root 14 feb 26  2021 /etc/redhat-release -> centos-release
lrwxrwxrwx. 1 root root 14 feb 26  2021 /etc/system-release -> centos-release

In fact, /etc/centos-release, /etc/redhat-release, and /etc/system-release contain the same information since they all point to the same file:

$ cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)

This way, we can obtain basic information about the system. The above command shows the distro (CentOS Linux), major (7) and minor releases (9), and the date code (2009) of the minor version.

4. lsb_release Command

The lsb_release command also provides some information about the Linux distribution. However, it isn’t usually installed by default. Therefore we’ll have to install it manually.

In Debian-based distros:

$ sudo apt install -y lsb_release

In RHEL/CentOS:

$ yum -y install redhat-lsb-core

Let’s show all the available information on the Ubuntu machine with the -a option:

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.6 LTS
Release:	18.04
Codename:	bionic

Let’s now pick out only the release via the -r option:

$ lsb_release -r
Release:	18.04

If we do some shell scripting, we’ll add -s to have the raw number exclusively:

$ release=$(lsb_release -rs)<br />$ echo $release <br />18.04

This command only extracts the release number and assigns it to a variable called release.

In fact, the lsb_release formats and prints the contents of the /etc/lsb_release file. Let’s have a look at it on the Ubuntu computer:

$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.6 LTS"

We again see the VARIABLE=value structure similar to the /etc/os-release file. Likewise, we can source this file to make the variables available in the shell environment.

5. hostnamectl Command

The hostnamectl command is part of systemd. It basically allows the user to query and change the system hostname and other related information.

For example, it shows the following on our Ubuntu machine:

$ hostnamectl 
   Static hostname: ubuntu
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: 0fb7ce66968341cc97b76d1b24426881
           Boot ID: 5d39cbf8fa4f470db74aada29a2ef6b4
  Operating System: Ubuntu 18.04.6 LTS
            Kernel: Linux 5.4.0-109-generic
      Architecture: x86-64

Its output is a mix between the lsb_release and the uname -a commands. The latter provides the kernel name, node name (hostname), kernel release and version, and system architecture, among others:

$ uname -a
Linux ubuntu 5.13.0-41-generic #46~20.04.1-Ubuntu SMP Wed Apr 20 13:16:21 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

We can see similarities between the output of uname and the info from hostnamectl, such as the hostname (ubuntu), the kernel release (5.13.0-41-generic), and the kernel architecture (x86_64).

Let’s check the output in our CentOS machine:

$ hostnamectl 
   Static hostname: centos
         Icon name: computer-server
           Chassis: server
        Machine ID: 9ceb2494f7844e90b51e436b38872021
           Boot ID: 20d3d7b05e024420b46aa799db5e239a
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1160.15.2.el7.x86_64
      Architecture: x86-64

We can see the outputs of Debian-based and RHEL-based distros are quite similar.

6. rpm Command

Lastly, we’ll use the rpm command on distros from the RHEL family. It’s a package manager but can also be used to query some system data.

If we don’t know the distro, we find out the RHEL major version:

$ rpm --eval '%{rhel}'
7

The previous command checks the value of a macro defined in rpm config files. In this case, the macro (%rhel) is stored within the /etc/rpm/macros.dist file. We can learn more about macros in the macro syntax manual.

Whereas if we do know the distro we have, we can use the -q option:

$ rpm -q centos-release
centos-release-7-9.2009.1.el7.centos.x86_64

It queries (-q) whether the package centos-release is installed. As a result, we get the major and minor versions, as well as the asynchronous release number and the architecture. In Red Hat, we would query the package redhat-release instead.

In addition, we can obtain a bit more information about the OS with the -i option:

$ rpm -q centos-release -i
Name        : centos-release
Version     : 7
Release     : 9.2009.1.el7.centos
Architecture: x86_64
Install Date: Fri 26 Feb 2021 17:11:33 CET
Group       : System Environment/Base
Size        : 44787
License     : GPLv2
Signature   : RSA/SHA256, Wed 02 Dic 2020 17:35:28 CET, Key ID 24c6a8a7f4a80eb5
Source RPM  : centos-release-7-9.2009.1.el7.centos.src.rpm
Build Date  : Mon 23 Nov 2020 16:08:41 PM CET
Build Host  : x86-01.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
Summary     : CentOS Linux release file
Description :
CentOS Linux release files

As with the hostnamectl output, we can see a very comprehensive description, including the name, version, release, architecture, and the install and build dates.

7. Conclusion

We’ve learned multiple ways to check the Linux distro with the command line. In either case, the choice of one method depends on our goal.

For a quick view of the distro and system features, we suggest hostnamectl or rpm -qi <distro name>-release (in case of using a distro from the RHEL family).

On the other hand, if we need to do some Bash scripting, we’d best sourcing either /etc/os-release or /etc/lsb_release.

Comments are closed on this article!