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

在 Linux 系统中,判断发行版属于 RedHat 系列(如 RHEL, CentOS, Fedora)还是 Ubuntu 系列(Debian 系),最通用且准确的方法是查看系统标识文件。以下是几种常用的命令:

1. 查看 /etc/os-release(推荐,适用于大多数现代发行版)

这是目前最标准的查看方式,几乎所有基于 systemd 的现代 Linux 发行版都包含此文件。

cat /etc/os-release

如何判断:

  • RedHat 系列:通常会看到 IDID_LIKE 字段包含 rhel, centos, fedora, rocky, almalinux 等。
    • 例如:ID="rhel", ID="fedora", ID="centos"
  • Ubuntu/Debian 系列:通常会看到 ID=ubuntuID=debian
    • 例如:ID="ubuntu", ID="debian"

快速筛选命令

grep -E "^ID=" /etc/os-release

2. 查看特定发行版标识文件

如果 /etc/os-release 信息不全,可以检查以下特定文件是否存在:

对于 RedHat 系列:

# 查看 CentOS/RHEL/Fedora 版本
cat /etc/redhat-release
# 或者
ls -l /etc/fedora-release /etc/centos-release /etc/system-release
  • 如果 /etc/redhat-release 存在且有内容,则属于 RedHat 系列。

对于 Ubuntu/Debian 系列:

# 查看 Ubuntu 版本
cat /etc/lsb-release
# 或者
cat /etc/debian_version
  • 如果 /etc/lsb-release 中包含 DISTRIB_ID="Ubuntu",则属于 Ubuntu 系列。
  • 如果 /etc/debian_version 存在,通常属于 Debian 或其衍生版(如 Ubuntu)。

3. 使用 hostnamectl 命令

如果你使用的是较新的系统(带有 systemd),这个命令非常直观:

hostnamectl

输出示例分析:

  • RedHat 系列Operating System: Rocky Linux 8.6 (Green Obsidian)CentOS Stream 9
  • Ubuntu 系列Operating System: Ubuntu 22.04.3 LTS

总结对照表

特征 RedHat 系列 (RHEL/CentOS/Fedora) Ubuntu/Debian 系列
核心 ID (/etc/os-release) ID=rhel, ID=fedora, ID=centos ID=ubuntu, ID=debian
专用文件 /etc/redhat-release /etc/lsb-release, /etc/debian_version
包管理器 yum / dnf / rpm apt / dpkg
配置目录风格 /etc/sysconfig/ /etc/default/

结论:最直接的方法是运行 cat /etc/os-release 并观察 ID= 这一行。如果是 ubuntudebian,则是 Ubuntu 系列;如果是 rhelcentosfedora 等,则是 RedHat 系列。

未经允许不得转载:CLOUD技术博 » linux查看系统是RedHat系列还是Ubuntu系列?