linux查看系统是RedHat系列还是Ubuntu系列?

在 Linux 系统中,可以通过以下几种常用命令快速判断系统是 Red Hat 系列(如 CentOS、RHEL、Fedora)还是 Ubuntu/Debian 系列。

1. 查看发行版标识文件(最通用)

绝大多数现代 Linux 发行版都在 /etc 目录下提供了版本信息文件。

方法 A:读取 os-release 文件(推荐)
这是目前最标准的方法,适用于几乎所有现代发行版(包括较新的 Ubuntu 和 RHEL 8+)。

cat /etc/os-release
  • Red Hat 系列特征:输出中通常包含 NAME="Red Hat Enterprise Linux"NAME="CentOS Linux"ID=centosID=fedora 等。
  • Ubuntu 系列特征:输出中会显示 NAME="Ubuntu"ID=ubuntuID_LIKE=debian

方法 B:读取特定发行版文件
如果系统较老或上述文件不存在,可以检查以下特定文件:

  • Red Hat 系列

    cat /etc/redhat-release
    • 如果有输出(例如 Red Hat Enterprise Linux Server release 7.9),则是 Red Hat 系列。
    • 如果没有输出或提示“没有那个文件或目录”,则不是 Red Hat 系列。
  • Ubuntu/Debian 系列

    cat /etc/lsb-release
    • 如果有输出且包含 DISTRIB_ID="Ubuntu",则是 Ubuntu 系列。

2. 使用 hostnamectl 命令(针对 systemd 系统)

如果你的系统使用了 systemd 初始化系统(大多数现代服务器都是),这个命令非常简洁:

hostnamectl
  • Red Hat 系列Operating System 字段通常显示为 Red Hat Enterprise LinuxCentOS Linux
  • Ubuntu 系列Operating System 字段通常显示为 Ubuntu

3. 一键判断脚本

如果你需要快速区分并得到明确结论,可以运行以下逻辑判断:

if [ -f /etc/redhat-release ]; then
    echo "✅ 属于 Red Hat 系列"
    cat /etc/redhat-release
elif grep -q "Ubuntu" /etc/os-release; then
    echo "✅ 属于 Ubuntu 系列"
    grep "^NAME=" /etc/os-release
elif grep -q "Debian" /etc/os-release; then
    echo "✅ 属于 Debian 系列 (Ubuntu 基于此)"
else
    echo "❓ 未知发行版,请查看 /etc/os-release"
    cat /etc/os-release
fi

总结对照表

特征关键字 所属系列 典型发行版示例
ID=centos, ID=rhel, ID=fedora Red Hat 系列 CentOS, RHEL, Fedora, Rocky Linux, AlmaLinux
ID=ubuntu, ID=debian Ubuntu/Debian 系列 Ubuntu, Debian, Kali Linux, Mint
/etc/redhat-release 存在 Red Hat 系列 (同上)
/etc/lsb-release 含 Ubuntu Ubuntu 系列 (同上)

通常情况下,直接运行 cat /etc/os-release 就能获得最准确的信息。

未经允许不得转载:云计算HECS » linux查看系统是RedHat系列还是Ubuntu系列?