确认Linux发行版本和类型
首先
在Linux上存在许多发行版。尽管企业系统使用的选择受限,但仍存在几种不同的类型。由于各个发行版对判定方法略有差异,因此我对主要的发行版进行了调查。
当初我考虑着要调查云服务的差别,但实际上并没有依赖于云服务的部分。
1-1. 激发动力和目标读者
Linuxディストリビューションの判定方法が、ディストリビューションによって異なる
ディストリビューションを調査し、共通の判別方法を調べる
1-2. 目标环境
RHEL系Linux OS(RHEL, CentOS, CentOS Stream, Amazon Linux, Oracle Linux, etc)
Ubuntu
オンプレミス&クラウド
2. 鉴别发行版的基本方法
ディストリビューションを調査し、共通の判別方法を調べる
-
RHEL系Linux OS(RHEL, CentOS, CentOS Stream, Amazon Linux, Oracle Linux, etc)
Ubuntu
オンプレミス&クラウド
2. 鉴别发行版的基本方法
基本上,要判別Linux发行版的类型,可以查看/etc/*release文件。以下是一些代表性的文件。
ディストリビューション共通
/etc/os-release
/etc/system-release
ディストリビューション固有
/etc/redhat-release
/etc/centos-release
/etc/oracle-release
例如,如果是CentOS操作系统,只需要查看/etc/centos-release文件即可。如果显示如下内容,则可以确认为CentOS 8.1。
CentOS Linux release 8.1.1911 (Core)
然而,除了CentOS以外,没有/etc/centos-release文件。因此,需要根据发行版更改要参考的文件。另外,虽然/etc/os-release和/etc/system-release是独立于发行版的文件,但它们缺乏一致性。
3. 每个发行版的调研
本次调查将针对以下发行版进行。RHEL系列是基于8版本的,但6和7版本基本上是相同的。
-
Red Hat Enterprise Linux
CentOS / CentOS Stream
Oracle Linux
Amazon Linux
Ubuntu
3-1. 红帽企业版Linux。
有三个文件,/etc/system-release是/etc/redhat-release的符号链接。
$ ls -l /etc/*release
lrwxrwxrwx. 1 root root 22 Mar 31 07:15 /etc/os-release -> ..//usr/lib/os-release
-rw-r--r--. 1 root root 45 Mar 31 07:15 /etc/redhat-release
lrwxrwxrwx. 1 root root 14 Mar 31 07:15 /etc/system-release -> redhat-release
$ cat /etc/redhat-release
Red Hat Enterprise Linux release 8.2 (Ootpa)
$ cat /etc/os-release
NAME="Red Hat Enterprise Linux"
VERSION="8.2 (Ootpa)"
ID="rhel"
ID_LIKE="fedora"
VERSION_ID="8.2"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Red Hat Enterprise Linux 8.2 (Ootpa)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:redhat:enterprise_linux:8.2:GA"
HOME_URL="https://www.redhat.com/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 8"
REDHAT_BUGZILLA_PRODUCT_VERSION=8.2
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="8.2"
3-2. CentOS / CentOS Stream
3-2. CentOS/ CentOS Stream 亦可翻译为:
在CentOS / CentOS Stream中,/etc/system-release和/etc/redhat-release被设置为指向/etc/centos-release的符号链接。
$ ls -l /etc/*release
-rw-r--r--. 1 root root 38 3月 13 04:15 /etc/centos-release
lrwxrwxrwx. 1 root root 21 3月 13 04:15 /etc/os-release -> ../usr/lib/os-release
lrwxrwxrwx. 1 root root 14 3月 13 04:15 /etc/redhat-release -> centos-release
lrwxrwxrwx. 1 root root 14 3月 13 04:15 /etc/system-release -> centos-release
$ cat /etc/centos-release
CentOS Linux release 8.1.1911 (Core)
$ cat /etc/centos-release
CentOS Stream release 8
$ cat /etc/os-release
NAME="CentOS Linux"
VERSION="8 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="8"
$ cat /etc/os-release
NAME="CentOS Stream"
VERSION="8"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Stream 8"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://centos.org/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux 8"
REDHAT_SUPPORT_PRODUCT_VERSION="CentOS Stream"
提示
对于大多数RHEL系发行版来说,小数点后一位(如8.1或8.2)并不是很重要。因为即使使用8.1的映像(媒体),通过使用Yum/DNF进行更新,可以达到最新的版本。
这也涉及到存储库的设置,但对此感兴趣的人可以自行调查。
3-3. Oracle Linux
3-3. Oracle Linux 這個選項
在Oracle Linux中,添加了/etc/oracle-release文档。
$ ls -l /etc/*release
-rw-r--r--. 1 root root 32 May 5 02:08 /etc/oracle-release
-rw-r--r--. 1 root root 479 May 5 02:08 /etc/os-release
-rw-r--r--. 1 root root 45 May 5 02:08 /etc/redhat-release
lrwxrwxrwx. 1 root root 14 May 5 02:08 /etc/system-release -> oracle-release
最令人感兴趣的是与CentOS的比较。在CentOS中,/etc/redhat-release是一个符号链接,但在Oracle Linux中却不是符号链接。
$ cat /etc/oracle-release
Oracle Linux Server release 8.2
$ cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="8.2"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="8.2"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Oracle Linux Server 8.2"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:8:2:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://bugzilla.oracle.com/"
ORACLE_BUGZILLA_PRODUCT="Oracle Linux 8"
ORACLE_BUGZILLA_PRODUCT_VERSION=8.2
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=8.2
由于/etc/redhat-release不是符号链接,所以它与RHEL相同。
$ cat /etc/redhat-release
Red Hat Enterprise Linux release 8.2 (Ootpa)
设定这样的规范,可能是为了确保与分发检查程序的兼容性。
许多商用产品和硬件供应商提供的驱动程序包通常使用/etc/*-release来确定发行版的种类和版本。通过保留原始的/etc/redhat-release文件,可以让系统错误地识别为RHEL,并顺利通过检查。
if /etc/redhat-releaseがあるか
バージョン確認
else if /etc/centos-releaseがあるか
バージョン確認
3-4. 亚马逊Linux 2
Amazon Linux 2中没有类似于/etc/<发行版本名称>-release的文件。
$ ls -l /etc/*release
-rw-r--r-- 1 root root 212 Jun 24 21:10 /etc/os-release
-rw-r--r-- 1 root root 31 Jun 24 21:10 /etc/system-release
$ cat /etc/system-release
Amazon Linux release 2 (Karoo)
亚马逊 Linux 是基于 RHEL 系列的发行版,但与 CentOS 或 Oracle Linux 不同,它并不是纯粹的 RHEL 兼容发行版。因此,/etc/os-release 文件的内容与其他 RHEL 系列发行版也不相同。
$ cat /etc/os-release
NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2"
PRETTY_NAME="Amazon Linux 2"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL="https://amazonlinux.com/"
3-5. 优酷(Chinese online video platform)
在Ubuntu中,有以下的文件。本次使用的版本是20.04 LTS,但18.04 LTS基本上也是一样的。
$ ls -l /etc/*release
-rw-r--r-- 1 root root 102 Apr 15 11:09 /etc/lsb-release
lrwxrwxrwx 1 root root 21 Apr 15 11:09 /etc/os-release -> ../usr/lib/os-release
$ ls -l /etc/*version
-rw-r--r-- 1 root root 13 Dec 5 2019 /etc/debian_version
-rw-r--r-- 1 root root 31 Jul 14 15:46 /etc/ec2_version
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
在Ubuntu中经常使用/etc/lsb-release或lsb_release -a命令。在RHEL系列发行版中也有lsb_release命令,但通常不会默认安装(需要redhat-lsb-core软件包)。
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04 LTS"
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04 LTS
Release: 20.04
Codename: focal
20.04 LTSのとき
$ cat /etc/debian_version
bullseye/sid
18.04 LTSのとき
$ cat /etc/debian_version
buster/sid
虽然提到EC2会让人觉得是Amazon EC2特有的,但Azure VM和Oracle Cloud Infrastructure Compute中也有这样的功能。
$ cat /etc/ec2_version
Ubuntu 20.04 LTS (Focal Fossa)
3-6. 额外的东西(示例代码)
我会介绍一个用于判断发行版类型和版本的Shell脚本样例代码。你可以使用sed来实现。
#!/bin/bash
FILENAME=/etc/os-release
dist=`grep -e ^NAME= ${FILENAME}`
dist=${dist#*\"}
dist=${dist%*\"}
echo "DIST: " $dist
version=`grep -e ^VERSION_ID= ${FILENAME}`
version=${version#*\"}
version=${version%*\"}
version=${version%*\.*} # マイナーバージョン削除
echo "VER: " $version
DIST: Oracle Linux Server
VER: 8
总结
ディストリビューションの種類やバージョンを調べるには/etc/*releaseを使用する
ディストリビューション共通のファイルとしては/etc/os-releaseが使える
ディストリビューション共通のファイルとしては/etc/os-releaseが使える