| 概念 | 含義 | 查看命令 | 範例輸出 |
|---|---|---|---|
| 發行版版本 | 用戶空間工具鏈、套件管理器、系統庫版本(如 RHEL 8.6、Ubuntu 22.04) | cat /etc/os-releaselsb_release -a | NAME="Ubuntu" VERSION="22.04.3 LTS (Jammy Jellyfish)" |
| 內核版本 | Linux 內核版本號(如 5.15.0-91-generic),決定硬體支援、系統調用、文件系統特性 | uname -rcat /proc/version | 5.15.0-91-generic |
| 內核構建信息 | 編譯器、編譯時間、配置選項 | cat /proc/version | Linux version 5.15.0-91-generic (buildd@lgw01-amd64-058) (gcc 11.4.0, GNU ld 2.38) #101-Ubuntu SMP |
/etc/os-release`(現代標準,systemd 規範,所有主流發行版支援)$ cat /etc/os-release
NAME="Ubuntu"
VERSION="22.04.3 LTS (Jammy Jellyfish)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 22.04.3 LTS"
VERSION_ID="22.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=jammy
UBUNTU_CODENAME=jammy
腳本解析:source /etc/os-release && echo $VERSION_ID(POSIX sh 相容)。
lsb_release -a`(LSB 標準,需安裝 lsb-release 套件)$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
優點:輸出格式固定,易腳本解析。缺點:最小安裝可能缺失。
hostnamectl`(systemd 系統,含硬體/虛擬化信息)$ hostnamectl
Static hostname: web-01
Icon name: computer-vm
Chassis: vm
Machine ID: 1234567890abcdef
Boot ID: abcdef1234567890
Virtualization: kvm
Operating System: Ubuntu 22.04.3 LTS
Kernel: Linux 5.15.0-91-generic
Architecture: x86-64
Hardware Vendor: QEMU
Hardware Model: Standard PC (Q35 + ICH9, 2009)
Firmware Version: 1.15.0-1
Firmware Date: Fri 2023-01-01
| 發行版家族 | 文件 | 範例內容 |
|---|---|---|
| RHEL/CentOS/Fedora/Rocky/AlmaLinux | /etc/redhat-release | CentOS Linux release 7.9.2009 (Core) |
| RHEL 系統 | rpm -q redhat-release | redhat-release-8.6-1.0.el8.x86_64 |
| Debian/Ubuntu | /etc/debian_version | 12.2 |
| SUSE/openSUSE | /etc/SuSE-release | openSUSE Leap 15.4 |
| Alpine Linux | /etc/alpine-release | 3.18.0 |
| Arch Linux | /etc/arch-release (空檔案,標識) | 滾動更新,無版本號 |
redhat-release-3AS-1 -> Red Hat Enterprise Linux AS 3
redhat-release-3AS-7.4 -> Red Hat Enterprise Linux AS 3 Update 4
redhat-release-4AS-2 -> Red Hat Enterprise Linux AS 4
redhat-release-4AS-2.4 -> Red Hat Enterprise Linux AS 4 Update 1
redhat-release-4AS-3 -> Red Hat Enterprise Linux AS 4 Update 2
redhat-release-4AS-4.1 -> Red Hat Enterprise Linux AS 4 Update 3
redhat-release-4AS-5.5 -> Red Hat Enterprise Linux AS 4 Update 4
redhat-release-5Server-5.0 -> RHEL 5 GA
redhat-release-6Server-6.0 -> RHEL 6 GA
redhat-release-7Server-7.0 -> RHEL 7 GA
redhat-release-8.6-1.0 -> RHEL 8.6
redhat-release-9.2-1.0 -> RHEL 9.2
$ uname -r
5.15.0-91-generic
# 解析:
# 5 - 主版本
# 15 - 次版本
# 0 - 修訂版本
# -91 - 發行版構建號(Ubuntu: 91 次構建)
# -generic - 風味:generic(桌面/伺服器), lowlatency(音頻), cloud(雲優化), aws/azure/gcp 等
檢查內核是否支援某特性:grep CONFIG_NF_TABLES /boot/config-$(uname -r) 或 zcat /proc/config.gz | grep CONFIG_。
uname -r 顯示宿主機內核;/etc/os-release 顯示容器鏡像發行版(可能與宿主機不同)。uname -r 顯示 Microsoft 構建內核版本(如 5.15.90.1-microsoft-standard-WSL2)。#!/bin/bash
# get_os_info.sh
get_os_info() {
if [ -f /etc/os-release ]; then
. /etc/os-release
OS_ID=$ID
OS_VERSION=$VERSION_ID
OS_PRETTY=$PRETTY_NAME
elif command -v lsb_release >/dev/null; then
OS_ID=$(lsb_release -si | tr '[:upper:]' '[:lower:]')
OS_VERSION=$(lsb_release -sr)
OS_PRETTY=$(lsb_release -sd)
elif [ -f /etc/redhat-release ]; then
OS_ID="rhel"
OS_VERSION=$(grep -oE '[0-9]+\.[0-9]+' /etc/redhat-release | head -1)
OS_PRETTY=$(cat /etc/redhat-release)
elif [ -f /etc/debian_version ]; then
OS_ID="debian"
OS_VERSION=$(cat /etc/debian_version)
OS_PRETTY="Debian $OS_VERSION"
else
OS_ID="unknown"
OS_VERSION="unknown"
OS_PRETTY="Unknown Linux"
fi
echo "ID=$OS_ID"
echo "VERSION=$OS_VERSION"
echo "PRETTY=$OS_PRETTY"
}
get_os_info
首選 /etc/os-release:標準化、無依賴、機器可解析、包含完整元數據。
輔助 uname -r:獲取內核版本,排查驅動/模組相容性。
避免 解析 uname -a、/proc/version 判斷發行版——它們反映的是內核,而非用戶空間發行版。掌握兩者區別,才能在跨平台腳本、Ansible 劇本、容器鏡像構建中精準識別運行環境。