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

后端 未结 9 871
天涯浪人
天涯浪人 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:40

    This work fine for all Linux environment.

    #!/bin/sh
    cat /etc/*-release
    

    In Ubuntu:

    $ cat /etc/*-release
    DISTRIB_ID=Ubuntu
    DISTRIB_RELEASE=10.04
    DISTRIB_CODENAME=lucid
    DISTRIB_DESCRIPTION="Ubuntu 10.04.4 LTS"
    

    or 12.04:

    $ cat /etc/*-release
    
    DISTRIB_ID=Ubuntu
    DISTRIB_RELEASE=12.04
    DISTRIB_CODENAME=precise
    DISTRIB_DESCRIPTION="Ubuntu 12.04.4 LTS"
    NAME="Ubuntu"
    VERSION="12.04.4 LTS, Precise Pangolin"
    ID=ubuntu
    ID_LIKE=debian
    PRETTY_NAME="Ubuntu precise (12.04.4 LTS)"
    VERSION_ID="12.04"
    

    In RHEL:

    $ cat /etc/*-release
    Red Hat Enterprise Linux Server release 6.5 (Santiago)
    Red Hat Enterprise Linux Server release 6.5 (Santiago)
    

    Or Use this Script:

    #!/bin/sh
    # Detects which OS and if it is Linux then it will detect which Linux
    # Distribution.
    
    OS=`uname -s`
    REV=`uname -r`
    MACH=`uname -m`
    
    GetVersionFromFile()
    {
        VERSION=`cat $1 | tr "\n" ' ' | sed s/.*VERSION.*=\ // `
    }
    
    if [ "${OS}" = "SunOS" ] ; then
        OS=Solaris
        ARCH=`uname -p` 
        OSSTR="${OS} ${REV}(${ARCH} `uname -v`)"
    elif [ "${OS}" = "AIX" ] ; then
        OSSTR="${OS} `oslevel` (`oslevel -r`)"
    elif [ "${OS}" = "Linux" ] ; then
        KERNEL=`uname -r`
        if [ -f /etc/redhat-release ] ; then
            DIST='RedHat'
            PSUEDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//`
            REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//`
        elif [ -f /etc/SuSE-release ] ; then
            DIST=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//`
            REV=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ //`
        elif [ -f /etc/mandrake-release ] ; then
            DIST='Mandrake'
            PSUEDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//`
            REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//`
        elif [ -f /etc/debian_version ] ; then
            DIST="Debian `cat /etc/debian_version`"
            REV=""
    
        fi
        if [ -f /etc/UnitedLinux-release ] ; then
            DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]"
        fi
    
        OSSTR="${OS} ${DIST} ${REV}(${PSUEDONAME} ${KERNEL} ${MACH})"
    
    fi
    
    echo ${OSSTR}
    
    0 讨论(0)
  • 2020-12-02 07:42

    With perl and Linux::Distribution, the cleanest solution for an old problem :

    #!/bin/sh
    
    perl -e '
        use Linux::Distribution qw(distribution_name distribution_version);
    
        my $linux = Linux::Distribution->new;
        if(my $distro = $linux->distribution_name()) {
              my $version = $linux->distribution_version();
              print "you are running $distro";
              print " version $version" if $version;
              print "\n";
        } else {
              print "distribution unknown\n";
        }
    '
    
    0 讨论(0)
  • The "lsb_release" command provides certain Linux Standard Base and distribution-specific information. So using the below command we can get Operating system name and operating system version.

    "lsb_release -a"

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

    I prepared following commands to find concise information about a Linux system:

    clear
    echo "\n----------OS Information------------"
    hostnamectl | grep "Static hostname:"
    hostnamectl | tail -n 3
    echo "\n----------Memory Information------------"
    cat /proc/meminfo | grep MemTotal
    echo "\n----------CPU Information------------"
    echo -n "Number of core(s): "
    cat /proc/cpuinfo | grep "processor" | wc -l
    cat /proc/cpuinfo | grep "model name" | head -n 1
    echo "\n----------Disk Information------------"
    echo -n "Total Size: "
    df -h --total | tail -n 1| awk '{print $2}'
    echo -n "Used: "
    df -h --total | tail -n 1| awk '{print $3}'
    echo -n "Available: "
    df -h --total | tail -n 1| awk '{print $4}'
    echo "\n-------------------------------------\n"
    

    Copy and paste in an sh file like info.sh and then run it using command sh info.sh

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

    Following command worked out for me nicely. It gives you the OS name and version.

    lsb_release -a
    
    0 讨论(0)
  • 2020-12-02 07:48

    With quotes:

    cat /etc/*-release | grep "PRETTY_NAME" | sed 's/PRETTY_NAME=//g'
    

    gives output as:

    "CentOS Linux 7 (Core)"
    

    Without quotes:

    cat /etc/*-release | grep "PRETTY_NAME" | sed 's/PRETTY_NAME=//g' | sed 's/"//g'
    

    gives output as:

    CentOS Linux 7 (Core)
    
    0 讨论(0)
提交回复
热议问题