Best way to find os name and version in Unix/Linux platform

后端 未结 9 872
天涯浪人
天涯浪人 2020-12-02 07:33

I need to find the OS name and version on Unix/Linux platform. For this I tried following:

  1. lsb_release utility

  2. /etc/redha

相关标签:
9条回答
  • 2020-12-02 07:55

    this command gives you a description of your operating system

    cat /etc/os-release

    0 讨论(0)
  • 2020-12-02 07:55

    My own take at @kvivek's script, with more easily machine parsable output:

    #!/bin/sh
    # Outputs OS Name, Version & misc. info in a machine-readable way.
    # See also NeoFetch for a more professional and elaborate bash script:
    # https://github.com/dylanaraps/neofetch
    
    SEP=","
    PRINT_HEADER=false
    
    print_help() {
    
        echo "`basename $0` - Outputs OS Name, Version & misc. info"
        echo "in a machine-readable way."
        echo
        echo "Usage:"
        echo "    `basename $0` [OPTIONS]"
        echo "Options:"
        echo "    -h, --help           print this help message"
        echo "    -n, --names          print a header line, naming the fields"
        echo "    -s, --separator SEP  overrides the default field-separator ('$SEP') with the supplied one"
    }
    
    # parse command-line args
    while [ $# -gt 0 ]
    do
        arg="$1"
        shift # past switch
    
        case "${arg}" in
            -h|--help)
                print_help
                exit 0
                ;;
            -n|--names)
                PRINT_HEADER=true
                ;;
            -s|--separator)
                SEP="$1"
                shift # past value
                ;;
            *) # non-/unknown option
                echo "Unknown switch '$arg'" >&2
                print_help
                ;;
        esac
    done
    
    OS=`uname -s`
    DIST="N/A"
    REV=`uname -r`
    MACH=`uname -m`
    PSUEDONAME="N/A"
    
    GetVersionFromFile()
    {
        VERSION=`cat $1 | tr "\n" ' ' | sed s/.*VERSION.*=\ // `
    }
    
    if [ "${OS}" = "SunOS" ] ; then
        DIST=Solaris
        DIST_VER=`uname -v`
        # also: cat /etc/release
    elif [ "${OS}" = "AIX" ] ; then
        DIST="${OS}"
        DIST_VER=`oslevel -r`
    elif [ "${OS}" = "Linux" ] ; then
        if [ -f /etc/redhat-release ] ; then
            DIST='RedHat'
            PSUEDONAME=`sed -e 's/.*\(//' -e 's/\)//' /etc/redhat-release `
            DIST_VER=`sed -e 's/.*release\ //' -e 's/\ .*//' /etc/redhat-release `
        elif [ -f /etc/SuSE-release ] ; then
            DIST=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//`
            DIST_VER=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ //`
        elif [ -f /etc/mandrake-release ] ; then
            DIST='Mandrake'
            PSUEDONAME=`sed -e 's/.*\(//' -e 's/\)//' /etc/mandrake-release`
            DIST_VER=`sed -e 's/.*release\ //' -e 's/\ .*//' /etc/mandrake-release`
        elif [ -f /etc/debian_version ] ; then
            DIST="Debian"
            DIST_VER=`cat /etc/debian_version`
        PSUEDONAME=`lsb_release -a 2> /dev/null | grep '^Codename:' | sed -e 's/.*[[:space:]]//'`
        #elif [ -f /etc/gentoo-release ] ; then
            #TODO
        #elif [ -f /etc/slackware-version ] ; then
            #TODO
        elif [ -f /etc/issue ] ; then
            # We use this indirection because /etc/issue may look like
        # "Debian GNU/Linux 10 \n \l"
            ISSUE=`cat /etc/issue`
            ISSUE=`echo -e "${ISSUE}" | head -n 1 | sed -e 's/[[:space:]]\+$//'`
            DIST=`echo -e "${ISSUE}" | sed -e 's/[[:space:]].*//'`
            DIST_VER=`echo -e "${ISSUE}" | sed -e 's/.*[[:space:]]//'`
        fi
        if [ -f /etc/UnitedLinux-release ] ; then
            DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]"
        fi
        # NOTE `sed -e 's/.*(//' -e 's/).*//' /proc/version`
        #      is an option that worked ~ 2010 and earlier
    fi
    
    if $PRINT_HEADER
    then
        echo "OS${SEP}Distribution${SEP}Distribution-Version${SEP}Pseudo-Name${SEP}Kernel-Revision${SEP}Machine-Architecture"
    fi
    echo "${OS}${SEP}${DIST}${SEP}${DIST_VER}${SEP}${PSUEDONAME}${SEP}${REV}${SEP}${MACH}"
    

    NOTE: Only tested on Debian 11

    Example Runs

    No args

    osInfo
    

    output:

    Linux,Debian,10.0,buster,4.19.0-5-amd64,x86_64
    

    Header with names and custom separator

    osInfo --names -s "\t| "
    

    output:

    OS  | Distribution  | Distribution-Version  | Pseudo-Name   | Kernel-Revision   | Machine-Architecture
    Linux   | Debian    | 10.0  | buster    | 4.19.0-5-amd64    | x86_64
    

    Filtered output

    osInfo | awk -e 'BEGIN { FS=","; } { print $2 " " $3 " (" $4 ")" }'
    

    output:

    Debian 10.0 (buster)
    
    0 讨论(0)
  • 2020-12-02 08:02

    In every distribute it has difference files so I write most common ones:

    ---- CentOS Linux distro
    `cat /proc/version`
    ---- Debian Linux distro
    `cat /etc/debian_version`
    ---- Redhat Linux distro
    `cat /etc/redhat-release` 
    ---- Ubuntu Linux distro
    `cat /etc/issue`   or   `cat /etc/lsb-release`
    

    in last one /etc/issue didn't exist so I tried the second one and it returned the right answer

    0 讨论(0)
提交回复
热议问题