要判断当前使用的 Linux 系统是 Ubuntu 还是 CentOS,可以使用多种方法。以下是几种常用的命令方式:
✅ 方法一:查看 /etc/os-release 文件
cat /etc/os-release
输出示例:
-
Ubuntu:
NAME="Ubuntu" VERSION="22.04.3 LTS (Jammy Jellyfish)" ID=ubuntu ID_LIKE=debian ... -
CentOS:
NAME="CentOS Linux" VERSION="7 (Core)" ID="centos" ID_LIKE="rhel fedora" ...
✅ 方法二:使用 lsb_release -d 命令(适用于 Ubuntu)
lsb_release -d
输出示例:
-
Ubuntu:
Description: Ubuntu 22.04.3 LTS -
CentOS 上可能没有安装
lsb_release,你可以先用which lsb_release判断是否存在。
✅ 方法三:查看系统发行版这里件
Ubuntu/Debian 系统通常有:
cat /etc/issue
或
cat /etc/lsb-release
CentOS/RHEL 系统通常有:
cat /etc/centos-release
或
cat /etc/redhat-release
✅ 方法四:使用 hostnamectl(如果系统使用 systemd)
hostnamectl
在输出中会显示操作系统信息,例如:
Operating System: Ubuntu 22.04.3 LTS
或
Operating System: CentOS Linux 7 (Core)
✅ 方法五:检查包管理器类型
你可以通过判断默认的包管理器来间接识别系统:
-
Ubuntu/Debian 使用
apt:which apt -
CentOS/RHEL 使用
yum或dnf:which yum
🧪 示例脚本(自动判断)
你可以运行如下 Bash 脚本来简单判断:
if [ -f /etc/os-release ]; then
. /etc/os-release
echo "This is $NAME"
elif type lsb_release >/dev/null 2>&1; then
echo "This is $(lsb_release -d | cut -f2)"
else
echo "Unknown OS"
fi
总结对比表:
| 特性 | Ubuntu | CentOS |
|---|---|---|
| 包管理器 | apt |
yum / dnf |
| 配置文件 | /etc/lsb-release |
/etc/centos-release |
| 发行商标识 | Debian-based | RHEL-based |
| 常见用途 | 桌面、开发环境 | 服务器、企业级应用 |
如果你需要进一步判断是否为 CentOS Stream 或 Rocky Linux、AlmaLinux 等衍生版本,也可以查看 /etc/os-release 中的 VERSION_ID 或 PRETTY_NAME 字段。
如需帮助分析具体输出内容,可以贴出来我帮你判断 👍
秒懂云