Check CentOS Version

There are several ways on how to check what version of CentOS is running on your system. The simplest way to check for the CentOS version number is to execute the cat /etc/centos-release command. Identifying the accurate CentOS version may be required to help you or your support team to troubleshoot your CentOS system.

The CentOS version consists of three release versions as illustrated below:
Identifying the CentOS Release version
The CentOS version consists of Major, Minor and Asynchronous Release number.

What is the command to check CentOS version?

The following table contains most common and recommended ways on how to check CentOS version on your CentOS Linux server or desktop.

Command Description
$ rpm -q centos-release CentOS version valid for CentOS 6 and higher. Causes to reveal major, minor and asynchronous CentOS version.
$ lsb_release -d Requires redhat-lsb package to be installed before execution.
$ cat /etc/centos-release Linux cat command to output content of the /etc/centos-release to query CentOS version. Works with CentOS 6 and higher.


Alternative commands to check CentOS version

In case the above-provided commands did not help you to obtain the CentOS version number you may try the following alternative commands.

Although available only for CentOS version 7 and above the hostnamectl command might provide you with a significant clue about your OS version number:
$ hostnamectl 
   Static hostname: localhost.localdomain
         Icon name: computer-vm
           Chassis: vm
        Machine ID: fe069af6a1764e07be909d7cf64add99
           Boot ID: b81bb73dc549484c8927e830e149eb55
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-862.6.3.el7.x86_64
      Architecture: x86-64
For more answers try to query all release files within the /etc directory:
$ cat /etc/*elease
CentOS Linux release 7.5.1804 (Core) 
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"

CentOS Linux release 7.5.1804 (Core) 
CentOS Linux release 7.5.1804 (Core)
The system you are running may have macros defined which can help you in identifying the major release version of your CentOS Linux server. Try the following:
$ rpm --eval '%{centos_ver}'
7
Lastly, your GRUB boot manu may provide some answers. This is not most reliable way to check for CentOS version however it may provide you with some clues:
# grep -w menuentry /boot/grub2/grub.cfg /etc/grub2.cfg
/boot/grub2/grub.cfg:menuentry 'CentOS Linux (3.10.0-862.6.3.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-693.el7.x86_64-advanced-176eba78-e8ec-475d-9086-0d582fcd4305' {
...
 


Using Programming to check CentOS version

In case you wish to program your way to check the CentOS version automatically you have multiple options available. This section will list some basic examples of how to check CentOS version using Bash script and Python programming language.

Bash Script to check CentOS version

The following bash script can be used to obtain the CentOS version number given that the /etc/centos-release file exists and is populated. Serves as an example, modify wherever appropriate:
#!/bin/bash

full=`cat /etc/centos-release | tr -dc '0-9.'`
major=$(cat /etc/centos-release | tr -dc '0-9.'|cut -d \. -f1)
minor=$(cat /etc/centos-release | tr -dc '0-9.'|cut -d \. -f2)
asynchronous=$(cat /etc/centos-release | tr -dc '0-9.'|cut -d \. -f3)

echo CentOS Version: $full
echo Major Relase: $major
echo Minor Relase: $minor
echo Asynchronous Relase: $asynchronous
Output:
$ ./check-centos-version.sh 
CentOS Version: 7.5.1804
Major Relase: 7
Minor Relase: 5
Asynchronous Relase: 1804

Python program to check CentOS version

The following python script will output the distribution name along with the OS version number:
#!/usr/bin/python

import platform
print platform.linux_distribution()
Output:
$ python check-centos-version.py 
('CentOS Linux', '7.5.1804', 'Core')
ARE YOU LOOKING FOR A LINUX JOB?
Submit your RESUME or create a JOB ALERT on LinuxCareers.com job portal.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in:



Comments and Discussions