Determine Linux distribution

11

5

I am trying to determine what flavor of linux server is running. I am not trying to determine the kernel version - but rater the distributor.

gcc is installed, and in the version output, it says RedHat

# gcc --version
gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-11)

Does this mean I can safely assume my server is a RedHat server. Is there a better - 100% bulletproof method to find this out? Also for other linux flavors - not just this server?

Edit: contents of /etc/ in case it helps - but I would like a solution that works for other versions of linux too.

# ls /etc/
./              backupmxhosts  domainusers     host.conf     localdomains  mailips        pam.d/      relayhosts               shadow              trustedmailhosts
../             bashrc*        exim.conf       inputrc       localtime     man.config     passwd      resolv.conf              skipsmtpcheckhosts  userdomains
DIR_COLORS      cron.deny      exim.pl         ld.so.cache   lynx.cfg      mtab           profile     secondarymx              spammeripblocks     vimrc
aliases         demodomains    exim.pl.local*  ld.so.conf    mail/         my.cnf         profile.d/  senderverifybypasshosts  sudoers
antivirus.exim  demouids       group           localaliases  mailhelo      nsswitch.conf  protocols   services                 termcap

I have no *release or *versionfiles in /etc/

# ls /etc*release; ls /etc/*version
/bin/ls: /etc/*release: No such file or directory
/bin/ls: /etc/*version: No such file or directory

I tried LSB

# lsb_release -a
LSB Version:
Distributor ID: n/a
Description:    (none)
Release:        n/a
Codename:       n/a

Also - I guess I am inside a chroot jail (not really sure what that is) which could likely be the cause for this issue.


Updated:

I think this does it for me. I think I can safely assume I am using cent-os.

# cat /proc/version
Linux version 2.6.9-103.plus.c4smp (mockbuild@builder10.centos.org) (gcc version 3.4.6 20060404 (Red Hat 3.4.6-11)) #1 SMP Wed Dec 21 16:17:23 EST 2011

Billy Moon

Posted 2011-10-07T21:12:31.363

Reputation: 226

Why do you need to know? Perhaps instead testing for common distribution-specific tools (package managers, for instance) will do what you want? – Daniel Pryden – 2011-10-07T21:21:56.697

1Well, what I really want to know is if I can replicate the server as a virtual machine locally, and then install stuff on local virtual machine, then copy the binaries over to the remote machine. I thought finding out the correct platform would be a good starting point. – Billy Moon – 2011-10-07T21:29:12.883

2The above directory listing is incomplete (e.g. hosts is missing, init.d, rc*). Is this a test? – None – 2011-10-07T21:38:16.910

How are you accessing this server? Is it possible that you don't have access to the server's filesystem directly, but are instead inside a chroot jail? – Daniel Pryden – 2011-10-07T21:52:48.657

I guess I could be inside a chroot jail - does this mean I can not find out what the underlying system is? – Billy Moon – 2011-10-07T22:03:03.580

Binary copy instead of using package-manager considered as very bad style! Root of this host will kill you – Lazy Badger – 2011-10-09T00:45:12.223

That is a crazy-old version of gcc. Just where did you find this server? Makes me wonder just how many known security vulnerabilities are on a machine this ancient... – sarnold – 2011-10-09T01:12:48.480

hm. speaking of package manager.. what is the package manager in place? you might be able to guess from what repos are there. – Journeyman Geek – 2011-10-10T05:13:37.093

Answers

4

cat /proc/version

Examples:

  1. Ubuntu:

    $ cat /proc/version
    Linux version 3.11.0-13-generic (buildd@roseapple) (gcc version 4.8.1 \
    (Ubuntu/Linaro 4.8.1-10ubuntu8) ) #20-Ubuntu SMP Wed Oct 23 07:38:26 UTC 2013
    
  2. Red Hat / CentOS:

    $ cat /proc/version
    Linux version 2.6.32-220.13.1.el6.x86_64 (mockbuild@c6b6.bsys.dev.centos.org) \
    (gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC) ) #1 SMP Tue Apr 17 23:56:34 BST 2012
    

See also How To Know Which Linux Distribution You Are Using?

vitaut

Posted 2011-10-07T21:12:31.363

Reputation: 161

This shows where was the kernel compiled not what distribution you run on. In many cases the kernel is being compiled on the same distribution but on an older version. – pabouk – 2013-11-20T08:48:17.110

13

On most modern Linux distros, the following command will give you its information:

lsb_release -a

Alpha01

Posted 2011-10-07T21:12:31.363

Reputation: 249

1No, this is not true. This works only for Debian and derivatives – MariusMatutiae – 2013-11-20T07:48:23.023

1This command is not installed by default on Red Hat and derivatives (i.e. CentOS). You have to install it explicitly before use: yum install redhat-lsb-core. – pabouk – 2013-11-20T09:03:38.507

@MariusMatutiae actually it is much more widespread than that but is not always installed by default. – terdon – 2013-12-19T17:06:48.020

@terdon thank you, I did not know that. – MariusMatutiae – 2013-12-19T17:09:22.740

@MariusMatutiae see here for more info, it seems to be absent from SuSe but should be installed on RH.

– terdon – 2013-12-19T17:11:29.477

@terdon I have it on my Debian/Debian-likes, I do not have it on Arch (my favorite)., where even /etc/arch-release is empty (the fact that it exists is a giveaway, though). It looks like ls /etc/*release is enough to determine the distro. – MariusMatutiae – 2013-12-19T17:16:36.900

@MariusMatutiae in some cases, it will fail on at least some non-Linux *nixes and is still not standardized. That is the way the standards seem to be going in though, yes. According to the SuSe dev quoted here: "The standard location for distribution information in the future will be /etc/os-release"

– terdon – 2013-12-19T17:21:52.160

9

/etc/issue might be a good starting point.

More could be found by inspecting the info files for different distributions:

Novell SuSE    /etc/SuSE-release
OpenSuSE       /etc/SuSE-release, /etc/os-release
Red Hat,CentOS /etc/redhat-release, /etc/redhat_version
Fedora         /etc/fedora-release, /etc/redhat-release, /etc/os-release
Slackware      /etc/slackware-release, /etc/slackware-version
Debian         /etc/debian_release, /etc/debian_version
Mandrake       /etc/mandrake-release
Yellow dog     /etc/yellowdog-release
Sun JDS        /etc/sun-release 
Solaris/Sparc  /etc/release 
Gentoo         /etc/gentoo-release
Amazon Linux   /etc/system-release
PLD Linux      /etc/pld-release, /etc/os-release
ArchLinux      /etc/arch-release, /etc/os-release

See Unix forum for more details.

Joey

Posted 2011-10-07T21:12:31.363

Reputation:

1unfortunately I don't have any files ending release or version in my etc directory – Billy Moon – 2011-10-07T21:32:19.810

4

If /etc/issue is missing, there may be one or more /etc/*_release files.

For example, my Ubuntu system has /etc/lsb-release:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=11.04
DISTRIB_CODENAME=natty
DISTRIB_DESCRIPTION="Ubuntu 11.04"

A friend's CentOS system (based on Red Hat) has /etc/redhat-release:

CentOS release 5.6 (Final)

I don't know how universal this is.

Keith Thompson

Posted 2011-10-07T21:12:31.363

Reputation: 4 645

no release or version files in my /etc/ – Billy Moon – 2011-10-07T21:34:25.787

@BillyMoon I believe it is only recent that Linux distributions started to add the lsb_release utility on their distribution. lsb_release -a – Alpha01 – 2011-10-07T21:42:58.407

Thanks for the tip - does not help me this time unfortunately – Billy Moon – 2011-10-07T22:01:39.850

2

cat /etc/issue might answer your question.

The distro makers are not friendly to you here. First, each one thinks it is the only one true way.

Most likely, instead of trying to sniff the distro, you should be looking in more detail at the particular question you actually need to answer. Focus on the functionality you are looking at, not the distro providing it. After all, a new version of a distro can turn around and go in the opposite direction on any particular axis.

bmargulies

Posted 2011-10-07T21:12:31.363

Reputation: 1 121

3there is no /etc/issue on my server – Billy Moon – 2011-10-07T21:17:47.030

Yes. Note that /etc/issue may contain escape sequences such as \n and \l. To see what these mean, man 5 issue and man getty (the /etc/issue file is primarily intended for use by getty). Of course the owner of the system could edit /etc/issue for some nefarious reason. – Keith Thompson – 2011-10-07T21:18:50.143

I don't think /etc/issue is required to exist, or to contain any information about the distribution vendor. It commonly does, but I don't think you can rely on it. – Daniel Pryden – 2011-10-07T21:20:11.020

@DanielPryden well one learns something every day. I'd never run into the absence of one myself before. – bmargulies – 2011-10-09T23:50:45.553

1

Use facter (yes that is how it is spelled).

Facter is a companion to puppet, it provides variables that can be queried which contain information about the system.

For example, here are just a few of the dozens of variables that factor can return

$ facter
...
operatingsystem => CentOS
operatingsystemmajrelease => 6
operatingsystemrelease => 6.4
osfamily => RedHat
...

You could simply query the osfamily fact to see that the server is running RedHat

$ facter osfamily
RedHat

Check out the documentation: http://puppetlabs.com/facter

spuder

Posted 2011-10-07T21:12:31.363

Reputation: 8 755

0

uname -a

Works always for any Tux

Lazy Badger

Posted 2011-10-07T21:12:31.363

Reputation: 3 557

1That reports the kernel version string, which could be very wrong if your host is running a kernel from a different distribution -- either on bare metal or because of a tool like OpenVZ or VServers. – sarnold – 2011-10-09T01:10:00.117

Yes, but I suppose in this case author of Q will know smth. about situation. "Foreign" kernel is rather odd case – Lazy Badger – 2011-10-09T01:42:58.700

0

There is no single sure fire way of doing this. I asked a similar question over on unix.se and the best way of getting this info is a script that checks a variety of sources:

#!/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}

The other choice is indeed using gcc --version, since gcc is always compiled for a particular distro, this will be relatively portable. I highly recommend you read the accepted answer on my question at U&L it explains the situation very nicely.

terdon

Posted 2011-10-07T21:12:31.363

Reputation: 45 216

0

This command worked for Debian based and Redhat based distributions: Using tr filter, you convert the document to one-word-per-line format and then count the first line which contains the distribution name.

tr -s ' \011' '\012' < /etc/issue | head -n 1

Yahya Yahyaoui

Posted 2011-10-07T21:12:31.363

Reputation: 101

-1

Thos is the best method

cat /etc/*release

Unnikrishnan

Posted 2011-10-07T21:12:31.363

Reputation: 1 193

I listed my /etc directory in the question. As discussed on other answers, this solution does not work for me. – Billy Moon – 2013-11-20T09:10:44.380

U tried ls /etcrelease; . It is wrong. Try ls /etc/release. It will work .am pretty sure – Unnikrishnan – 2013-11-20T09:41:33.963

I also did ls in /etc/ directory and you can see that no files in there end in release – Billy Moon – 2013-11-20T12:02:53.927