Linux - 獲取系統版本信息

背景

寫 shell 腳本的時候想根據系統版本來做條件判斷,所以這篇就是這裏搬那裏搬,當做記錄了

 

cat /proc/version

獲取內核信息

[root@poloyy ~]# cat /proc/version
Linux version 4.18.0-240.22.1.el8_3.x86_64 ([email protected]) (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.2011
Release:    8.3.2011
Codename:    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: 20200519114807021642997698320941
Boot ID: da0aa5943375449c8bdb8bdd63e162f3
Virtualization: kvm
Operating System: CentOS Linux 8
CPE OS Name: cpe:/o:centos:centos:8
Kernel: 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'

 

有空再對每個命令展開詳解吧

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章