Contents

Finding the OS of a Linux system in bash script

Contents

Introduction

Sometimes, you need to figure out what type of system you’re running on during a script. A really good example of this is when interfacing with yum or apt, or adding selinux configurations.

This can be difficult :/

Enter /etc/os-release a really handy file that can be included into any shell script . /etc/os-release or bash script source /etc/os-release to determine common OS attributes. Information such as the distribution name, version and URLs to maintainers can be found in here, hurray!

Example /etc/os-release of a CentOS 7 System:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

Sadly, CentOS 6 and RHEL 6 don’t have this file, because they’re difficult and want to be special, but they do have /etc/redhat-release which is enough 9/10 times.

find_os()?

I’ve created this handy shell function to get the OS version of Centos, Ubuntu and Debian server OS, with more to come as needed. Simply reference OS-DISTRO and OS_VERSION_ID in your script when needed!

It’s licensed under GPL-V.3, so have at it, but do give it some love on GitHub :)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
#
# - This function can detect the operating system for a target Linux Machine. 
# - This function is aimed at server operating systems, Tested on: Centos 6 -5, Ubuntu 14-20, Debian 8-10
findOS () {
#Get OS distro
if [ -f "/etc/os-release" ]; then
    . "/etc/os-release"
    case "$NAME" in
        # Matches RHEL 7
        "Red Hat Enterprise Linux Server" ) OS_DISTRO="rhel" ;;
        # Matches CentOS 7 and 8
        "CentOS Linux" ) OS_DISTRO="centos" ;;
        # Matches Ubuntu 16.04 18.04 20.04
        "Ubuntu" ) OS_DISTRO="ubuntu" ;;
        # Matches Debian 8 9
        "Debian GNU/Linux" ) OS_DISTRO="debian" ;;
        * ) OS_DISTRO="invalid-or-unknown" ;;
    esac
    if [ "$OS_DISTRO" == "centos" ] || [ "$OS_DISTRO" == "rhel" ]; then
        case "$VERSION_ID" in
            7* ) OS_VERSION_ID="7";;
            8* ) OS_VERSION_ID="8";;
            * ) OS_VERSION_ID="invalid-or-too-old" ;;
        esac
    fi
    if [ "$OS_DISTRO" == "ubuntu" ]; then
        case "$VERSION_ID" in
            12* ) OS_VERSION_ID="12";;
            14* ) OS_VERSION_ID="14";;
            16* ) OS_VERSION_ID="16";;
            18* ) OS_VERSION_ID="18";;
            20* ) OS_VERSION_ID="20";;
            * ) OS_VERSION_ID="invalid-or-too-old" ;;
        esac
    fi
    if [ "$OS_DISTRO" == "debian" ]; then
        case "$VERSION_ID" in
            7* ) OS_VERSION_ID="7";;
            8* ) OS_VERSION_ID="8";;
            9* ) OS_VERSION_ID="9";;
            10* ) OS_VERSION_ID="10";;
            * ) OS_VERSION_ID="invalid-or-too-old" ;;
        esac
    fi
elif [ -f "/etc/centos-release" ]; then
#CentOS 6 doesnt have an os-release file, so doing this manually using the centos-release file
    if (grep "^CentOS release 6" /etc/centos-release >> /dev/null) then
        OS_DISTRO="centos"
        OS_VERSION_ID="6"
    fi
elif [ -f "/etc/redhat-release" ]; then
#RHEL 6 doesnt have an os-release file, so doing this manually using the redhat-release file
    if (grep "^Red Hat Enterprise Linux Server release 6" /etc/redhat-release >> /dev/null) then
        OS_DISTRO="rhel"
        OS_VERSION_ID="6"
    fi
else
    bool_success="False"
    echo "[Error] Could not determine how to detect OS version. os-release or centos/redhat-release files not present."
    exit 1
fi
if [ "$DEBUG" == "True" ]; then
    echo "[Info] Detected OS $OS_DISTRO $OS_VERSION_ID"
fi
}