怎么查看系统是ubuntu还是centos?

服务器

你可以通过以下几种方法来查看当前系统是 Ubuntu 还是 CentOS


✅ 方法一:使用命令查看系统发行版信息

cat /etc/os-release

这个文件会显示系统的详细信息,包括名称、版本等。例如:

  • Ubuntu 会显示:

    NAME="Ubuntu"
    VERSION="20.04.6 LTS (Focal Fossa)"
    ...
  • CentOS 会显示:

    NAME="CentOS Linux"
    VERSION="7 (Core)"
    ...

✅ 方法二:使用 lsb_release 命令(适用于 Ubuntu)

lsb_release -d

输出示例:

  • Ubuntu
    Description:    Ubuntu 20.04.6 LTS

注意:CentOS 默认可能没有安装 lsb_release,需要手动安装(如:yum install redhat-lsb-core)。


✅ 方法三:查看特定的系统文件

对于 CentOS:

cat /etc/centos-release

输出示例:

CentOS Linux release 7.9.2009 (Core)

对于 Ubuntu:

cat /etc/issue

输出示例:

Ubuntu 20.04.6 LTS n l

✅ 方法四:使用 hostnamectl(如果系统使用 systemd)

hostnamectl

这会显示操作系统和内核信息,例如:

Operating System: Ubuntu 20.04.6 LTS
         CPE OS Name: cpe:/o:canonical:ubuntu_linux:20.04

或者:

Operating System: CentOS Linux 7 (Core)

✅ 方法五:用简单的判断脚本(可选)

你可以写一个简单的 shell 脚本来判断:

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 -sd)"
else
    echo "Unknown OS"
fi

总结

方法 是否通用 说明
cat /etc/os-release ✅ 是 推荐使用
lsb_release -d ✅ 是 需要安装 lsb 工具(CentOS 可能需额外安装)
cat /etc/centos-release ❌ CentOS 专用
cat /etc/issue ✅ 是 简单但格式不统一
hostnamectl ✅ 是(systemd 系统) 显示完整系统信息

如果你还有其他系统(比如 Debian、Fedora、Rocky Linux),这些方法也基本适用。

需要我帮你判断你当前系统的类型?可以把命令执行结果发给我 😊

未经允许不得转载:CDNK博客 » 怎么查看系统是ubuntu还是centos?