12

I'm starting to work with a box that has a custom (small) Linux build on it. How can I tell whether this is a Debian or Red Hat-based build?

This is what I know so far:

$ cat /proc/version
Linux version 2.6.31-2.5 (build@build-desktop) (gcc version 4.4.3 (Broadcom stbgcc-4.4.3-1.2) ) #7

$ apt-get
-sh: apt-get: command not found

$ yum
-sh: yum: command not found
Kevin
  • 40,767
Sparky1
  • 223
  • why lsb_release -i 2> /dev/null | sed 's/:\t/:/' | cut -d ':' -f 2- ??

    Just run lsb_release -si

    –  Oct 22 '16 at 04:24

5 Answers5

14

For testing for Debian systems, you can check whether /etc/debian_version exists:

if [ -f "/etc/debian_version" ]; then
   # do stuff
fi

It should be included in Debian and systems based on it (including Ubuntu and its derivatives), though a few may not have it — in this case, you can check all /etc/*release files:

if [ "$(grep -Ei 'debian|buntu|mint' /etc/*release)" ]; then
   # do stuff
fi

Where debian|buntu|mint is a list of distribution names to look for (not case sensitively) — you can get an idea of some common derivatives from here, though Debian derivatives like Ubuntu have their own derivatives.


For RedHat based systems, the derivatives use a larger range of files, and might not have lsb-release installed — so you can apply the following methods:

  • get the release name from

     lsb_release -i 2> /dev/null | sed 's/:\t/:/' | cut -d ':' -f 2-
    
  • check the DISTRIB-ID in the lsb-release file — a fallback method that is probably unnecessary on modern systems, also, the file apparently is missing on Fedora, and does not contain DISTRIB_ID on OpenSUSE

  • check for the existence of some of the following

    • /etc/fedora-release and/or /etc/redhat-release for Fedora or RedHat, respectively
    • /etc/SuSE-release for SuSe
    • /etc/mandriva-release for mandriva/mageia
  • use a similar method to the latter Debian one:

     if [ "$(grep -Ei 'fedora|redhat' /etc/*release)" ]; then
     ...
    

The first 3 points I sourced from the update cron of Google Chrome, so you could examine that to find out more (it also determines package managers).


For a wider range of OSs, "How to detect the OS from a Bash script?" [on Stack Overflow] should help.

Wilf
  • 2,385
3

Running uname -a should give you some general information about the system. Also, you can run apropos "package manager" or with similar keywords to hopefully find out more about the package manager. Look in /etc for a file named xyz-release where xyz should be whatever distro is running.

3

Check output of:

lsb_release -a

and:

cat /etc/issue

You can also check for more low-lever package commands rpm for RedHat and dpkg for Debian.

1

If it's an embedded device (e.g. a SoHo router), it probably won't be any of the "desktop/server" distros. I'd try to look into the device's manual, manufacturer's website (should have source to the GPL-licensed code), and the Web at large.

0

uname -a and cat /proc/version (read-only for superusers), in my honest opinion, are the only correct answers to this question as the rest - like lsb_release, /etc/issue or cat /proc/sys/kernel/version - are not available/not usable on every system (not even official Debian distros). Also don't bother using /etc/*releases, /etc/*version since they can be altered. The latter is not available on all Debian distros.

Other options could be (not my recommendation):

  • Check for dash or verify if sh is linked to dash (it should be the default for running scripts)
  • Check the package manager by executing >/dev/null 2>&1 dpkg --version && { ... }
  • uname -a and cat /proc/version provide information on the executing kernel. So e.g. within a container (e.g. Docker, lxc, etc.), these commands show information about the container's host distribution. The host may be different from the container (e.g. host is Debian, container is Arch). – Abdull Sep 11 '23 at 11:19