Best way to find the OS name and version on a Unix/Linux platform
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?
uname is in most unix environments and guaranteed to be on every LSB compliant linux distro: refspecs.linuxfoundation.org/LSB_2.0.1/LSB-Core/LSB-Core/…
@Niraj — By reading the manpage 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.
10 Answers 10
This work fine for all Linux environments.
$ cat /etc/*-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=10.04 DISTRIB_CODENAME=lucid DISTRIB_DESCRIPTION="Ubuntu 10.04.4 LTS"
$ 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"
$ cat /etc/*-release Red Hat Enterprise Linux Server release 6.5 (Santiago) Red Hat Enterprise Linux Server release 6.5 (Santiago)
#!/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 [ "$" = "SunOS" ] ; then OS=Solaris ARCH=`uname -p` OSSTR="$ $($ `uname -v`)" elif [ "$" = "AIX" ] ; then OSSTR="$ `oslevel` (`oslevel -r`)" elif [ "$" = "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="$[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]" fi OSSTR="$ $ $($ $ $)" fi echo $
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 ?
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)
Following command worked out for me nicely. It gives you the OS name and version.
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«
This command gives you a description of your operating system:
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.
With quotes:
cat /etc/*-release | grep "PRETTY_NAME" | sed 's/PRETTY_NAME=//g'
Without quotes:
cat /etc/*-release | grep "PRETTY_NAME" | sed 's/PRETTY_NAME=//g' | sed 's/"//g'
@PeterMortensen in /etc/*-release file original value is stored with double quotes. if you want to save it in database most probably you would want to remove those double quotes as well. 🙂
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 "$" 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 [ "$" = "SunOS" ] ; then DIST=Solaris DIST_VER=`uname -v` # also: cat /etc/release elif [ "$" = "AIX" ] ; then DIST="$" DIST_VER=`oslevel -r` elif [ "$" = "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 "$" | head -n 1 | sed -e 's/[[:space:]]\+$//'` DIST=`echo -e "$" | sed -e 's/[[:space:]].*//'` DIST_VER=`echo -e "$" | sed -e 's/.*[[:space:]]//'` fi if [ -f /etc/UnitedLinux-release ] ; then 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$Distribution$Distribution-Version$Pseudo-Name$Kernel-Revision$Machine-Architecture" fi echo "$$$$$$$$$$$"
NOTE: Only tested on Debian 11
Example Runs
No args
Linux,Debian,10.0,buster,4.19.0-5-amd64,x86_64
Header with names and custom separator
OS | Distribution | Distribution-Version | Pseudo-Name | Kernel-Revision | Machine-Architecture Linux | Debian | 10.0 | buster | 4.19.0-5-amd64 | x86_64
Filtered output
5 Linux Commands to Check Distro Name and Version
When working with an unknown server, the first task of a sys admin is to gather some information about the system, like what OS is it running, what version, what services are running and so on.
And there is no single command that can detect distribution specific information consistently across all linux distributions.
The command is different across Debian, CentOS and ArchLinux.
So in this post we are listing out some common commands that are used to detect distro specific information on linux. This includes the distro name and version.
1. lsb_release
The lsb_release command prints out distribution specific information about a linux distro.
On Ubuntu/debian based systems the command is available by default.
$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 11.04 Release: 11.04 Codename: natty
The lsb_release command is also available on CentOS/Fedora based systems, if the lsb core packages are installed.
# lsb_release -a LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch Distributor ID: CentOS Description: CentOS release 6.4 (Final) Release: 6.4 Codename: Final
2. /etc/*-release files
The /etc directory contains a couple of files that contains information about the distribution. The following files are present on Ubuntu/Debian based systems.
/etc/issue /etc/issue.net /etc/lsb-release /etc/os-release
$ cat /etc/issue Ubuntu 13.10 \n \l
$ cat /etc/issue.net Ubuntu 13.10
$ cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=13.10 DISTRIB_CODENAME=saucy DISTRIB_DESCRIPTION="Ubuntu 13.10"
$ cat /etc/os-release NAME="Ubuntu" VERSION="13.10, Saucy Salamander" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 13.10" VERSION_ID="13.10" HOME_URL="http://www.ubuntu.com/" SUPPORT_URL="http://help.ubuntu.com/" BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
The file os-release contains a whole lot of information about the system.
CentOS/Fedora based systems contain similar files but with different names.
/etc/centos-release /etc/lsb-release /etc/redhat-release /etc/system-release
# cat /etc/centos-release CentOS release 6.4 (Final)
# cat /etc/lsb-release LSB_VERSION=base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
# cat /etc/redhat-release CentOS release 6.4 (Final)
# cat /etc/system-release CentOS release 6.4 (Final)
Note that the lsb specific command and files are not present on CentOS by default. The redhat-lsb-core packages have to be installed to make lsb available. The /etc/lsb-release file does not print the distro information in a simple format.
Fedora contains the /etc/os-release file, similar to ubuntu
$ cat /etc/os-release NAME=Fedora VERSION="18 (Spherical Cow)" ID=fedora VERSION_ID=18 PRETTY_NAME="Fedora 18 (Spherical Cow)" ANSI_COLOR="0;34" CPE_NAME="cpe:/o:fedoraproject:fedora:18"
3. cat /proc/version
The /proc/version file contains information about the kernel and some indication about the distro.
On a typical Ubuntu system the contents look like this
$ cat /proc/version Linux version 2.6.38-13-generic ([email protected]) (gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4) ) #52-Ubuntu SMP Tue Nov 8 16:53:51 UTC 2011
On a typical CentOS system the output looks as follows
# cat /proc/version Linux version 2.6.32-358.11.1.el6.x86_64 ([email protected]) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC) ) #1 SMP Wed Jun 12 03:34:52 UTC 2013
As can be seen above, the version information about the distro is not very clear, although it might be possible to deduce the distro in use.
Output on a RHEL 5 system
# cat /proc/version Linux version 2.6.18-028stab070.14 ([email protected]) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)) #1 SMP Thu Nov 18 16:04:02 MSK 2010
4. uname -a
The uname command can also indicate which linux distro is in use, but gives very little information about it.
On Ubuntu, uname can clearly indicate the distribution name.
$ uname -a Linux enlightened-desktop 2.6.38-13-generic #52-Ubuntu SMP Tue Nov 8 16:53:51 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux
However on CentOS, the exact distro name is not revealed.
# uname -a Linux dhcppc3 2.6.32-358.11.1.el6.x86_64 #1 SMP Wed Jun 12 03:34:52 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
Instead it reports the base distro name EL (Redhat).
Portable command
The following is an attempt to get a portable command for checking distro info across different linux systems.
$ cat /etc/[A-Za-z]*[_-][rv]e[lr]* squeeze/sid DISTRIB_ID=Ubuntu DISTRIB_RELEASE=11.04 DISTRIB_CODENAME=natty DISTRIB_DESCRIPTION="Ubuntu 11.04"
A simpler approach to make a portable command would be like this
$ lsb_release -a || cat /etc/redhat-release || cat /etc/*-release || cat /etc/issue
If one option fails, the command moves to the next one, until one of them works. The above command is not a well tested one and is expected to work fine only on ubuntu/debian and centos/fedora based systems.
That would print all unique lines from all /etc/*-release files. Works well on most distros.
A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected] .
One Comment
- Tony August 10, 2021 at 10:10 pm Hi, Nice article.
I personaly use this command in one of my scripts: (lsb_release -a 2>/dev/null || cat /etc/redhat-release || cat /etc/*-release || cat /etc/issue) | grep -i “centos\|ubuntu\|red *hat\|fedora\|debian\|slackware\|suse\|arch\|deepin\|kubuntu\|alpine” -o | sort -f | uniq -i -u It’s not getting all the Distributions out there, but enough for my use. Cheers,
Tony