方法一:可以用lsb_release -a来查看系统版本
[root@centos6 ~]# lsb_release -aLSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarchDistributor ID: CentOSDescription: CentOS release 6.9 (Final)Release: 6.9Codename: Final
但是,centos7.3里没有lsb_release命令,所以这种方法无法满足我的需要。
方法二:可以查看/etc/os-release文件来查看系统版本
-
[root@centos7 app]# cat /etc/os-release -
NAME="CentOS Linux" -
VERSION="7 (Core)" -
ID="centos" -
ID_LIKE="rhel fedora" -
VERSION_ID="7" -
PRETTY_NAME="CentOS Linux 7 (Core)" -
ANSI_COLOR="0;31" -
CPE_NAME="cpe:/o:centos:centos:7" -
HOME_URL="https://www.centos.org/" -
BUG_REPORT_URL="https://bugs.centos.org/" -
CENTOS_MANTISBT_PROJECT="CentOS-7" -
CENTOS_MANTISBT_PROJECT_VERSION="7" -
REDHAT_SUPPORT_PRODUCT="centos" -
REDHAT_SUPPORT_PRODUCT_VERSION="7"
但是,centos6里没有这个文件,所以这个方法也不适用
方法三:可以查看/etc/redhat-release(/etc/centos-release)文件来查看系统版本
-
[root@centos6 ~]# cat /etc/redhat-release -
CentOS release 6.9 (Final) -
[root@centos7 app]# cat /etc/redhat-release -
CentOS Linux release 7.3.1611 (Core)
可以看到,这个方法在两个版本中都可以使用,但是在两个版本中的显示结果不同,不好统一取版本值,所以这个方法也不能满足我的需求。
方法四:可以使用rpm -q centos-release来查看系统版本
-
[root@centos6 ~]# rpm -q centos-release -
centos-release-6-9.el6.12.3.x86_64 -
[root@centos7 app]# rpm -q centos-release -
centos-release-7-3.1611.el7.centos.x86_64
如上,这个命令在两个版本中显示结果格式一致,所以我们可以使用cut很轻松的取系统版本的值。
-
[root@centos6 ~]# rpm -q centos-release|cut -d- -f3 -
6 -
[root@centos7 ~]# rpm -q centos-release|cut -d- -f3 -
7
方法五(最有效)
cat /etc/*release*
然后就可以在脚本中进行判断系统版本了。
CentOS系统版本查看方法
本文介绍了五种在CentOS系统中查看系统版本的方法,包括使用lsb_release、os-release文件、redhat-release文件、rpm命令以及直接搜索所有可能的release文件。详细对比了这些方法在CentOS 6和CentOS 7中的适用性和结果一致性。
3590

被折叠的 条评论
为什么被折叠?



