編碼的世界 / 優質文選 / 感情

如何查看CentOS版本


2022年6月08日
-   

轉載來源:https://linuxconfig.org/how-to-check-centos-version
 

如何查看CentOS版本


 
有幾種方法可以檢查系統上運行的CentOS版本。檢查CentOS版本號的最簡單方法是執行cat /etc/centos-release命令。可能需要確定准確的CentOS版本,以幫助您或您的支持團隊對CentOS系統進行故障排除。
 
CentOS版本包含三個版本,如下列所示:
CentOS版本包括Major,Minor和Asynchronous Release number。

 
檢查CentOS版本的命令是什麼?
下表包含有關如何在CentOS Linux服務器或桌面上檢查CentOS版本的最常見和推薦的方法。 
命令描述
$ rpm -q centos-releaseCentOS版本適用於CentOS 6及更高版本。顯示主要,次要和異步CentOS版本的原因。
$ lsb_release -d需要redhat-lsb在執行之前安裝包。
$ cat /etc/centos-releaseLinux cat命令輸出/etc/centos-release查詢CentOS版本的內容。適用於CentOS 6及更高版本。

 
檢查CentOS版本的替代命令
如果上面提供的命令無法幫助您獲取CentOS版本號,您可以嘗試以下替代命令。 雖然僅適用於CentOS版本7及更高版本,但該hostnamectl命令可能會為您提供有關您的操作系統版本號的重要線索:
$ hostnamectl
Static hostname: localhost.localdomain
Icon name: computer-vm
Chassis: vm
Machine ID: fe069af6a1764e07be909d7cf64add99
Boot ID: b81bb73dc549484c8927e830e149eb55
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-862.6.3.el7.x86_64
Architecture: x86-64

要獲得更多答案,請嘗試查詢/etc目錄中的所有發布文件:
$ cat /etc/*elease
CentOS Linux release 7.5.1804 (Core)
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"
CentOS Linux release 7.5.1804 (Core)
CentOS Linux release 7.5.1804 (Core)

您運行的系統可能已定義宏,可幫助您識別CentOS Linux服務器的主要發行版本。請嘗試以下方法:
$ rpm eval '%{centos_ver}'
7

最後,您的GRUB引導手冊可能會提供一些答案。這不是檢查CentOS版本最可靠的方法,但它可能會為您提供一些線索:
#grep -w menuentry /boot/grub2/grub.cfg /etc/grub2.cfg
/boot/grub2/grub.cfg:menuentry'CentOS Linux(3.10.0-862.6.3.el7.x86_64)7(Core)' - class centos class gnu-linux class gnu class os - -unrestricted $ menuentry_id_option'gnulinux-3.10.0-693.el7.x86_64-advanced-176eba78-e8ec-475d-9086-0d582fcd4305'{

 
使用編程來檢查CentOS版本
如果您希望自行編程以自動檢查CentOS版本,您可以選擇多種選項。本節將列出如何使用Bash腳本和Python編程語言檢查CentOS版本的一些基本示例。
 
Bash腳本檢查CentOS版本
以下bash腳本可用於獲取CentOS版本號,前提是/etc/centos-release文件存在並已填充。作為示例,在適當的地方進行修改:
#!/bin/bash
full=`cat /etc/centos-release | tr -dc '0-9.'`
major=$(cat /etc/centos-release | tr -dc '0-9.'|cut -d . -f1)
minor=$(cat /etc/centos-release | tr -dc '0-9.'|cut -d . -f2)
asynchronous=$(cat /etc/centos-release | tr -dc '0-9.'|cut -d . -f3)
echo CentOS Version: $full
echo Major Relase: $major
echo Minor Relase: $minor
echo Asynchronous Relase: $asynchronous

輸出:
$ ./check-centos-version.sh
CentOS Version: 7.5.1804
Major Relase: 7
Minor Relase: 5
Asynchronous Relase: 1804

 
用於檢查CentOS版本的Python程序
以下python腳本將輸出分發名稱以及OS版本號:
#!/usr/bin/python
import platform
print platform.linux_distribution()

Output:
$ python check-centos-version.py
('CentOS Linux', '7.5.1804', 'Core')

熱門文章