Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2264
  • Last Modified:

detecting version of linux

We have a shell script that currently asks if the user is running Debian, Red Hat, or SUSE, and adapts a makefile accordingly. (I think Red Hat may be the oddman out.)  We'd like to determine what distribution of Linux they're running.  

A short and acceptable (and wonderful) answer might be a snippet of shell script (including the #! at the top) that should run on all those flavors of Linux, and do the follow pseudocode: (I think the existing script runs under ksh and bash? Does Linux not usually come with ksh?)

  echo redhat
  echo suse
  echo debian

I'm not sure what the most reliable way of detecting dist is; though gcc -v seems to work.  But then I'm not sure how to redirect it's stderr to stdout under ksh and/or bash, and my shell scripting is weak enough that I don't know the easiest way of checking the result of that grep, so any advice on that would be appreciated as well...

1 Solution
Why not use methods that will work regardless of distribution.

A few hints;
1) Use Bourn shell (#!/bin/sh) (even though some systems have /bin/sh being a link to /bin/bash, bash will then act in 'sh' compability mode)
Properly written your script should/will work not only in Linux but in Solaris, AIX, QNX, *BSD, SGI etc etc

2) The dependencies of what you attempt to install can be checked within your script regardless of dirstribution (don't assume users have left the system "intact" after installation...)
A true Linux user have most likely choosen Linux for this reason. To be able to tailor it to hell(tm), and they will ;-)

3) Don't assume anything, test for everything (or the user (assume they are more experienced than you are) will get back at you with a vengeance if you trash up their system.)

To answer your script related questions;
(think /bin/sh from now on!)

Redirecting stderr to stdout you can usually do:
command 2>&1 | grep ..

unix# gcc -v 2>&1 | awk '{  s=split($4,r,"/"); for (n=1;n<=s;n++) if ( r[n] ~ /[l|L]inux/) print r[n] ; exit}'

There is no single way to detect distribution reliably. Asking the user may be the best option if you really must know what distribution was used during install.
Though the distribution may not be as interesting as the  kernel version, glibc and other libraries existance/versions... depending on what you attempt to achive of course.

I think the RedHat installation leaves a file behind (/etc/redhat-release) that contains distribution name and version.

Yes, some distributions do leave those files there.  Examples:


But in concurrence with bummerlord, I certainly wouldn't rely on this.
On RedHat you can do "cat /proc/version"; don't know 'bout the others
szaboc, that has nothing to do with distributions and should be the same for all distributions with a reasonably recent kernel.
As you know the proc filesystem is a virtual filesystem provided by Linux (Linux is really only the kernel) as a link to read/alter some parameters, either by humans or user space programs.

/usr/bin/lsb_release -a

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now