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 核心)。
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 腳本解析。
lsb_release -a
# Distributor ID: CentOS
# Description: CentOS Linux release 7.9.2009 (Core)
# Release: 7.9.2009
# Codename: Core
cat /etc/redhat-release
# CentOS Linux release 7.9.2009 (Core)
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"
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 判斷發行版