管理數十上百台 CentOS 主機時,快速確認作業系統版本是日常運維的基本動作。本文整理三種最常用、最可靠的查看方式,並說明各自適用場景與注意事項。
/etc/issue —— 登入前的版本宣告[root@syy ~]# cat /etc/issue
CentOS release 6.5 (Final)
Kernel \r on an \m
/etc/os-release —— 標準化、機器可讀(推薦)[root@syy ~]# 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/"
VERSION_ID、ID 適合腳本解析(source /etc/os-release && echo $VERSION_ID)centos-release 套件才會有此檔案uname -r —— 核心版本(非發行版本)[root@syy ~]# uname -r
3.10.0-1160.el7.x86_64
uname -a 取得完整架構資訊#!/bin/bash
# get_os_info.sh
echo "=== OS Release ==="
[ -f /etc/os-release ] && source /etc/os-release && echo "$PRETTY_NAME" || cat /etc/issue
echo "=== Kernel ==="
uname -r
echo "=== Architecture ==="
uname -m
/etc/os-release 反映的是容器映像版本,非宿主機版本。VERSION_ID 為 8/9,但 ID_LIKE 含 rhel,需留意。lsb_release 指令,優先用檔案讀取。掌握這三招,無論是手動巡檢、Ansible Playbook 還是 Prometheus Node Exporter 客製化收集,都能精準獲取 CentOS 版本資訊。