如何检查CentOS版本

时间:2020-02-23 14:38:18  来源:igfitidea点击:

有很多方法可以检查CentOS版本。
在本教程中,我们将学习如何通过八个不同的实用程序来检查CentOS版本。

检查CentOS版本的8种方法

-/etc/centos-release文件
-/etc/system-release文件
-/etc/os-release文件
-/etc/redhat-release文件

  • lsb_release命令
  • rpm查询
  • rpm宏
  • hostnamectl命令

CentOS版本号是什么意思?

CentOS版本包括三个部分。

  • 主要版本:主要发行版本号
  • 次要版本:次要发行版本号
  • Monthstamp:代码库的月份和年份时间戳

1./etc/centos-release文件

特定于CentOS的文件,其中包含CentOS发行版和版本详细信息。

[root@centos ~]# cat /etc/centos-release
CentOS Linux release 8.1.1911 (Core) 
[root@centos ~]# 

  • 主要版本:8
  • 次要版本:1
  • 月戳:1911年,即2019年11月。

Centos发行版本

2./etc/system-release文件

该文件还包含与centos-release文件相同的信息。

[root@centos ~]# cat /etc/system-release
CentOS Linux release 8.1.1911 (Core) 
[root@centos ~]#

3./etc/os-release文件

该文件包含Linux OS信息。
您可以从该文件中获取主要版本号。

[root@centos ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="8 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="8"

[root@centos ~]# 

4./etc/redhat-release文件

CentOS建立在RedHat Linux发行版之上。
该文件还包含与centos-release文件相同的信息。

[root@centos ~]# cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core) 
[root@centos ~]#

5. lsb_release命令

CentOS服务器未预安装此命令。
您可以使用以下命令进行安装。

[root@centos ~]# yum install redhat-lsb

然后使用以下命令检查CentOS版本。

[root@centos ~]#  lsb_release -d
Description:	CentOS Linux release 8.1.1911 (Core) 
[root@centos ~]#

6. rpm查询

我们可以查询rpm软件包管理器以获取CentOS版本信息。

[root@centos ~]# rpm -q centos-release
centos-release-8.1-1.1911.0.8.el8.x86_64
[root@centos ~]# 

7. rpm宏

这是获得CentOS主版本的简单rpm宏评估。

[root@centos ~]# rpm -E %{rhel}
8
[root@centos ~]#

8. hostnamectl命令

hostnamectl命令可用于获取系统信息。
它还显示了操作系统版本。

[root@centos ~]# hostnamectl
 Static hostname: localhost.localdomain
Transient hostname: li1176-240.members.linode.com
       Icon name: computer-vm
         Chassis: vm
      Machine ID: c2a4bfa7e0c74457b3a978656ab959e8
         Boot ID: c89bae2d3ec7493987a455bfa15e4818
  Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
     CPE OS Name: cpe:/o:centos:centos:7
          Kernel: Linux 3.10.0-1062.12.1.el7.x86_64
    Architecture: x86-64
[root@centos ~]# 
[root@centos ~]# 
[root@centos ~]# hostnamectl | grep "Operating System"
Operating System: CentOS Linux 7 (Core)
[root@centos ~]# 

您是否想知道为什么此命令打印CentOS 7,而其他命令打印CentOS 8?

这是因为hostnamectl命令在Docker容器中不起作用。
此命令输出来自我自己的VPS计算机,而其他命令输出来自我的本地Docker CentOS 8容器。

如何以编程方式检查CentOS版本?

我是Java和Python程序员。
让我们看看如何在Java和Python程序中找到CentOS版本。

1. Java程序打印的CentOS版本

没有提供操作系统信息的特定Java类。

我只是在读取/etc/centos-release文件,然后将其打印到控制台。

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class CentOSVersion {

	public static void main(String[] args) throws IOException {
		String centosVersion = new String(Files.readAllBytes(Paths.get("/etc/centos-release")));

		System.out.println(centosVersion);
	}

}

2.打印CentOS版本的Python程序

我们可以使用"平台"模块来获取操作系统信息。

# Python 3 Script

import platform

print('Server Details:', platform.linux_distribution())

我正在预先安装了Python的CentOS 7 VPS服务器上运行此文件。

[root@centos ~]# python3 centos-version.py 
Server Details: ('CentOS Linux', '7.7.1908', 'Core')
[root@centos ~]#