Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 版本查看全攻略:lsb_release、/etc/os-release、uname、內核版本與發行版區別詳解

編輯:Linux 教學

核心概念區分

概念含義查看命令範例輸出
發行版版本用戶空間工具鏈、套件管理器、系統庫版本(如 RHEL 8.6、Ubuntu 22.04)cat /etc/os-release
lsb_release -a
NAME="Ubuntu"
VERSION="22.04.3 LTS (Jammy Jellyfish)"
內核版本Linux 內核版本號(如 5.15.0-91-generic),決定硬體支援、系統調用、文件系統特性uname -r
cat /proc/version
5.15.0-91-generic
內核構建信息編譯器、編譯時間、配置選項cat /proc/versionLinux version 5.15.0-91-generic (buildd@lgw01-amd64-058) (gcc 11.4.0, GNU ld 2.38) #101-Ubuntu SMP

標準化查看方法(推薦順序)

1. /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 相容)。

2. 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

優點:輸出格式固定,易腳本解析。缺點:最小安裝可能缺失。

3. 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-releaseCentOS Linux release 7.9.2009 (Core)
RHEL 系統rpm -q redhat-releaseredhat-release-8.6-1.0.el8.x86_64
Debian/Ubuntu/etc/debian_version12.2
SUSE/openSUSE/etc/SuSE-releaseopenSUSE Leap 15.4
Alpine Linux/etc/alpine-release3.18.0
Arch Linux/etc/arch-release (空檔案,標識)滾動更新,無版本號

RHEL 版本號對照表(rpm 查詢)

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_。

容器/雲環境特殊情況

  • Docker 容器:共享宿主機內核,uname -r 顯示宿主機內核;/etc/os-release 顯示容器鏡像發行版(可能與宿主機不同)。
  • WSL 2:運行真實 Linux 內核,uname -r 顯示 Microsoft 構建內核版本(如 5.15.90.1-microsoft-standard-WSL2)。
  • 雲伺服器:廠商可能提供定製內核(AWS Kernel、Aliyun Kernel),版本號含廠商標識。

腳本化最佳實踐:跨平台獲取發行版 ID 與版本

#!/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 劇本、容器鏡像構建中精準識別運行環境。

Copyright © Windows教程網 All Rights Reserved