125

How do I determine the version of a CentOS server without access to any graphical interface? I've tried several commands:

# cat /proc/version
Linux version 2.6.18-128.el5 (mockbuild@hs20-bc1-7.build.redhat.com)
(gcc version 4.1.2 20080704 (Red Hat 4.1.2-44)) …

# cat /etc/issue
Red Hat Enterprise Linux Server release 5.3 (Tikanga)

but which one is correct: 4.1.2-4 from /proc/version or 5.3 from /etc/issue?

10 Answers 10

43

As you can see in /etc/issue, you're using CentOS 5.3. (It says Red Hat because CentOS is based upon the RH sources, and some software checks /etc/issue to identify the distro in use; thus, they'd fail if this was changed to CentOS).

The 4.1.2-4 in /proc/version refers to the version of the gcc C compiler used to build the kernel.

  • 3
    I'll just add, what You could use uname -a to detect, if it 32 or 64 bit compatible. – Fedir RYKHTIK May 31 '13 at 9:46
  • 15
    On CentOS 7, 'cat /etc/issue' yields gobbledygook: \S Kernel \r on an \m – Urhixidur Jul 8 '15 at 16:09
  • 9
    Correct answer should be the one by h3rrmiller... cat /etc/*elease. /etc/issue is often replaced by an organisation's MOTD/disclaimer – batfastad Oct 7 '15 at 12:52
  • The /etc/issue file shows that it is RHEL, not CentOS. – Michael Hampton Jun 18 at 20:03
161

In cases like CentOS the actual version is usually placed in /etc/*elease.

cat /etc/*elease

granted this file usually holds the version of the entire OS minus the kernel (since you can choose which to load). This file will have the same information as /etc/issue but with CentOS instead of RedHat

  • 1
    cat /etc/*release will work too, and you are not omitting a letter of what are you looking for ;) because elease is not always easy to guess. * can be nothing, all, one or more items at time. Thank you, i always forgot the way to get the release version. – erm3nda May 22 '15 at 17:11
  • 9
    The reason I leave the "R" off is because in some cases the "R" in "release" is capitalized. – h3rrmiller Jun 1 '15 at 19:08
  • Just to say, doing ls /etc/*elease on my system gives /etc/centos-release /etc/redhat-release /etc/system-release. So I'm guessing from all this that the release files tend to be in /etc/*-release - but possibly with some capitalisation. – mwfearnley Aug 7 '15 at 12:35
  • 2
    This is correct. I find at many sites that /etc/issue has been overwritten with an MOTD or security disclaimer for use with the Banner option in sshd_config – batfastad Oct 6 '15 at 14:20
  • cat /etc/*os-release should yield more targetted results for CentOS and still work on Ubuntu and others. – webaholik Apr 15 at 19:59
19

The most reliable way of finding MAJOR version of CentOS (5 or 6 etc) is:

# rpm -q --queryformat '%{VERSION}' centos-release
6

For RHEL do this:

# rpm -q --queryformat '%{RELEASE}' redhat-release-server | awk -F. '{print $1}'
7

The only portable way of finding out a version without lsb_release or other tools is:

# grep -oE '[0-9]+\.[0-9]+' /etc/redhat-release
6.5
  • 3
    Can be simplified to: cat /etc/redhat-release | grep -o '[0-9]\.[0-9]' – Dave Johansen Jul 31 '15 at 23:22
  • 1
    +1 rpm -q centos-release is the only way I have yet found to get the minor version on older CentOS5 releases where /etc/redhat-release only contains the major version. Thanks for the hint. – PolyTekPatrick Nov 19 '15 at 6:54
  • 1
    To get the major version: cat /etc/redhat-release | grep -oP '(?<= )[0-9]+(?=\.)' – Rockallite Feb 8 '17 at 7:16
  • 1
    Just curious: why "cat file | grep pattern" instead of "grep pattern file"? – barrycarter Jun 17 '17 at 15:36
  • Right, digged that from shell history and was able to correct this. Fixed. – lzap Jul 3 '17 at 6:48
13

You can determine it by just calling the following command:

hostnamectl

Which will return as the following:

Static hostname: mgbcctli01
     Icon name: computer-vm
       Chassis: vm
    Machine ID: de14d80a0900427894dbcf6137e058e7
       Boot ID: 6865f9839c064bc9be32281d0f262cc8
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
   CPE OS Name: cpe:/o:centos:centos:7
        Kernel: Linux 3.10.0-514.2.2.el7.x86_64
  Architecture: x86-64

You can also use rpm to find details about CentOS version:

rpm --query centos-release

Which will return in my case:

centos-release-7-3.1611.el7.centos.x86_64
  • 1
    hostnamectl only returns the major release number – fpmurphy Jan 24 '17 at 11:48
  • 1
    That's right, using rpm, you can get a detailed version – hd84335 Jan 24 '17 at 11:49
6

Correct way is lsb_release -d.

  • 8
    On CentOS 7: lsb_release: Command not found. – Dereckson Feb 17 '16 at 0:42
  • yum install redhat-lsb-core ... – gena2x Feb 17 '16 at 17:13
  • 7
    Sure, but I meant: I'm not sure is valuable to have to install a new package to get the current version. – Dereckson Feb 17 '16 at 19:07
  • 1
    If you want some command which is able to do so across multiple distributions it may still be worth installing. – gena2x Feb 18 '16 at 9:49
5
# echo "I am running: `cat /etc/redhat-release` (`arch`)"

Outputs the following:

I am running: CentOS release 6.7 (Final) (x86_64)
3

It can be found at the location /etc, inside the file os-release. So type in:

cat /etc/os-release
  • Was this backported to CentOS 5 or 6? If not, this is mostly useful going forward.... – mattdm Aug 17 '17 at 23:52
  • CentOS made this available starting with CentOS 7 - I've not seen a backport. – webaholik Apr 15 at 19:54
3

The most truly reliable (and short) way to get MAJOR version of either CentOS or RHEL is:

rpm -E %{rhel}

Will give you a value of e.g. 6, 7, or 8 (now that RHEL 8 is out).

2

Run rpm --eval '%{centos_ver}' to get MAJOR version of centos.

0

Here is some command I collected through google, may help someone:

https://forum.directadmin.com/showthread.php?t=15878

cat /etc/*release*
cat /etc/centos-release

http://www.liquidweb.com/kb/how-to-check-your-centos-version/

cat /etc/redhat-release

https://linuxconfig.org/how-to-check-centos-version

# the later two may need some package to install
rpm --query centos-release
hostnamectl
lsb_release -d

I created a gist to record this, too.

protected by Community Jun 4 at 9:33

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.