查看CentOS6/7/8系统版本
要写一个CentOS系统的初始化脚本,但是CentOS6、CentOS7、CentOS8版本有很多命令都不相同,所以为了让脚本在两个版本之间都可以使用,就需要对CentOS系统版本进行判断。
通过查找各种资料,我发现基本有下面几种查看系统版本的方法:
方法一:
要写一个CentOS系统的初始化脚本,但是CentOS6、CentOS7、CentOS8版本有很多命令都不相同,所以为了让脚本在两个版本之间都可以使用,就需要对CentOS系统版本进行判断。
通过查找各种资料,我发现基本有下面几种查看系统版本的方法:
lsb_release -a
[root@centos6 ~]# lsb_release -a
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
Distributor ID: CentOS
Description: CentOS release 6.9 (Final)
Release: 6.9
Codename: Final
但是,CentOS7.3+里没有lsb_release命令,所以这种方法无法满足我的需要。
方法二:
cat /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里没有这个文件,所以这个方法也不适用。
方法三(推荐):
cat /etc/redhat-release
or cat /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)
可以看到,这个方法在三个版本中都可以使用,可以使用sed命令取版本的值
[root@centos6 ~]# cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'
6
[root@centos7 ~]# cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'
7
方法四:
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
如上,这个命令在CentOS6和CentOS7两个版本中显示结果格式一致,所以我们可以使用cut很轻松的取系统版本的值。
[root@centos6 ~]# rpm -q centos-release|cut -d- -f3
6
[root@centos7 ~]# rpm -q centos-release|cut -d- -f3
7
然后就可以在脚本中使用这个值判断系统版本了。
最新评论
有快3个月了没续费了,如何重新激活我的服务?
楼主,有一点不明白: “准备好你的服务器,确认账号(一般是 root)和密码,系统建议 Centos7 ×64+、Ubuntu、Debian”是指自己要准备一台安装了上述系统的电脑做服务器还是指购买的
网上一堆的节点怎么填写,看不懂得
请问证书会过期嘛?要怎么处理
好怕被墙,vmess+ws
搞定了!网速很快哦,感谢感谢!不过最后一步还是要提醒小白们一下,v2ray设置好以后,要点下电脑右下角的V,点击鼠标右键,点”系统代理”,点“自动配置系统代理”,V变成红色了,就可以google了。