如何检查CentOS版本– 8种方法

本文介绍了通过8种不同方法检查CentOS版本的教程,包括查看特定系统文件、使用lsb_release命令、rpm查询等。同时,还提供了用Java和Python编程检查版本的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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-release
CentOS 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-release
CentOS 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-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文件 (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-release
CentOS 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 -d
Description:	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-release
centos-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.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 ~]# 

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 Script

import platform

print('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版本。

翻译自: https://www.journaldev.com/39919/how-to-check-centos-version

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值