在Linux系统中,查看CentOS版本信息是一个常见的需求,通过多种命令,可以获取系统的详细信息,包括操作系统版本、内核版本、架构等,以下将详细介绍几种常用的方法:
1、使用cat 命令查看特定文件

/etc/redhatrelease 文件:该文件包含了CentOS的版本信息。
cat /etc/redhatrelease输出示例:
CentOS Linux release 7.9.2009 (Core)/etc/centosrelease 文件:这个文件也包含了CentOS的版本信息。
cat /etc/centosrelease输出示例:
CentOS Linux release 7.9.2009 (Core)/etc/issue 文件:显示系统发行版的信息。
cat /etc/issue输出示例:

CentOS release 6.8 (Final)2、使用lsb_release 命令
如果系统中已安装了lsb_release 命令,可以直接运行以下命令来查看系统版本。
lsb_release a输出示例:
LSB Version: :core4.1amd64:core4.1noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.9.2009 (Core)
Release: 7.9.2009
Codename: Core3、查看/etc/osrelease 文件
在CentOS 7及以上版本中,可以通过查看/etc/osrelease 文件来获取系统版本信息。
cat /etc/osrelease输出示例:

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_MANTISBTIS_PROJECT="CentOS7"
CENTOS_MANTISBTIS_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"4、使用hostnamectl 命令
hostnamectl 命令不仅可以显示主机名,还可以显示操作系统的相关信息。
hostnamectl输出示例:
Static hostname: localhost.localdomain
Icon name: computervm
Chassis: vm
Machine ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Boot ID: e8f8e3f7fd6b4c9e85d6b0c5f8dce8e5
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
Kernel: Linux 3.10.01160.el7.x86_64
Architecture: x86645、查看内核版本
使用uname 命令可以查看内核版本信息,虽然这不直接显示CentOS的版本,但内核版本通常与操作系统版本相关联。
uname r输出示例:
3.10.01160.el7.x86_64以下是关于上述方法的归纳和对比表格:
| 方法 | 命令 | 输出内容 | 适用版本 |
cat /etc/redhatrelease | cat /etc/redhatrelease | CentOS Linux release 7.9.2009 (Core) | 所有版本 |
lsb_release a | lsb_release a | LSB Version, Distributor ID, Description, Release, Codename | 需要安装lsb_release |
cat /etc/osrelease | cat /etc/osrelease | NAME, VERSION, ID, ID_LIKE, VERSION_ID, PRETTY_NAME, ANSI_COLOR, CPE_NAME等 | CentOS 7及以上 |
hostnamectl | hostnamectl | Static hostname, Icon name, Chassis, Machine ID, Boot ID, Virtualization, OS, Kernel | CentOS 7及以上 |
uname r | uname r | 3.10.01160.el7.x86_64 | 所有版本 |
FAQs
Q1: 如何在CentOS中查看系统的位数(32位或64位)?
A1: 可以使用以下命令之一来查看系统的位数:
1、getconf LONG_BIT 或getconf WORD_BIT
getconf LONG_BIT
返回结果为64表示系统是64位。
2、检查/bin/ls 文件的类型
file /bin/ls
输出示例:
/bin/ls: ELF 64bit LSB executable, x8664, version 1 (SYSV), dynamically linked, for GNU/Linux 2.6.18, stripped
如果显示 "ELF 64bit LSB",则表示系统是64位。
Q2: 为什么有些命令在某些CentOS版本上不可用?
A2: 某些命令如lsb_release 可能未在所有CentOS版本上默认安装,较旧的CentOS版本可能没有预装这个命令,在这种情况下,可以尝试安装相关软件包:
sudo yum install redhatlsbcore
然后再运行lsb_release a,不同版本的CentOS可能包含不同的文件和命令,因此建议根据具体版本选择合适的方法。
