[Centos系统] centos6/7通用查看系统版本

396 0
Honkers 2026-6-3 03:37:07 来自手机 | 显示全部楼层 |阅读模式

方法一:可以用lsb_release -a来查看系统版本

  1. [root@centos6 ~]# lsb_release -a
  2. LSB 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-noarch
  3. Distributor ID: CentOS
  4. Description: CentOS release 6.9 (Final)
  5. Release: 6.9
  6. Codename: Final
复制代码

         但是,centos7.3里没有lsb_release命令,所以这种方法无法满足我的需要。

方法二:可以查看/etc/os-release文件来查看系统版本

  1. [root@centos7 app]# cat /etc/os-release
  2. NAME="CentOS Linux"
  3. VERSION="7 (Core)"
  4. ID="centos"
  5. ID_LIKE="rhel fedora"
  6. VERSION_ID="7"
  7. PRETTY_NAME="CentOS Linux 7 (Core)"
  8. ANSI_COLOR="0;31"
  9. CPE_NAME="cpe:/o:centos:centos:7"
  10. HOME_URL="https://www.centos.org/"
  11. BUG_REPORT_URL="https://bugs.centos.org/"
  12. CENTOS_MANTISBT_PROJECT="CentOS-7"
  13. CENTOS_MANTISBT_PROJECT_VERSION="7"
  14. REDHAT_SUPPORT_PRODUCT="centos"
  15. REDHAT_SUPPORT_PRODUCT_VERSION="7"
复制代码

    但是,centos6里没有这个文件,所以这个方法也不适用

方法三:可以查看/etc/redhat-release(/etc/centos-release)文件来查看系统版本

  1. [root@centos6 ~]# cat /etc/redhat-release
  2. CentOS release 6.9 (Final)
  3. [root@centos7 app]# cat /etc/redhat-release
  4. CentOS Linux release 7.3.1611 (Core)
复制代码

    可以看到,这个方法在两个版本中都可以使用,但是在两个版本中的显示结果不同,不好统一取版本值,所以这个方法也不能满足我的需求。

方法四:可以使用rpm -q centos-release来查看系统版本

  1. [root@centos6 ~]# rpm -q centos-release
  2. centos-release-6-9.el6.12.3.x86_64
  3. [root@centos7 app]# rpm -q centos-release
  4. centos-release-7-3.1611.el7.centos.x86_64
复制代码

    如上,这个命令在两个版本中显示结果格式一致,所以我们可以使用cut很轻松的取系统版本的值。

 

  1. [root@centos6 ~]# rpm -q centos-release|cut -d- -f3
  2. 6
  3. [root@centos7 ~]# rpm -q centos-release|cut -d- -f3
  4. 7
复制代码

方法五(最有效)

        cat  /etc/*release*

        然后就可以在脚本中进行判断系统版本了。

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行