【如何判断Linux系统是Ubuntu还是CentOS】
要确定您的操作系统是 Ubuntu 还是 CentOS,可以通过以下方法快速检查:
方法 1:通过终端命令(推荐)
在终端中执行以下命令之一:
查看 /etc/os-release
文件
cat /etc/os-release
- Ubuntu 特征:显示
NAME="Ubuntu"
或ID=ubuntu
- CentOS 特征:显示
NAME="CentOS Linux"
或ID="centos"
直接提取系统 ID
grep -E '^ID=' /etc/os-release | cut -d= -f2 | tr -d '"'
- 输出结果直接显示
ubuntu
或centos
。
方法 2:检查发行版特有文件
检查 CentOS 专属文件
cat /etc/redhat-release # 或 /etc/centos-release
- CentOS 会显示类似
CentOS Linux release 7.x
的信息。 - Ubuntu 会提示 “No such file or directory”。
检查 Ubuntu 专属文件
cat /etc/lsb-release
- Ubuntu 会显示
DISTRIB_ID=Ubuntu
。 - CentOS 可能提示 “No such file or directory”。
方法 3:使用包管理器命令
检查 apt
(Ubuntu 专属)
which apt # 或 dpkg --version
- 如果返回
/usr/bin/apt
,则系统是 Ubuntu。
检查 yum
(CentOS 专属)
which yum # 或 rpm --version
- 如果返回
/usr/bin/yum
,则系统是 CentOS。
总结判断逻辑
- 执行
cat /etc/os-release
→ 查看NAME
或ID
字段。 - 若无法确定 → 检查
cat /etc/redhat-release
(CentOS)或cat /etc/lsb-release
(Ubuntu)。 - 辅助验证 → 使用
which apt
(Ubuntu)或which yum
(CentOS)。
执行上述命令后,您将明确区分系统是 Ubuntu 还是 CentOS。