Linux - 获取系统版本信息

x33g5p2x  于2021-11-14 转载在 Linux  
字(1.7k)|赞(0)|评价(0)|浏览(325)

背景

写 shell 脚本的时候想根据系统版本来做条件判断,所以这篇就是这里搬那里搬,当做记录了

cat /proc/version

获取内核信息

[root@poloyy ~]# cat /proc/version
Linux version 4.18.0-240.22.1.el8_3.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC)) #1 SMP Thu Apr 8 19:01:30 UTC 2021

 uname -a

获取内核信息

[root@poloyy ~]# uname -a
Linux poloyy 4.18.0-240.22.1.el8_3.x86_64 #1 SMP Thu Apr 8 19:01:30 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

lsb_release -a

  • 获取系统信息
  • 有些系统会没有 lsb_release 命令
[root@poloyy ~]# lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch
Distributor ID:    CentOS
Description:    CentOS Linux release 8.3.2011Release:    8.3.2011Codename:    n/a

cat /etc/os-release

获取系统信息

[root@poloyy ~]# cat /etc/os-release
NAME="CentOS Linux"VERSION="8"ID="centos"ID_LIKE="rhel fedora"VERSION_ID="8"PLATFORM_ID="platform:el8"PRETTY_NAME="CentOS Linux 8"ANSI_COLOR="0;31"CPE_NAME="cpe:/o:centos:centos:8"HOME_URL="https://centos.org/"BUG_REPORT_URL="https://bugs.centos.org/"CENTOS_MANTISBT_PROJECT="CentOS-8"CENTOS_MANTISBT_PROJECT_VERSION="8"

cat /etc/redhat-release

仅适用于 Redhat 系的 Linux

[root@poloyy ~]# cat /etc/redhat-release
CentOS Linux release 8.3.2011

hostnamectl

获取系统信息

[root@poloyy ~]# hostnamectl
Static hostname: poloyy
Icon name: computer-vm
Chassis: vm
Machine ID: 20200519114807021642997698320941Boot ID: da0aa5943375449c8bdb8bdd63e162f3
Virtualization: kvm
Operating System: CentOS Linux 8CPE OS Name: cpe:/o:centos:centos:8Kernel: Linux 4.18.0-240.22.1.el8_3.x86_64
Architecture: x86-64

准确获取系统版本号

方式一
sudo cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'
方式二

有些系统会没有 lsb_release 命令

lsb_release -a|grep -e Release|awk -F ":" '{ print $2 }' | awk -F "." '{print $1}' | sed 's/[[:space:]]//g'

有空再对每个命令展开详解吧

相关文章