Determining WHICH *nix distro is currently used…

–Edit.
Originally, I had run into the issue of trying to determine the *nix distribution – a command that would tell me the distribution and the build and everythign else I wanted to know. After reviewing some of the awesome comments left behind, here’s the updated perl script to determine the distribution (in Perl).

#!/usr/bin/env perl
use strict;
use warnings;

my ( $distro );
$distro = `lsb_release -i -s`;
chomp $distro;

my %installCmd = (
"Fedora" => \&fedora,
"Ubuntu" => \&ubuntu,
);

&test();

sub test {
if ( exists $installCmd{$distro} ) {
$installCmd{$distro}->();
} else {
print "Supported Distributions are: ". join(', ' , keys %installCmd) . ".\n";
exit;
}
}

sub fedora { print "Hello from the Fedora Family!"; }
sub ubuntu { print "Hello from UBUNTU!!!"; }

18 thoughts on “Determining WHICH *nix distro is currently used…

  1. Jay Scott Raymond says:

    Determining distro:

    Try:

    cat /etc/issue

    • /etc/issue is typically the banner for ssh logins, and some people may find a need to change that banner to either a) mask what os they’re running or b) write a disclaimer or something for those logging into the system.

      falls under UserError, imo.

  2. Christian says:

    uname -v does display ‘Ubuntu’ as part of the kernel version. What about Fedora?

  3. cHagHi says:

    Try parsing the contents of /etc/lsb-release

  4. etank says:

    lsb_release -a

    On a Fedora box would give you something like this:

    LSB Version: :core-3.1-ia32:core-3.1-noarch:core-3.2-ia32:core-3.2-noarch:desktop-3.1-ia32:desktop-3.1-noarch:desktop-3.2-ia32:desktop-3.2-noarch
    Distributor ID: Fedora
    Description: Fedora release 12 (Constantine)
    Release: 12
    Codename: Constantine

    The same command will work on Ubuntu / Debian too.

    • etank, that’s great. I didn’t even consider the lsb_release (underline)…kept trying the lsb-release in fedora, since the /etc/lsb-release in ubuntu…

      Works great, thanks mate!

  5. etank says:

    On Fedora if you do:

    uname -a

    You get something like:

    Linux carter 2.6.31.6-162.fc12.i686.PAE #1 SMP Fri Dec 4 00:43:59 EST 2009 i686 i686 i386 GNU/Linux

    Notice the fc12 in the string. That means I am running Fedora 12.

    • ya, i’m not looking for the fc12 or anything like that…I want something that will be uniform throughout, across all platforms of linux (preferably).

      will the lsb_release -a work with slax, mandrake, gentoo and other distros as well?

      • NoOne says:

        It should. -a gives you everything though, for just the distributor, you want -i (see man page for other options)

  6. Julien says:

    Facter will give you a lot of informations, you need facter anyway if you use puppet… you use puppet, right ?
    ;-)

  7. Julien says:

    Give it a shot, especially with what you’re trying to achieve (modular, cross distribution setup/config management).

    Example run here :

    Facter : http://pastebin.com/f5a2fae61
    Puppet : http://bitfieldconsulting.com/agile-sysadmin

  8. agfitzp says:

    Use the -s flag to get the short answer:

    alex@dv9000-ubuntu:~$ lsb_release -i -s
    Ubuntu

  9. Rajasun says:

    For Ubuntu,
    cat /proc/version
    and of course
    cat /etc/lsb-release

    And cat /etc/debian_version though this may kinda confused some :P

  10. Azrael says:

    “cat /etc/*release” should work on most Linux distros and will work on most Unixes.

Leave a reply to Christian Cancel reply