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 查看發行版本與核心版本完整指令集

編輯:Linux 教學

Linux 系統資訊查詢分為「核心版本」與「發行版本」兩個層面,混淆兩者是常見錯誤。本文整理標準查詢指令與適用場景。

核心版本查詢


# 完整核心資訊
uname -a
# Linux hostname 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

# 僅核心版本號
uname -r
# 3.10.0-1160.el7.x86_64

說明:uname 只反映目前執行的核心版本,不代表發行套件版本 (如 RHEL 7.9 可能跑 3.10 或 5.4 核心)。

發行版本查詢 (依優先序推薦)

1. 標準化介面:/etc/os-release (systemd 系統通用,現代最佳)


cat /etc/os-release
# NAME="CentOS Linux"
# VERSION="7 (Core)"
# ID="centos"
# VERSION_ID="7"
# PRETTY_NAME="CentOS Linux 7 (Core)"

支援 source /etc/os-release && echo $VERSION_ID 腳本解析。

2. LSB 標準:lsb_release (需安裝 redhat-lsb-core)


lsb_release -a
# Distributor ID: CentOS
# Description:    CentOS Linux release 7.9.2009 (Core)
# Release:        7.9.2009
# Codename:       Core

3. Red Hat 系專屬:/etc/redhat-release


cat /etc/redhat-release
# CentOS Linux release 7.9.2009 (Core)

4. Debian/Ubuntu 系專屬:/etc/issue 或 /etc/lsb-release


cat /etc/issue
# Ubuntu 20.04.3 LTS \n \l

cat /etc/lsb-release
# DISTRIB_ID=Ubuntu
# DISTRIB_RELEASE=20.04
# DISTRIB_CODENAME=focal
# DISTRIB_DESCRIPTION="Ubuntu 20.04.3 LTS"

5. 通用備援:/etc/issue (登入前顯示,可能被自訂修改)


cat /etc/issue

腳本自動偵測範例


#!/bin/bash
detect_distro() {
    if [ -f /etc/os-release ]; then
        source /etc/os-release
        echo "$ID $VERSION_ID"
    elif [ -f /etc/redhat-release ]; then
        grep -oE '[0-9]+\.[0-9]+' /etc/redhat-release | head -1 | xargs -I{} echo "rhel {}"
    elif [ -f /etc/lsb-release ]; then
        source /etc/lsb-release
        echo "$DISTRIB_ID $DISTRIB_RELEASE"
    else
        echo "unknown"
    fi
}

硬體架構與其他資訊


# CPU 架構
uname -m        # x86_64, aarch64
arch            # 同上

# 完整硬體資訊
lscpu
dmidecode -t system  # 需 root

# 套件管理器版本 (間接反映發行版)
rpm --version   # RPM 系
dpkg --version  # DEB 系

總結建議

  • 現代腳本:優先解析 /etc/os-release,標準、穩定、跨發行版
  • 人工查詢:cat /etc/redhat-release 或 lsb_release -a 最直觀
  • 核心版本:uname -r,模組相容性、驅動編譯時必查
  • 避免:單獨依賴 /etc/issue (易被修改)、uname -a 判斷發行版
Copyright © Windows教程網 All Rights Reserved