Try this command:
Copyrpm -qa | grep release
for instance on my machine I get this
Answer from Tim on Stack Overflowredhat-release-workstation-6Workstation-6.4.0.4.el6.x86_64
Try this command:
Copyrpm -qa | grep release
for instance on my machine I get this
redhat-release-workstation-6Workstation-6.4.0.4.el6.x86_64
If "anybody" has root access to your machine to either change /etc/redhat-release or install an alternate kernel you're most probably in bigger trouble than determining the redhat version of your system.
Just use the value pointed out by /etc/redhat-release or even better in terms of portability use the output of lsb_release as this is exactly the purpose they were made for.
With "anybody" being able to do anything with your system there is no other chance at all.
You can use the lsb_release command on various Linux distributions:
lsb_release -i -r
This will tell you the Distribution and Version and is a little bit more accurate than accessing files that may or may not have been modified by the admin or a software package. As well as working across multiple distros.
For RHEL, you should use:
cat /etc/redhat-release
You can look at the contents of /etc/redhat-release, which will look something like this:
$ cat /etc/redhat-release
CentOS release 5.4 (Final)
The contents are different for an actual RHEL system. This technique works on all RedHat derivatives, including CentOS, Fedora, and others.
Videos
The kernel is universally detected with uname:
$ uname -or
2.6.18-128.el5 GNU/Linux
There really isn't a cross-distribution way to determine what distribution and version you're on. There have been attempts to make this consistent, but ultimately it varies, unfortunately. LSB tools provide this information, but ironically aren't installed by default everywhere. Example on an Ubuntu 9.04 system with the lsb-release package installed:
$ lsb_release -irc
Distributor ID: Ubuntu
Release: 9.04
Codename: jaunty
Otherwise, the closest widely-available method is checking /etc/something-release files. These exist on most of the common platforms, and on their derivatives (i.e., Red Hat and CentOS).
Here are some examples.
Ubuntu has /etc/lsb-release:
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=9.04
DISTRIB_CODENAME=jaunty
DISTRIB_DESCRIPTION="Ubuntu 9.04"
But Debian has /etc/debian_version:
$ cat /etc/debian_version
5.0.2
Fedora, Red Hat and CentOS have:
Fedora: $ cat /etc/fedora-release
Fedora release 10 (Cambridge)
Red Hat/older CentOS: $ cat /etc/redhat-release
CentOS release 5.3 (Final)
newer CentOS: $ cat /etc/centos-release
CentOS Linux release 7.1.1503 (Core)
Gentoo:
$ cat /etc/gentoo-release
Gentoo Base System release 1.12.11.1
I don't have a SUSE system available at the moment, but I believe it is /etc/SuSE-release.
Slackware has /etc/slackware-release and/or /etc/slackware-version.
Mandriva has /etc/mandriva-release.
For most of the popular distributions then,
$ cat /etc/*{release,version}
will most often work. Stripped down and barebones "server" installations might not have the 'release' package for the distribution installed.
Additionally, two 3rd party programs you can use to automatically get this information are Ohai and Facter.
Note that many distributions have this kind of information in /etc/issue or /etc/motd, but some security policies and best practices indicate that these files should contain access notification banners.
Related: How to find out version of software package installed on the node?, puppet.
You could also try:
$ cat /etc/issue
It usually (not always, though) will tell you what distribution you are using. /etc/issue is the file used for the login screen.
if grep -q -i "release 6" /etc/redhat-release
then
echo "running RHEL 6.x"
fi
This would be the simplest way I can think of.
You can also use the lsb_release command. If you're already certain it's RHEL, getting the major version number is:
majversion=$(lsb_release -rs | cut -f1 -d.)