How to Check CentOS Version – 8 Ways

Filed Under: CentOS
Check Centos Version Featured

There are many ways to check the CentOS version. In this tutorial, we will learn how to check the CentOS version through eight different utilities.

8 Ways to Check CentOS Version

  1. /etc/centos-release file
  2. /etc/system-release file
  3. /etc/os-release file
  4. /etc/redhat-release file
  5. lsb_release command
  6. rpm query
  7. rpm macro
  8. hostnamectl command

What does CentOS Version Number Means?

CentOS version have three parts.

  1. Major Version: Major release version number
  2. Minor Version: Minor release version number
  3. Monthstamp: codebase month and year timestamp

1. /etc/centos-release file

The CentOS specific file, which contains the CentOS release and version details.

[root@centos ~]# cat /etc/centos-release
CentOS Linux release 8.1.1911 (Core) 
[root@centos ~]# 
  • Major Version: 8
  • Minor Version: 1
  • Monthstamp: 1911 i.e. November 2019.
Centos Release Version
Centos Release Version

2. /etc/system-release file

This file also contains the same information as the centos-release file.

[root@centos ~]# cat /etc/system-release
CentOS Linux release 8.1.1911 (Core) 
[root@centos ~]#

3. /etc/os-release file

This file contains the Linux OS information. You can get the major version number from this file.

[root@centos ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="8 (Core)"
ID_LIKE="rhel fedora"
PRETTY_NAME="CentOS Linux 8 (Core)"


[root@centos ~]# 

4. /etc/redhat-release file

CentOS is built on top of RedHat Linux distribution. This file also contains the same information as the centos-release file.

[root@centos ~]# cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core) 
[root@centos ~]#

5. lsb_release command

This command doesn’t come pre-installed with the CentOS server. You can install it using the following command.

[root@centos ~]# yum install redhat-lsb

Then check the CentOS version using the following command.

[root@centos ~]#  lsb_release -d
Description:	CentOS Linux release 8.1.1911 (Core) 
[root@centos ~]#
Lsb Release Centos Version

6. rpm query

We can query the rpm package manager to get the CentOS version information.

[root@centos ~]# rpm -q centos-release
[root@centos ~]# 
Rpm Centos Version

7. rpm macro

Here is a simple rpm macro evaluation to get the CentOS major version.

[root@centos ~]# rpm -E %{rhel}
[root@centos ~]#
Rpm Macro Centos Version

8. hostnamectl command

The hostnamectl command can be used to get the system information. It also reveals the Operating System version.

[root@centos ~]# hostnamectl
   Static hostname: localhost.localdomain
Transient hostname:
         Icon name: computer-vm
           Chassis: vm
        Machine ID: c2a4bfa7e0c74457b3a978656ab959e8
           Boot ID: c89bae2d3ec7493987a455bfa15e4818
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1062.12.1.el7.x86_64
      Architecture: x86-64
[root@centos ~]# 
[root@centos ~]# 
[root@centos ~]# hostnamectl | grep "Operating System"
  Operating System: CentOS Linux 7 (Core)
[root@centos ~]# 

Are you wondering why this command is printing CentOS 7, whereas others are printing CentOS 8?

It’s because the hostnamectl command doesn’t work in Docker containers. This command output is from my own VPS machine whereas the other commands output is from my local Docker CentOS 8 container.

How to Check CentOS Version Programatically?

I am a Java and Python programmer. Let’s see how to find the CentOS version in Java and Python programs.

1. Java Program to Print CentOS Version

There is no specific Java class that provides operating system information.

I am just reading the /etc/centos-release file and then printing it to the console.

import java.nio.file.Files;
import java.nio.file.Paths;

public class CentOSVersion {

	public static void main(String[] args) throws IOException {
		String centosVersion = new String(Files.readAllBytes(Paths.get("/etc/centos-release")));


Centos Version Java Program

2. Python Program to Print CentOS Version

We can use the platform module to get the operating system information.

# Python 3 Script

import platform

print('Server Details:', platform.linux_distribution())

I am running this on my CentOS 7 VPS server, where Python is pre-installed.

[root@centos ~]# python3 
Server Details: ('CentOS Linux', '7.7.1908', 'Core')
[root@centos ~]# 


There are many ways to find out the CentOS version information. We also learned how to get the CentOS version in Python and Java programs.

    Generic selectors
    Exact matches only
    Search in title
    Search in content
    Post Type Selectors