IT TIP

Unix / Linux 플랫폼에서 OS 이름과 버전을 찾는 가장 좋은 방법

itqueen 2020. 10. 31. 10:21
반응형

Unix / Linux 플랫폼에서 OS 이름과 버전을 찾는 가장 좋은 방법


Unix / Linux 플랫폼에서 OS 이름과 버전을 찾아야합니다. 이를 위해 다음을 시도했습니다.

  1. lsb_release 유용

  2. /etc/redhat-release 또는 특정 파일

그러나 LSB_RELEASE 지원이 더 이상 RHEL 7에 대해 지원되지 않으므로 최상의 솔루션이 아닌 것 같습니다.

Unix 또는 Linux 플랫폼에서 작동하는 방법이 있습니까?


이것은 모든 Linux 환경에서 잘 작동합니다.

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

Ubuntu에서 :

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

또는 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"

RHEL에서 :

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

또는이 스크립트 사용 :

#!/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}

다음 명령은 나를 위해 잘 작동했습니다. OS 이름과 버전을 제공합니다.

lsb_release -a

"lsb_release"명령은 특정 Linux Standard Base 및 배포 별 정보를 제공합니다. 따라서 아래 명령을 사용하여 운영 체제 이름과 운영 체제 버전을 얻을 수 있습니다.

" lsb_release -a "


리눅스 :: 유통 , 오래된 문제에 대한 가장 깨끗한 해결책 :

#!/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";
    }
'

모든 배포에는 다른 파일이 있으므로 가장 일반적인 파일을 작성합니다.

---- 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`

마지막으로 / etc / issue가 존재하지 않았으므로 두 번째를 시도했는데 정답을 반환했습니다.


더 쉽게 기계 구문 분석 가능한 출력으로 @kvivek의 스크립트를 직접 가져 왔습니다 .

#!/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}"

참고 : Debian 11에서만 테스트되었습니다.

예제 실행

인수 없음

osInfo

산출:

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

이름 및 사용자 지정 구분 기호가있는 헤더

osInfo --names -s "\t| "

산출:

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

필터링 된 출력

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

산출:

Debian 10.0 (buster)

이 명령은 운영 체제에 대한 설명을 제공합니다.

cat /etc/os-release


다음 명령을 사용하십시오.

lsb_release -irc

내 OS는 CentO이고

Distributor ID: CentOS
Description: CentOS Linux release 7.2.1511 (Core)
Release: 7.2.1511
Codename: Core

따옴표 포함 :

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

출력을 다음과 같이 제공합니다.

"CentOS Linux 7 (Core)"

인용없이:

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

출력을 다음과 같이 제공합니다.

CentOS Linux 7 (Core)

Linux 시스템에 대한 간결한 정보를 찾기 위해 다음 명령을 준비했습니다.

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"

info.sh와 같은 sh 파일에 복사하여 붙여 넣은 다음 sh info.sh 명령을 사용하여 실행하십시오.

참고 URL : https://stackoverflow.com/questions/26988262/best-way-to-find-os-name-and-version-in-unix-linux-platform

반응형