查看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號