識別CentOS和Ubuntu的系統版本 一、用 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 二、從定義好的配置文件中讀取 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 三、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