要查看 Linux 服务器是 Ubuntu 还是 CentOS,可以使用以下几种方法:
方法一:查看 /etc/os-release 文件(推荐)
cat /etc/os-release
输出示例:
- Ubuntu 中会包含:
NAME="Ubuntu" VERSION="20.04.6 LTS (Focal Fossa)" ID=ubuntu - CentOS 中会包含:
NAME="CentOS Linux" VERSION="7 (Core)" ID="centos"
✅ 这是最标准、最通用的方法,适用于大多数现代 Linux 发行版。
方法二:查看 /etc/issue 文件
cat /etc/issue
输出可能为:
- Ubuntu:
Ubuntu 20.04.6 LTS n l - CentOS:
CentOS Linux release 7.9.2009 (Core)
方法三:使用 lsb_release 命令(主要适用于 Ubuntu/Debian)
lsb_release -a
如果系统是 Ubuntu,会显示详细版本信息。
CentOS 默认可能未安装 lsb_release,可运行:
yum install -y redhat-lsb-core # CentOS 7/8
方法四:查看特定发行版文件
# 检查是否存在 Ubuntu 特有的文件
ls /etc/lsb-release &>/dev/null && echo "可能是 Ubuntu"
# 检查 CentOS 或 RHEL 相关文件
ls /etc/centos-release &>/dev/null && cat /etc/centos-release
ls /etc/redhat-release &>/dev/null && cat /etc/redhat-release
- 输出如:
CentOS Linux release 7.9.2009 (Core)→ 是 CentOS - 输出如:
Ubuntu 20.04.6 LTS→ 是 Ubuntu
方法五:使用 hostnamectl 命令
hostnamectl
输出中会包含操作系统信息,例如:
Operating System: Ubuntu 20.04.6 LTS
CPE OS Name: cpe:/o:canonical:ubuntu_linux:20.04
总结:一键判断脚本
你可以运行下面这一行命令快速判断:
grep '^ID=' /etc/os-release | awk -F= '{print $2}' | tr -d '"'
输出结果:
ubuntu→ 是 Ubuntucentos→ 是 CentOS
✅ 推荐组合命令(简洁可靠):
source /etc/os-release && echo "系统是: $NAME, 版本: $VERSION"
这样就能清楚知道你的服务器是 Ubuntu 还是 CentOS 了。
ECLOUD博客