博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何检查CentOS版本– 8种方法
阅读量:2541 次
发布时间:2019-05-11

本文共 5981 字,大约阅读时间需要 19 分钟。

There are many ways to check the CentOS version. In this tutorial, we will learn how to check the CentOS version through eight different utilities.

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

检查CentOS版本的8种方法 (8 Ways to Check CentOS Version)

  1. /etc/centos-release file

    / etc / centos-release文件
  2. /etc/system-release file

    / etc / system-release文件
  3. /etc/os-release file

    / etc / os-release文件
  4. /etc/redhat-release file

    / etc / redhat-release文件
  5. lsb_release command

    lsb_release命令
  6. rpm query

    rpm查询
  7. rpm macro

    rpm宏
  8. hostnamectl command

    hostnamectl命令

CentOS版本号是什么意思? (What does CentOS Version Number Means?)

CentOS version have three parts.

CentOS版本包括三个部分。

  1. Major Version: Major release version number

    主要版本:主要发行版本号
  2. Minor Version: Minor release version number

    次要版本:次要发行版本号
  3. Monthstamp: codebase month and year timestamp

    Monthstamp:代码库的月份和年份时间戳

1. / etc / centos-release文件 (1. /etc/centos-release file)

The CentOS specific file, which contains the CentOS release and version details.

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

[root@centos ~]# cat /etc/centos-releaseCentOS Linux release 8.1.1911 (Core) [root@centos ~]#
  • Major Version: 8

    主要版本:8
  • Minor Version: 1

    次要版本:1
  • Monthstamp: 1911 i.e. November 2019.

    月戳:1911年,即2019年11月。
Centos Release Version
Centos Release Version
Centos发行版本

2. / etc / system-release文件 (2. /etc/system-release file)

This file also contains the same information as the centos-release file.

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

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

3. / etc / os-release文件 (3. /etc/os-release file)

This file contains the Linux OS information. You can get the major version number from this file.

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

[root@centos ~]# cat /etc/os-releaseNAME="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文件 (4. /etc/redhat-release file)

CentOS is built on top of RedHat Linux distribution. This file also contains the same information as the centos-release file.

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

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

5. lsb_release命令 (5. lsb_release command)

This command doesn’t come pre-installed with the CentOS server. You can install it using the following command.

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

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

Then check the CentOS version using the following command.

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

[root@centos ~]#  lsb_release -dDescription:	CentOS Linux release 8.1.1911 (Core) [root@centos ~]#
Lsb Release Centos Version

6. rpm查询 (6. rpm query)

We can query the rpm package manager to get the CentOS version information.

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

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

7. rpm宏 (7. rpm macro)

Here is a simple rpm macro evaluation to get the CentOS major version.

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

[root@centos ~]# rpm -E %{rhel}8[root@centos ~]#
Rpm Macro Centos Version

8. hostnamectl命令 (8. hostnamectl command)

The hostnamectl command can be used to get the system information. It also reveals the Operating System version.

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

[root@centos ~]# hostnamectl   Static hostname: localhost.localdomainTransient 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 ~]#

Are you wondering why this command is printing CentOS 7, whereas others are printing CentOS 8?

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

It’s because the hostnamectl command doesn’t work in Docker containers. This command output is from my own VPS machine whereas the other commands output is from my local Docker CentOS 8 container.

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

如何以编程方式检查CentOS版本? (How to Check CentOS Version Programatically?)

I am a Java and Python programmer. Let’s see how to find the CentOS version in Java and Python programs.

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

1. Java程序以打印CentOS版本 (1. Java Program to Print CentOS Version)

There is no specific Java class that provides operating system information.

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

I am just reading the /etc/centos-release file and then printing it to the console.

我只是在读取/ 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);	}}
Centos Version Java Program

2.打印CentOS版本的Python程序 (2. Python Program to Print CentOS Version)

We can use the platform module to get the operating system information.

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

# Python 3 Scriptimport platformprint('Server Details:', platform.linux_distribution())

I am running this on my CentOS 7 VPS server, where Python is pre-installed.

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

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

结论 (Conclusion)

There are many ways to find out the CentOS version information. We also learned how to get the CentOS version in Python and Java programs.

有很多找到CentOS版本信息的方法。 我们还学习了如何在Python和Java程序中获得CentOS版本。

翻译自:

转载地址:http://whlzd.baihongyu.com/

你可能感兴趣的文章
VsVim - Shortcut Key (快捷键)
查看>>
C++练习 | 模板与泛式编程练习(1)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>
RTMP
查看>>
求一个数的整数次方
查看>>
点云PCL中小细节
查看>>
铁路信号基础
查看>>
RobotFramework自动化2-自定义关键字
查看>>
[置顶] 【cocos2d-x入门实战】微信飞机大战之三:飞机要起飞了
查看>>
BABOK - 需求分析(Requirements Analysis)概述
查看>>
第43条:掌握GCD及操作队列的使用时机
查看>>
Windows autoKeras的下载与安装连接
查看>>
CMU Bomblab 答案
查看>>
微信支付之异步通知签名错误
查看>>
2016 - 1 -17 GCD学习总结
查看>>
linux安装php-redis扩展(转)
查看>>
Vue集成微信开发趟坑:公众号以及JSSDK相关
查看>>
技术分析淘宝的超卖宝贝
查看>>