如何确认服务器主机安装的是Ubuntu还是CentOS?

可以通过以下几种方法快速确认服务器安装的是Ubuntu还是CentOS:

1. 查看发行版信息文件

# 通用方法
cat /etc/os-release

# 或使用以下命令
cat /etc/*-release
lsb_release -a
hostnamectl

2. 检查特定标识文件

# CentOS特有文件
cat /etc/centos-release
cat /etc/redhat-release

# Ubuntu特有文件
cat /etc/lsb-release
cat /etc/debian_version

3. 通过包管理器判断

# 检查包管理器
which apt      # Ubuntu/Debian使用apt
which yum      # CentOS 7及以前使用yum
which dnf      # CentOS 8+使用dnf

4. 快速判断脚本

#!/bin/bash
if [ -f /etc/os-release ]; then
    . /etc/os-release
    echo "系统: $NAME"
    echo "版本: $VERSION"
elif [ -f /etc/centos-release ]; then
    echo "系统: CentOS"
    echo "版本: $(cat /etc/centos-release)"
elif [ -f /etc/redhat-release ]; then
    echo "系统: RedHat/CentOS"
    echo "版本: $(cat /etc/redhat-release)"
elif [ -f /etc/lsb-release ]; then
    echo "系统: Ubuntu"
    . /etc/lsb-release
    echo "版本: $DISTRIB_DESCRIPTION"
fi

5. 输出特征对比

Ubuntu典型输出:

NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu

CentOS典型输出:

NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"

6. 其他辅助方法

# 查看内核信息(辅助判断)
uname -a

# 查看系统架构
arch

# 查看系统启动日志
dmesg | head -20

推荐使用顺序:

  1. 首选cat /etc/os-release – 最标准、信息最全
  2. 备选lsb_release -ahostnamectl
  3. 快速判断:检查包管理器 which apt/yum/dnf

这些方法在大多数情况下都能准确识别系统类型,即使是通过SSH远程连接也能使用。

云服务器