78

I need to find the OS name and version on a Unix/Linux platform. For this I tried the following:

  1. lsb_release utility

  2. /etc/redhat-release or specific file

But it does not seem to be the best solution, as LSB_RELEASE support is no longer for RHEL 7.

Is there a way that will work on any Unix or Linux platform?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Niraj Nandane
  • 1,318
  • 1
  • 13
  • 24
  • This problem need heuristic approach, that's why I gave you a perl's snippet to run in a shell – Gilles Quénot Nov 18 '14 at 07:34
  • 3
    lsb_release -d will work on ubuntu – Wolfgang Fahl Nov 18 '14 at 07:41
  • 4
    `uname` is in most unix environments and guaranteed to be on every LSB compliant linux distro: http://refspecs.linuxfoundation.org/LSB_2.0.1/LSB-Core/LSB-Core/command.html – technosaurus Nov 18 '14 at 07:56
  • how to get os version for eg. redhat 6.5 using uname ? – Niraj Nandane Nov 18 '14 at 08:00
  • @Niraj - By reading the manpage http://linux.die.net/man/1/uname and grokking its output (assuming it is supported in RH6.5) ... either way there is no (single) portable way to get this because it is mostly irrelevant info. Portable programs should probe for required features, not use some whitelist of prechecked distros. – technosaurus Nov 18 '14 at 08:10
  • ``lsb_release -d`` works for Red Hat too. – Pushpendra Jul 15 '16 at 01:45
  • Possible duplicate of [How do I identify the particular Linux flavor via command line?](https://stackoverflow.com/q/13036048/608639), [How to discover what Linux distribution is in use](https://stackoverflow.com/q/264290/608639), etc. – jww Oct 13 '18 at 04:51
  • ON RH and derivatives, the command `/usr/bin/lsb_release` is provided by the package 'redhat-lsb-release'. However, it isn't required by the `redhat-release` or `centos-release` packages, and so isn't installed by default. Use `yum`/`dnf` to install it, and you're good. – Samveen Jul 25 '22 at 09:40

10 Answers10

150

This work fine for all Linux environments.

#!/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 Ubuntu 12.04 (Precise Pangolin):

$ 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}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kvivek
  • 3,321
  • 1
  • 15
  • 17
  • 2
    yeah you are right but i dont want to read it from *-release file. – Niraj Nandane Nov 18 '14 at 07:00
  • i want some utility like lsb_realease – Niraj Nandane Nov 18 '14 at 07:02
  • -1 : output on my archlinux is `Linux 3.16.4-1-ARCH( 3.16.4-1-ARCH x86_64)` – Gilles Quénot Nov 18 '14 at 07:46
  • The 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 ? – Niraj Nandane Nov 18 '14 at 07:59
  • I 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 Nov 18 '14 at 08:48
  • What is the Output of this Command `cat /etc/redhat-release | sed s/.*\(// | sed s/\)//` in your RHEL 6.5 – kvivek Nov 18 '14 at 08:55
  • Note 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. – Steve Amerige Jun 02 '21 at 12:51
14

Following command worked out for me nicely. It gives you the OS name and version.

lsb_release -a
Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
10

The "lsb_release" command provides a certain Linux Standard Base (LSB) and distribution-specific information.

So using the below command we can get the Operating system name and operating system version.

"lsb_release -a"

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
5

This command gives you a description of your operating system:

cat /etc/os-release

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
YakovGdl35
  • 331
  • 3
  • 4
3

In every distribution, it has different files, so I list the most common ones:

---- CentOS Linux distribution
`cat /proc/version`

---- Debian Linux distribution
`cat /etc/debian_version`

---- Redhat Linux distribution
`cat /etc/redhat-release`

---- Ubuntu Linux distribution
`cat /etc/issue`   or   `cat /etc/lsb-release`

In the last one, /etc/issue didn't exist, so I tried the second one and it returned the right answer.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

With quotes:

cat /etc/*-release | grep "PRETTY_NAME" | sed 's/PRETTY_NAME=//g'

gives output as:

"CentOS Linux 7 (Core)"

Without quotes:

cat /etc/*-release | grep "PRETTY_NAME" | sed 's/PRETTY_NAME=//g' | sed 's/"//g'

gives output as:

CentOS Linux 7 (Core)
Saurabh Nemade
  • 1,522
  • 2
  • 11
  • 20
1

With and Linux::Distribution, the cleanest solution for an old problem:

#!/bin/sh

perl -e '
    use Linux::Distribution qw(distribution_name distribution_version);

    my $linux = Linux::Distribution->new;
    if(my $distro = $linux->distribution_name()) {
          my $version = $linux->distribution_version();
          print "you are running $distro";
          print " version $version" if $version;
          print "\n";
    } else {
          print "distribution unknown\n";
    }
'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
1

My own take at @kvivek's script, with more easily machine parsable output:

#!/bin/sh
# Outputs OS Name, Version & misc. info in a machine-readable way.
# See also NeoFetch for a more professional and elaborate bash script:
# https://github.com/dylanaraps/neofetch

SEP=","
PRINT_HEADER=false

print_help() {

    echo "`basename $0` - Outputs OS Name, Version & misc. info"
    echo "in a machine-readable way."
    echo
    echo "Usage:"
    echo "    `basename $0` [OPTIONS]"
    echo "Options:"
    echo "    -h, --help           print this help message"
    echo "    -n, --names          print a header line, naming the fields"
    echo "    -s, --separator SEP  overrides the default field-separator ('$SEP') with the supplied one"
}

# parse command-line args
while [ $# -gt 0 ]
do
    arg="$1"
    shift # past switch

    case "${arg}" in
        -h|--help)
            print_help
            exit 0
            ;;
        -n|--names)
            PRINT_HEADER=true
            ;;
        -s|--separator)
            SEP="$1"
            shift # past value
            ;;
        *) # non-/unknown option
            echo "Unknown switch '$arg'" >&2
            print_help
            ;;
    esac
done

OS=`uname -s`
DIST="N/A"
REV=`uname -r`
MACH=`uname -m`
PSUEDONAME="N/A"

GetVersionFromFile()
{
    VERSION=`cat $1 | tr "\n" ' ' | sed s/.*VERSION.*=\ // `
}

if [ "${OS}" = "SunOS" ] ; then
    DIST=Solaris
    DIST_VER=`uname -v`
    # also: cat /etc/release
elif [ "${OS}" = "AIX" ] ; then
    DIST="${OS}"
    DIST_VER=`oslevel -r`
elif [ "${OS}" = "Linux" ] ; then
    if [ -f /etc/redhat-release ] ; then
        DIST='RedHat'
        PSUEDONAME=`sed -e 's/.*\(//' -e 's/\)//' /etc/redhat-release `
        DIST_VER=`sed -e 's/.*release\ //' -e 's/\ .*//' /etc/redhat-release `
    elif [ -f /etc/SuSE-release ] ; then
        DIST=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//`
        DIST_VER=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ //`
    elif [ -f /etc/mandrake-release ] ; then
        DIST='Mandrake'
        PSUEDONAME=`sed -e 's/.*\(//' -e 's/\)//' /etc/mandrake-release`
        DIST_VER=`sed -e 's/.*release\ //' -e 's/\ .*//' /etc/mandrake-release`
    elif [ -f /etc/debian_version ] ; then
        DIST="Debian"
        DIST_VER=`cat /etc/debian_version`
    PSUEDONAME=`lsb_release -a 2> /dev/null | grep '^Codename:' | sed -e 's/.*[[:space:]]//'`
    #elif [ -f /etc/gentoo-release ] ; then
        #TODO
    #elif [ -f /etc/slackware-version ] ; then
        #TODO
    elif [ -f /etc/issue ] ; then
        # We use this indirection because /etc/issue may look like
    # "Debian GNU/Linux 10 \n \l"
        ISSUE=`cat /etc/issue`
        ISSUE=`echo -e "${ISSUE}" | head -n 1 | sed -e 's/[[:space:]]\+$//'`
        DIST=`echo -e "${ISSUE}" | sed -e 's/[[:space:]].*//'`
        DIST_VER=`echo -e "${ISSUE}" | sed -e 's/.*[[:space:]]//'`
    fi
    if [ -f /etc/UnitedLinux-release ] ; then
        DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]"
    fi
    # NOTE `sed -e 's/.*(//' -e 's/).*//' /proc/version`
    #      is an option that worked ~ 2010 and earlier
fi

if $PRINT_HEADER
then
    echo "OS${SEP}Distribution${SEP}Distribution-Version${SEP}Pseudo-Name${SEP}Kernel-Revision${SEP}Machine-Architecture"
fi
echo "${OS}${SEP}${DIST}${SEP}${DIST_VER}${SEP}${PSUEDONAME}${SEP}${REV}${SEP}${MACH}"

NOTE: Only tested on Debian 11

Example Runs

No args

osInfo

output:

Linux,Debian,10.0,buster,4.19.0-5-amd64,x86_64

Header with names and custom separator

osInfo --names -s "\t| "

output:

OS  | Distribution  | Distribution-Version  | Pseudo-Name   | Kernel-Revision   | Machine-Architecture
Linux   | Debian    | 10.0  | buster    | 4.19.0-5-amd64    | x86_64

Filtered output

osInfo | awk -e 'BEGIN { FS=","; } { print $2 " " $3 " (" $4 ")" }'

output:

Debian 10.0 (buster)
hoijui
  • 3,615
  • 2
  • 33
  • 41
1

I prepared the following commands to find concise information about a Linux system:

clear
echo "\n----------OS Information------------"
hostnamectl | grep "Static hostname:"
hostnamectl | tail -n 3
echo "\n----------Memory Information------------"
cat /proc/meminfo | grep MemTotal
echo "\n----------CPU Information------------"
echo -n "Number of core(s): "
cat /proc/cpuinfo | grep "processor" | wc -l
cat /proc/cpuinfo | grep "model name" | head -n 1
echo "\n----------Disk Information------------"
echo -n "Total Size: "
df -h --total | tail -n 1| awk '{print $2}'
echo -n "Used: "
df -h --total | tail -n 1| awk '{print $3}'
echo -n "Available: "
df -h --total | tail -n 1| awk '{print $4}'
echo "\n-------------------------------------\n"

Copy and paste in an sh file, like info.sh, and then run it using command sh info.sh.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MRazian
  • 88
  • 1
  • 13
  • While this gets information that may be useful, it doesn't answer the question (getting the OS name and version) – Cocowalla Feb 12 '20 at 13:19
1

This is the best and easiest way to find the OS name and kernel version. It is also supported in all Linux flavors.

VERSION=`cat /proc/version`
echo $VERSION
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shashank arora
  • 1,176
  • 11
  • 14