要判断服务器运行的是 Red Hat 还是 Ubuntu 系统,可以通过以下几种方法进行查看。以下是常用的命令和判断方式:
✅ 方法一:查看 /etc/os-release 文件(推荐)
这是最通用、最准确的方法,适用于大多数现代 Linux 发行版。
cat /etc/os-release
输出示例:
-
Ubuntu:
NAME="Ubuntu" VERSION="20.04.6 LTS (Focal Fossa)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 20.04.6 LTS" ... -
Red Hat Enterprise Linux (RHEL):
NAME="Red Hat Enterprise Linux" VERSION="8.8 (Ootpa)" ID="rhel" ID_LIKE="fedora" PRETTY_NAME="Red Hat Enterprise Linux 8.8 (Ootpa)" ...
通过 NAME 或 ID 字段即可判断。
✅ 方法二:使用 lsb_release 命令(主要适用于 Ubuntu)
lsb_release -a
- 如果系统是 Ubuntu,会显示详细版本信息。
- 如果是 RHEL/CentOS,可能提示命令未找到(除非安装了
redhat-lsb包)。
提示:可尝试
lsb_release -d只查看描述。
✅ 方法三:查看特定发行版文件
检查是否存在 Red Hat 相关文件:
cat /etc/redhat-release
- 如果输出类似
Red Hat Enterprise Linux Server release 7.9 (Maipo),则是 RHEL。 - CentOS 或 Fedora 也会有类似文件。
检查 Ubuntu 特有的文件:
cat /etc/issue
通常会显示:
Ubuntu 20.04.6 LTS n l
✅ 方法四:使用 hostnamectl 命令(systemd 系统通用)
hostnamectl
输出中会包含操作系统信息,例如:
Operating System: Ubuntu 20.04.6 LTS
Kernel: Linux 5.4.0-150-generic
Architecture: x86-64
或:
Operating System: Red Hat Enterprise Linux 8.8
✅ 方法五:通过包管理器判断
-
查看是否使用
yum或dnf(Red Hat 系):which yum || which dnf -
查看是否使用
apt(Ubuntu/Debian 系):which apt
虽然不能完全确定,但可以辅助判断。
总结:快速判断脚本
你可以运行以下命令一键判断:
if [ -f /etc/os-release ]; then
. /etc/os-release
echo "系统是: $PRETTY_NAME"
elif [ -f /etc/redhat-release ]; then
echo "Red Hat 系统: $(cat /etc/redhat-release)"
else
echo "无法识别系统类型"
fi
✅ 推荐顺序:
cat /etc/os-releasehostnamectlcat /etc/*-release
这些方法安全、可靠,不会影响系统运行。
CDNK博客