User Rating: 3 / 5

Star ActiveStar ActiveStar ActiveStar InactiveStar Inactive
I just faced an issue I thought should be easy to solve. I wanted to know which Linux distribution a bash script runs on. There exists a proposal of the FSB (Free Standards Group) to implement lsb_release which delivers all the info.Unfortunately it's not implemented by all Linux distributions so I installed various distributions on VMWare to get a clue how to write code to extract the Linux distribution. Finally I wrote a bash script and python script to extract the Linux distribution.


lsb_release would be the easiest way to find out the Linux distribution. According FSG there can exist two files on a Linux distribution:
1) /etc/lsb-release
2) /etc/distribution-release
Unfortunately lsb_release is not available on all distributions and the files in /etc are not following the rules. It seems all naming variations of the config file can be found with 
ls /etc/*[-_]{release,version} 


Finally I wrote following bash script and this python script to use all information available to derive the Linux distribution. It either uses lsb_release to find out the distro or extracts the distro name from the existing /etc/*[-_]{release,version} file by using the string starting after /etc/ and ending with - or _. Example: /etc/fedora-release exists => distro is fedora
The script was tested on a lot of distributions but will also work on most other distributions. Pls let me know if you run it successfully on another Linux distro and I update the following list of tested distros:
# opensuse             openSuSE 11.0 (no lsb_release) and 11.2 (lsb_release)
# fedora                    Fedora 12
# centos                   CentOS 5.4
# kubuntu                 Kubuntu 9.10
# debian                   Debian 5.0.3
# arch                        Arch
# slackware             Slackware
# mandriva               Mandriva 2009.1
# debian                   Knoppix 6.2
# linuxmint               Mint 8
For the most recent list of tested distributions see header comment in source code.
Welche Distribution besitze ich (Which distribution do I have) : List of files in /etc/ directory used by various Linux distributions
lsb_release man page :  Man page of the Free Standards Group
/etc/release equivalents for sundry Linux (and other Unix) distributions : List of files in /etc/ directory used by various Linux distributions
Other implementations:
  • tisu suggested following single liner which is a codesnipet from the
DISTRO=`grep -ihs "buntu\|SUSE\|Fedora\|PCLinuxOS\|MEPIS\|Mandriva\|Debian\|Damn\|Sabayon\|Slackware\|KNOPPIX\|Gentoo\|Zenwalk\|Mint\|Kubuntu\|FreeBSD\|Puppy\|Freespire\|Vector\|Dreamlinux\|CentOS\|Arch\|Xandros\|Elive\|SLAX\|Red\|BSD\|KANOTIX\|Nexenta\|Foresight\|GeeXboX\|Frugalware\|64\|SystemRescue\|Novell\|Solaris\|BackTrack\|KateOS\|Pardus" /etc/{issue,*release,*version}` 
  • inxi contains a function which extracts the distro
Add comment

*** Note ***

Comments are welcome. But in order to reject spam posts please consider following rules:
  1. Comments with string http are rejected with message You have no rights to use this tag
  2. All comments are reviewed by hand and thus it usually takes one day until a comment will be published.