要查看当前系统是 Ubuntu 还是 CentOS,可以通过以下几种方法来判断:
✅ 方法一:使用 cat /etc/os-release
这是最直接的方法,适用于大多数现代 Linux 发行版。
cat /etc/os-release
输出示例:
-
Ubuntu:
NAME="Ubuntu" VERSION="20.04.6 LTS (Focal Fossa)" ID=ubuntu ID_LIKE=debian ... -
CentOS:
NAME="CentOS Linux" VERSION="7 (Core)" ID="centos" ID_LIKE="rhel fedora" ...
✅ 方法二:使用 lsb_release -d
如果安装了 lsb-core 包,可以使用这个命令:
lsb_release -d
输出示例:
-
Ubuntu:
Description: Ubuntu 22.04.3 LTS -
CentOS:
Description: CentOS Linux release 8.5.2111
⚠️ 注意:某些最小化安装的 CentOS 可能没有安装
lsb-core,此时会提示命令未找到。
✅ 方法三:查看是否存在特定发行版文件
你可以检查一些特定的系统文件是否存在或包含什么内容:
检查 /etc/issue 文件:
cat /etc/issue
输出示例:
-
Ubuntu:
Ubuntu 22.04.3 LTS n l -
CentOS:
CentOS Linux release 8.5.2111 n l
✅ 方法四:使用 grep 搜索关键字
grep VERSION_ID /etc/os-release
输出示例:
-
Ubuntu:
VERSION_ID="22.04" -
CentOS:
VERSION_ID="8"
✅ 方法五:使用 hostnamectl(适用于 systemd 系统)
hostnamectl
在输出中查找 Operating System 行:
-
Ubuntu:
Operating System: Ubuntu 22.04.3 LTS -
CentOS:
Operating System: CentOS Linux 8 (Core)
总结
| 命令 | 是否通用 | 是否推荐 |
|---|---|---|
cat /etc/os-release |
✅ 是 | ✅ 推荐 |
lsb_release -d |
❌ 部分系统无 | ✅ 推荐 |
cat /etc/issue |
✅ 是 | ✅ 可用 |
hostnamectl |
✅ systemd 系统 | ✅ 推荐 |
如果你不确定系统类型,可以用上面任意一个方法快速判断。需要我帮你分析具体输出也可以贴出来看看 👇
云计算