本文共 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 version have three parts.
CentOS版本包括三个部分。
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 ~]#
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 ~]#
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 ~]#
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 ~]#
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 ~]#
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 ~]#
Here is a simple rpm macro evaluation to get the CentOS major version.
这是获得CentOS主版本的简单rpm宏评估。
[root@centos ~]# rpm -E %{rhel}8[root@centos ~]#
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容器 。
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版本。
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); }}
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 ~]#
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/