在云服务器中查看CentOS或Ubuntu版本,可以使用以下命令:
通用方法(适用于大多数Linux发行版)
1. 查看发行版信息
# 查看发行版详细信息
cat /etc/os-release
# 或使用以下命令
lsb_release -a
2. 查看内核版本
# 查看内核版本
uname -r
# 查看完整系统信息
uname -a
CentOS/RHEL系统专用
1. 查看CentOS版本
# CentOS 7及以下版本
cat /etc/redhat-release
# CentOS 8及以上版本
cat /etc/centos-release
2. 使用hostnamectl(CentOS 7+)
hostnamectl
Ubuntu/Debian系统专用
1. 查看Ubuntu版本
# 查看版本信息
cat /etc/issue
# 或查看更详细的信息
cat /etc/lsb-release
快速判断系统类型
1. 一键判断脚本
# 执行以下命令可快速判断系统类型和版本
if [ -f /etc/redhat-release ]; then
echo "CentOS/RHEL系统:"
cat /etc/redhat-release
elif [ -f /etc/lsb-release ]; then
echo "Ubuntu/Debian系统:"
cat /etc/lsb-release | grep DESCRIPTION
else
echo "其他Linux系统"
cat /etc/os-release
fi
常用组合命令
1. 简洁显示版本
# 显示发行版名称和版本号
cat /etc/os-release | grep -E "PRETTY_NAME|VERSION_ID"
# 或使用
hostnamectl | grep -E "Operating System|Kernel"
2. 仅显示版本号
# CentOS
cat /etc/redhat-release | awk '{print $4}'
# Ubuntu
cat /etc/os-release | grep VERSION_ID | cut -d '"' -f 2
示例输出
CentOS示例:
$ cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
Ubuntu示例:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal
建议
- 首选命令:
cat /etc/os-release– 最通用,几乎所有Linux发行版都支持 - 快速查看:
hostnamectl– 显示信息全面且格式美观(需要systemd) - 兼容性考虑:如果脚本需要跨平台,建议使用
/etc/os-release
这些命令在云服务器(如阿里云、腾讯云、AWS等)中同样适用,因为云服务器通常使用标准的Linux发行版镜像。
CLOUD技术笔记