查看Linux 服务器是基于 Debian/Ubuntu 还是 Red Hat/CentOS
要判断 Linux 服务器是基于 Debian/Ubuntu 还是 Red Hat/CentOS(或现代的 Rocky Linux、AlmaLinux),可以使用以下几种简单有效的方法:
✅ 方法 1:查看 /etc/os-release 文件(推荐,最通用)
cat /etc/os-release
输出示例:
Debian/Ubuntu:
NAME="Ubuntu"
VERSION="22.04.3 LTS"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 22.04.3 LTS"
CentOS/Rocky Linux:
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
🔍 关键看
ID=或ID_LIKE=字段:
debian,ubuntu→ Debian 系rhel,centos,fedora,rocky→ Red Hat 系
✅ 方法 2:查看 /etc/issue 或 /etc/*-release 文件
# 通用查看
cat /etc/issue
# 或查看特定发行版文件
ls /etc/*-release
cat /etc/os-release
cat /etc/debian_version # 存在 → 可能是 Debian/Ubuntu
cat /etc/redhat-release # 存在 → Red Hat/CentOS
例如:
cat /etc/redhat-release
# 输出:CentOS Linux release 7.9.2009 (Core)
✅ 方法 3:使用 hostnamectl 命令(systemd 系统通用)
hostnamectl
输出中会包含操作系统信息:
Operating System: Ubuntu 22.04.3 LTS
Kernel: Linux 5.15.0-86-generic
Architecture: x86-64
✅ 方法 4:通过包管理器判断(最直接)
不同的系统使用不同的包管理器:
| 系统类型 | 包管理器 | 命令示例 |
|---|---|---|
| Debian/Ubuntu | apt / dpkg |
apt --version 或 dpkg --version |
| Red Hat/CentOS | yum / dnf / rpm |
yum --version 或 rpm --version |
运行:
which apt || which yum || which dnf
结果:
- 输出
apt→ Debian/Ubuntu - 输出
yum或dnf→ Red Hat/CentOS/Fedora
✅ 方法 5:使用 lsb_release 命令(适用于 LSB 标准系统)
lsb_release -a
输出:
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
如果提示
command not found,可以安装:# Debian/Ubuntu apt install lsb-release # Red Hat/CentOS yum install redhat-lsb-core
🧩 一键快速判断脚本
你可以运行以下命令快速识别:
. /etc/os-release && echo "系统类型: $ID / $ID_LIKE"
常见输出:
debian或ubuntu→ 使用aptrhel,centos,fedora,rocky→ 使用yum或dnf
🔁 总结:如何选择后续命令?
| 任务 | Debian/Ubuntu | Red Hat/CentOS |
|---|---|---|
| 安装软件 | sudo apt install git |
sudo yum install git 或 sudo dnf install git |
| 更新软件源 | sudo apt update |
sudo yum check-update |
| 查看已安装包 | dpkg -l git |
rpm -qa git |

浙公网安备 33010602011771号