要判断当前运行的 Linux 系统是 Ubuntu 还是 CentOS,可以通过以下几种方法来识别:
✅ 方法一:查看系统发行版信息文件
1. 使用 cat /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" ...
2. 使用 cat /etc/issue
cat /etc/issue
输出示例:
- Ubuntu:
Ubuntu 22.04.3 LTS n l - CentOS:
CentOS Linux release 7.9.2009 (Core) n l
3. 查看特定发行版文件(推荐)
-
Ubuntu:
cat /etc/lsb-release输出中会包含
DISTRIB_ID=Ubuntu。 -
CentOS:
cat /etc/centos-release或者
cat /etc/redhat-release示例输出:
CentOS Linux release 7.9.2009 (Core)
✅ 方法二:使用命令行工具
1. 使用 hostnamectl(适用于 systemd 系统)
hostnamectl
在输出中可以看到操作系统那一行:
Operating System: Ubuntu 22.04.3 LTSOperating System: CentOS Linux 7 (Core)
2. 使用 lsb_release -d
lsb_release -d
输出示例:
- Ubuntu:
Description: Ubuntu 22.04.3 LTS - CentOS:
Description: CentOS Linux release 7.9.2009 (Core)
注意:某些最小化安装的 CentOS 可能没有安装
lsb_release工具,可以使用yum install redhat-lsb-core安装。
✅ 方法三:通过包管理器判断
你可以根据默认的包管理器判断:
| 发行版 | 默认包管理器 |
|---|---|
| Ubuntu | apt 或 apt-get |
| CentOS | yum 或 dnf(CentOS 8+) |
执行:
which apt yum dnf
例如:
- 如果输出
/usr/bin/apt存在,则可能是 Ubuntu。 - 如果输出
/usr/bin/yum存在,则可能是 CentOS。
🔍 总结一句话判断:
grep -Ei 'ubuntu|centos' /etc/os-release
或者直接查看系统信息:
cat /etc/os-release
如果你提供一段终端输出,我也可以帮你判断具体是哪种系统。
CDNK博客