云服务器上如何检查CentOS或Ubuntu的系统版本?

在云服务器上检查CentOS或Ubuntu系统版本,可以通过以下几种常用命令:

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

1. 查看发行版信息

# 查看系统详细信息
cat /etc/os-release

# 或使用更简洁的命令
lsb_release -a

# 如果lsb_release未安装,可以安装
# CentOS: yum install redhat-lsb-core
# Ubuntu: apt install lsb-release

2. 查看内核版本

# 查看内核版本
uname -r

# 查看完整系统信息
uname -a

二、针对CentOS/RHEL系统

1. 查看CentOS版本

# 方法1:查看发行版文件
cat /etc/redhat-release
cat /etc/centos-release

# 方法2:使用hostnamectl
hostnamectl

# 方法3:查看系统版本文件
cat /etc/system-release

2. 检查CentOS版本号

# 获取主版本号
rpm -q centos-release
# 或
cat /etc/redhat-release | awk '{print $4}'

三、针对Ubuntu/Debian系统

1. 查看Ubuntu版本

# 方法1:查看版本文件
cat /etc/issue
cat /etc/lsb-release

# 方法2:使用hostnamectl
hostnamectl

# 方法3:查看版本代号
lsb_release -c

2. 检查Ubuntu版本详细信息

# 查看完整版本信息
lsb_release -d

# 查看版本号和代号
lsb_release -r -c

四、快速判断系统类型

1. 快速识别脚本

#!/bin/bash
if [ -f /etc/redhat-release ]; then
    echo "这是CentOS/RHEL系统"
    cat /etc/redhat-release
elif [ -f /etc/lsb-release ]; then
    echo "这是Ubuntu/Debian系统"
    cat /etc/lsb-release
elif [ -f /etc/debian_version ]; then
    echo "这是Debian系统"
    cat /etc/debian_version
else
    echo "未知系统"
fi

2. 使用Python检查

python3 -c "import platform; print(platform.platform())"

五、实用技巧

1. 组合命令查看完整信息

echo "系统版本:" && cat /etc/os-release | grep PRETTY_NAME
echo "内核版本:" && uname -r
echo "架构信息:" && uname -m

2. 使用表格形式显示

printf "%-20s %sn" "系统类型:" "$(cat /etc/os-release | grep ^NAME= | cut -d'"' -f2)"
printf "%-20s %sn" "版本号:" "$(cat /etc/os-release | grep VERSION_ID | cut -d'"' -f2)"
printf "%-20s %sn" "内核版本:" "$(uname -r)"

六、云服务器特殊注意事项

  1. 镜像版本:云服务器可能使用定制镜像,版本信息可能略有不同
  2. 最小化安装:某些云镜像可能是最小化安装,需要先安装lsb_release
  3. 容器环境:如果在容器内,可能需要检查基础镜像版本

示例输出

CentOS示例:

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

Ubuntu示例:

$ lsb_release -a
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:        20.04
Codename:       focal

选择最适合你需求的方法即可快速查看系统版本信息。

云服务器