Best way to find os name and version in Unix/Linux platform
Solution 1
This work fine for all Linux environment.
#!/bin/sh
cat /etc/*-release
In Ubuntu:
$ cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.04
DISTRIB_CODENAME=lucid
DISTRIB_DESCRIPTION="Ubuntu 10.04.4 LTS"
or 12.04:
$ cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.4 LTS"
NAME="Ubuntu"
VERSION="12.04.4 LTS, Precise Pangolin"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu precise (12.04.4 LTS)"
VERSION_ID="12.04"
In RHEL:
$ cat /etc/*-release
Red Hat Enterprise Linux Server release 6.5 (Santiago)
Red Hat Enterprise Linux Server release 6.5 (Santiago)
Or Use this Script:
#!/bin/sh
# Detects which OS and if it is Linux then it will detect which Linux
# Distribution.
OS=`uname -s`
REV=`uname -r`
MACH=`uname -m`
GetVersionFromFile()
{
VERSION=`cat $1 | tr "\n" ' ' | sed s/.*VERSION.*=\ // `
}
if [ "${OS}" = "SunOS" ] ; then
OS=Solaris
ARCH=`uname -p`
OSSTR="${OS} ${REV}(${ARCH} `uname -v`)"
elif [ "${OS}" = "AIX" ] ; then
OSSTR="${OS} `oslevel` (`oslevel -r`)"
elif [ "${OS}" = "Linux" ] ; then
KERNEL=`uname -r`
if [ -f /etc/redhat-release ] ; then
DIST='RedHat'
PSUEDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//`
REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//`
elif [ -f /etc/SuSE-release ] ; then
DIST=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//`
REV=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ //`
elif [ -f /etc/mandrake-release ] ; then
DIST='Mandrake'
PSUEDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//`
REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//`
elif [ -f /etc/debian_version ] ; then
DIST="Debian `cat /etc/debian_version`"
REV=""
fi
if [ -f /etc/UnitedLinux-release ] ; then
DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]"
fi
OSSTR="${OS} ${DIST} ${REV}(${PSUEDONAME} ${KERNEL} ${MACH})"
fi
echo ${OSSTR}
Solution 2
Following command worked out for me nicely. It gives you the OS name and version.
lsb_release -a
Solution 3
The "lsb_release" command provides certain Linux Standard Base and distribution-specific information. So using the below command we can get Operating system name and operating system version.
"lsb_release -a"
Solution 4
this command gives you a description of your operating system
cat /etc/os-release
Solution 5
In every distribute it has difference files so I write most common ones:
---- CentOS Linux distro
`cat /proc/version`
---- Debian Linux distro
`cat /etc/debian_version`
---- Redhat Linux distro
`cat /etc/redhat-release`
---- Ubuntu Linux distro
`cat /etc/issue` or `cat /etc/lsb-release`
in last one /etc/issue didn't exist so I tried the second one and it returned the right answer
Niraj Nandane
Updated on July 05, 2022Comments
-
Niraj Nandane almost 2 years
I need to find the OS name and version on Unix/Linux platform. For this I tried following:
lsb_release
utility/etc/redhat-release
or specific file
But it does not seem to be best solution as LSB_RELEASE support is no longer for RHEL 7.
Is there any way that will work on any Unix or Linux platform?
-
Niraj Nandane over 9 yearsyeah you are right but i dont want to read it from *-release file.
-
Niraj Nandane over 9 yearsi want some utility like lsb_realease
-
Gilles Quenot over 9 yearsPost edited accordingly. You have to install perl's module
Linux::Distribution
-
Gilles Quenot over 9 years
liblinux-distribution-perl
package for debian & derivatives -
Gilles Quenot over 9 years-1 : output on my archlinux is
Linux 3.16.4-1-ARCH( 3.16.4-1-ARCH x86_64)
-
Niraj Nandane over 9 yearsThe script is useful but for linux it is showing ==Linux RedHat version(Final 2.6.32-431.el6.x86_64 x86_64) .my redhat version is 6.5 but it is not showing in output ?
-
kvivek over 9 yearsI tested on RHEL6.3 It is showing output as
Linux RedHat 6.3(Santiago 2.6.32-279.22.1.el6.x86_64 x86_64)
-
kvivek over 9 yearsWhat is the Output of this Command
cat /etc/redhat-release | sed s/.*\(// | sed s/\)//
in your RHEL 6.5 -
Cocowalla over 4 yearsWhile this gets information that may be useful, it doesn't answer the question (getting the OS name and version)
-
Steve Amerige almost 3 yearsNote that
[ -f /etc/redhat-release ]
may not be sufficient. CentOS 7, for example, has symbolic links from /etc/redhat-release to /etc/centos-release. If one wishes to distinguish between Red Hat and CentOS, then idioms such as[ -f /etc/redhat-release ] && [ ! -h /etc/redhat-release ]
could be used. Similarly with others. -
SamB almost 3 yearsWhat if it's not Linux, or not using systemd?