识别CentOS和Ubuntu的系统版本
1、用 lsb-release
#!/bin/bash
Install_LSB()
{
if [ "$PM" = "yum" ]; then
yum install -y redhat-lsb centos-release
elif [ "$PM" = "apt" ]; then
apt-get update
apt-get install -y lsb-release
fi
}
Install_LSB
lsb_release -d
2、从定义好的配置文件中读取
CentOS 中相关的文件
cat /etc/centos-release
cat /etc/redhat-release (/etc/redhat-release -> centos-release)
cat /etc/system-release (/etc/system-release -> centos-release)
rpm -q centos-release
# rpm -q redhat-release
如果是CentOS7,支持 cat /etc/os-release
Ubuntu 中相关的文件
cat /etc/os-release (/etc/os-release -> ../usr/lib/os-release)
cat /etc/lsb-release
在CentOS和Ubuntu中也可以这样读系统版本,不过 /etc/issue (登录欢迎信息)有时候是空的
cat /etc/issue
#cat /etc/*elease
for i in $(ls /etc/*release); do echo ===$i===; cat $i; done
3、hostnamectl
在CentOS7和Ubuntu中,通过 hostnamectl 读系统版本
hostnamectl
Ubuntu中这样安装 hostnamectl
apt install -y systemd systemd-services apt-file
apt-file update
apt-file search hostnamectl
/usr/bin/hostnamectl