Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (18.4k points)

I wanted to find my OS name and version on Unix/Linux platform. So I tried the following:

  1. lsb_release utility
  2. /etc/redhat-release or specific file

I think it is not the best solution as LSB_RELEASE support is no longer for RHEL 7.

Wanted to know another way that will work on any Unix or Linux platform?

1 Answer

0 votes
by (36.8k points)

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}

Want to be a Linux expert? Come and join this Linux course

 

Related questions

0 votes
1 answer
asked Jan 26, 2021 in Linux by dev_sk2311 (45k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...