云服务器上如何确认当前运行的是CentOS还是Ubuntu系统?

可以通过以下几种方法快速确认云服务器的操作系统类型:

1. 查看发行版信息文件

# 方法1:查看 /etc/os-release(推荐)
cat /etc/os-release

# 方法2:查看 /etc/issue
cat /etc/issue

# 方法3:查看 /etc/redhat-release(仅CentOS/RHEL)
cat /etc/redhat-release 2>/dev/null || echo "Not CentOS/RHEL"

2. 使用系统命令

# 方法1:hostnamectl(systemd系统)
hostnamectl

# 方法2:lsb_release
lsb_release -a

# 方法3:uname命令(查看内核)
uname -a

3. 通过包管理器判断

# 检查包管理器
which yum 2>/dev/null && echo "可能是CentOS/RHEL"
which apt 2>/dev/null && echo "可能是Ubuntu/Debian"

4. 快速判断脚本

#!/bin/bash
if [ -f /etc/redhat-release ]; then
    echo "系统是 CentOS/RHEL"
    cat /etc/redhat-release
elif [ -f /etc/lsb-release ]; then
    echo "系统是 Ubuntu"
    grep DISTRIB_DESCRIPTION /etc/lsb-release
else
    echo "其他系统"
    cat /etc/os-release
fi

5. 主要区别特征

  • CentOS:有 /etc/redhat-release 文件,使用 yum/dnf 包管理器
  • Ubuntu:有 /etc/lsb-release 文件,使用 apt 包管理器

6. 云平台控制台查看

大多数云服务商(阿里云、腾讯云、AWS等)的控制台也会显示实例的操作系统信息。

最简单直接的方法:

cat /etc/os-release | grep "^NAME="

这会直接显示类似 NAME="CentOS Linux" 或 NAME="Ubuntu" 的信息。

云服务器