如何在CentOS和Ubuntu系统中查看系统版本信息?

在 CentOS 和 Ubuntu 系统中,查看系统版本信息的方法略有不同,以下是常用命令及说明:


一、通用方法(适用于大多数 Linux 发行版)

# 查看内核版本(不区分发行版)
uname -r

# 查看操作系统名称和版本号(部分系统支持)
cat /etc/os-release

os-release 是 systemd 标准文件,Ubuntu 20.04+ 和 CentOS 7/8/RHEL 7+ 均支持。


二、CentOS 专用方法

1. CentOS 7 及更早版本

# 查看发行版信息(推荐)
cat /etc/centos-release

# 或查看详细版本
rpm -q centos-release

2. CentOS Stream / Rocky Linux / AlmaLinux

# 统一使用 os-release(最可靠)
cat /etc/os-release | grep -E "NAME|VERSION"

# 或直接查看完整内容
cat /etc/os-release

💡 注意:CentOS 8 已停止维护,官方推荐使用 Rocky LinuxAlmaLinux,它们的 /etc/os-release 格式与 RHEL 兼容。


三、Ubuntu 专用方法

1. Ubuntu 16.04 及之后版本

# 推荐方式(标准化)
cat /etc/os-release

# 快速查看版本号
lsb_release -a

⚠️ 若未安装 lsb-release 包,可先运行:

sudo apt update && sudo apt install lsb-release

2. 旧版 Ubuntu(如 14.04)

# 传统方式
cat /etc/issue
cat /etc/ubuntu-release  # 部分旧版本存在

四、对比总结表

命令 CentOS 7+ Ubuntu 16.04+ 备注
cat /etc/os-release ✅ 推荐 ✅ 推荐 最通用可靠
lsb_release -a ❌ 通常无此命令 ✅ 需安装 Ubuntu 专属工具
cat /etc/centos-release ✅ 仅 CentOS ❌ 不存在 CentOS 7 特有
cat /etc/issue ⚠️ 可能不完整 ⚠️ 可能不完整 非标准化,仅作辅助参考

示例输出

CentOS 7:

$ cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)

Ubuntu 22.04:

$ cat /etc/os-release
NAME="Ubuntu"
VERSION="22.04.3 LTS (Jammy Jellyfish)"
...

🔍 提示:生产环境中建议优先使用 cat /etc/os-release,因其符合 freedesktop.org 标准,兼容性最好。