可以通过以下几种方法快速确认云服务器的操作系统类型:
1. 查看系统信息文件
# 方法1:查看 /etc/os-release 文件(推荐)
cat /etc/os-release
# 方法2:查看 /etc/issue 文件
cat /etc/issue
# 方法3:查看 /etc/redhat-release(仅CentOS)
cat /etc/redhat-release 2>/dev/null || echo "不是CentOS"
2. 使用系统命令
# 方法1:hostnamectl 命令(systemd系统)
hostnamectl
# 方法2:lsb_release 命令(需要安装)
lsb_release -a
# 方法3:uname 命令(查看内核)
uname -a
3. 通过包管理器判断
# 检查包管理器
which apt # Ubuntu/Debian 使用 apt
which yum # CentOS/RHEL 7及以下使用 yum
which dnf # CentOS/RHEL 8及以上使用 dnf
4. 快速判断脚本
#!/bin/bash
if [ -f /etc/os-release ]; then
. /etc/os-release
echo "操作系统: $NAME"
echo "版本: $VERSION"
elif [ -f /etc/redhat-release ]; then
echo "CentOS/RHEL: $(cat /etc/redhat-release)"
else
echo "无法确定系统类型"
fi
5. 不同系统的特征对比
| 特征 | Ubuntu | CentOS |
|---|---|---|
| 默认包管理器 | apt | yum/dnf |
| 初始化系统 | systemd | systemd(CentOS 7+) |
| 默认Shell | bash | bash |
| 软件源配置 | /etc/apt/sources.list | /etc/yum.repos.d/ |
| 系统日志 | journalctl(systemd) | journalctl(CentOS 7+) |
6. 通过SSH连接时的提示
有些云服务商会在SSH登录时显示系统信息,注意查看登录后的欢迎信息。
最简单的方法:
直接执行这个命令:
cat /etc/os-release | grep -E "^(NAME|VERSION)="
输出示例:
- Ubuntu会显示:
NAME="Ubuntu" - CentOS会显示:
NAME="CentOS Linux"
选择其中一种方法即可快速确认你的云服务器操作系统。
CLOUD技术笔记