快速確認 Linux 發行版、版本號、內核版本是運維、腳本適配、軟件安裝的基礎動作。本文匯總 5 類通用與特定命令,實測 Oracle Linux、CentOS、Red Hat 三大主流發行版,並說明適用邊界。
/etc/issue — 登錄前顯示的發行信息# Oracle Linux 5.7
[root@DB-Server ~]# cat /etc/issue
Oracle Linux Server release 5.7
Kernel \r on an \m
# CentOS 6.4
[root@Centos2 ~]# cat /etc/issue
CentOS release 6.4 (Final)
Kernel \r on an \m
# Red Hat Enterprise Linux 5.7
[root@getlnx05 ~]# cat /etc/issue
Red Hat Enterprise Linux Server release 5.7 (Tikanga)
Kernel \r on an \m
特點: 所有發行版通用、簡潔直觀。缺點: 可被管理員自定義修改,非權威來源;僅顯示發行版信息,無內核版本。
lsb_release -a — LSB 標準化查詢 (推薦)# Oracle Linux
[root@DB-Server ~]# lsb_release -a
LSB Version: :core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:printing-4.0-noarch
Distributor ID: EnterpriseEnterpriseServer
Description: Enterprise Linux Enterprise Linux Server release 5.7 (Carthage)
Release: 5.7
Codename: Carthage
# CentOS 6.4
[root@Centos2 ~]# lsb_release -a
LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID: CentOS
Description: CentOS release 6.4 (Final)
Release: 6.4
Codename: Final
# Red Hat Enterprise Linux 5.7
[root@getlnx05 ~]# lsb_release -a
LSB Version: :core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:printing-4.0-noarch
Distributor ID: RedHatEnterpriseServer
Description: Red Hat Enterprise Linux Server release 5.7 (Tikanga)
Release: 5.7
Codename: Tikanga
優勢: 標準化輸出、含發行版 ID、描述、版本號、代號,腳本解析友好。前提: 需安裝 redhat-lsb-core 或 lsb-release 包 (最小化安裝可能缺失)。
/etc/*release 系列文件 — 發行版專屬權威源# Oracle Linux (多文件)
[root@DB-Server etc]# cat /etc/*release
Enterprise Linux Enterprise Linux Server release 5.7 (Carthage)
Oracle Linux Server release 5.7
Red Hat Enterprise Linux Server release 5.7 (Tikanga)
[root@DB-Server etc]# ls /etc/*release
/etc/enterprise-release /etc/oracle-release /etc/redhat-release
# CentOS 6.4
[root@Centos2 ~]# cat /etc/*release
CentOS release 6.4 (Final)
LSB_VERSION=base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
CentOS release 6.4 (Final)
CentOS release 6.4 (Final)
[root@Centos2 ~]# ls /etc/*release
/etc/centos-release /etc/lsb-release /etc/redhat-release /etc/system-release
# Red Hat Enterprise Linux 5.7
[root@getlnx05 ~]# cat /etc/*release
Red Hat Enterprise Linux Server release 5.7 (Tikanga)
[root@getlnx05 ~]# ls /etc/*release
/etc/redhat-release
規律: RHEL 系統一提供 /etc/redhat-release;衍生版 (CentOS、Oracle、Scientific) 提供各自 *-release 文件;/etc/os-release (systemd 系統) 為新標準,跨發行版通用。
cat /etc/os-release — systemd 時代的統一標準 (現代系統首選)# CentOS 7 / RHEL 7 / Ubuntu 16.04+ 等
[root@centos7 ~]# cat /etc/os-release
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/"
優勢: 跨發行版統一格式、機器可解析、包含 ID_LIKE 追溯上游、systemd 系統必備。
rpm -q redhat-release / dpkg -l — 包管理器查詢 (特定發行版)# RHEL/CentOS/Oracle (rpm)
[root@getlnx05 ~]# rpm -q redhat-release
redhat-release-5Server-5.7.0.3
# Oracle 專屬
rpm -q oraclelinux-release
# Debian/Ubuntu (dpkg)
dpkg -l | grep base-files
適用性: 僅限對應包管理體系,跨平台腳本需判斷包管理器類型。
uname -r # 如 3.10.0-1160.el7.x86_64
uname -a # 完整內核信息
cat /proc/version # 含 GCC 版本、編譯時間
/etc/os-release (現代) 或 lsb_release -a (傳統)cat /etc/redhat-release (RHEL 系) / cat /etc/issue (通用)/etc/*release 文件/etc/os-release 通常映射到容器內uname -r