走啊走
加油

怎么看系统是ubuntu还是centos?

服务器价格表

如何快速区分系统是Ubuntu还是CentOS

结论:通过检查系统发行版信息文件、查看包管理器类型或运行特定命令,可以快速准确判断当前系统是Ubuntu还是CentOS。

一、通过系统文件直接识别

1. 检查/etc/os-release文件

这是最通用的方法,适用于大多数现代Linux发行版:

cat /etc/os-release
  • Ubuntu会显示包含"Ubuntu"ID=ubuntu的字段
  • CentOS会显示"CentOS"ID="centos"(CentOS 7/8)或ID="rhel"(CentOS Stream)

2. 检查传统发行版文件

  • Ubuntu特有文件:

    cat /etc/lsb-release

    输出会包含DISTRIB_ID=Ubuntu

  • CentOS特有文件:

    cat /etc/centos-release  # CentOS 7及以下
    cat /etc/redhat-release # CentOS 8/Stream可能也使用

二、通过包管理器识别

不同发行版使用不同的包管理工具,这是最直观的区分特征:

  • Ubuntu/Debian系

    which apt  # 会返回/usr/bin/apt

    核心特征:使用aptdpkg管理软件包

  • CentOS/RHEL系

    which yum   # CentOS 7及以下
    which dnf   # CentOS 8/Stream

    核心特征:使用yumdnf管理软件包

三、通过命令输出识别

1. 使用hostnamectl命令(systemd系统)

hostnamectl | grep "Operating System"
  • Ubuntu输出示例:Operating System: Ubuntu 22.04 LTS
  • CentOS输出示例:Operating System: CentOS Linux 7 (Core)

2. 使用uname -a辅助判断

虽然不能直接显示发行版,但结合内核版本可以辅助推断:

  • Ubuntu通常使用较新的内核版本(如5.x)
  • CentOS 7默认使用3.10内核,CentOS 8/Stream使用4.x+

四、通过默认目录结构和服务差异

  • Ubuntu

    • Apache配置文件路径:/etc/apache2/
    • 默认使用netplan管理网络(较新版本)
  • CentOS

    • Apache配置文件路径:/etc/httpd/
    • 默认使用NetworkManagernetwork-scripts

五、快速验证脚本

if [ -f /etc/os-release ]; then
    . /etc/os-release
    echo "系统是: $NAME $VERSION"
elif [ -f /etc/centos-release ]; then
    echo "系统是: $(cat /etc/centos-release)"
else
    echo "无法确定系统类型"
fi

总结建议

  1. 首选方法:检查/etc/os-release文件,这是最标准化的方式
  2. 快速判断:运行which aptwhich yum包管理器类型是区分Ubuntu和CentOS的最显著特征
  3. 注意版本差异:CentOS 8之后转向dnf包管理器,而Ubuntu始终保持apt

通过以上任一方法,都可在10秒内准确判断系统类型。对于自动化脚本,建议使用/etc/os-release解析,这是最可靠的跨发行版方案。