[Linux]查看硬件及操作系統信息


許多的軟件產品對硬件及操作系統等環境是有具體要求的,那么這時候如何快速知曉目標機器的目標資源信息是較為頻繁的操作。

命令

  • 全部硬件及系統信息
    • dmidecode (軟硬件全部信息)
    • hostnamectl (系統信息: OS發行版、CPU架構、虛擬化、主機名、Linux內核版本)
    • uname -a (系統信息: CPU架構、主機名、Linux內核版本等)
  • BIOS
    • dmidecode -t bios
  • CPU(指令集/架構/核數等)
    • [arch] / [uname -p] / [uname -m] / [uname -i] / [lscpu | awk 'NR1{print}'] / [hostnamectl | awk 'NR12{print}'] # 查看CPU架構
    • gcc -march=native -Q --help=target|grep march # 查看具體支持的CPU指令集
    • [lscpu] # 查看CPU硬件詳情信息(CPU架構、CPU模型、處理器個數/核數、模型名稱/廠家、L1/L2/L3的Cache、Hypervisor vendor、cpu family[CPU產品系列代號]等)
    • cat /proc/cpuinfo # 查看CPU的各處理器詳情
    • getconf LONG_BIT 查看當前運行模式(運行位數)
  • 內存/swap區
    • cat /proc/meminfo
    • [cat /proc/swaps] 查看哪些swap區被使用
    • [free -h] 單位:KB / [free -m] 單位:MB / [free -g] 單位: GB
  • 硬盤/分區/文件系統(掛載點)
    • mount | column -t
    • fdisk -l
    • lsblk
    • df -hT
    • [cat /proc/mounts] 查看已加載的文件系統
  • PCI設備/USB設備
    • lspci
    • lsusb
  • 網絡接口(網卡)/MAC/IP等
    • lspci | grep -i 'eth'
    • ifconfig -a
      • cat /sys/class/net/ens192/address # 查看MAC地址
    • ip addr
    • ip link show
    • ethtool eth0
    • [cat /proc/net/dev] 顯示網絡適配器(網卡)及統計
  • 主機名(主機名!= 域名)
    • [uname -n] / [hostname] # 查看服務器的主機名
      • hostnamectl set-hostname xxxx 重置hostname
  • Linux內核信息
    • [uname -r/--kernel-release] / [cat /proc/version] / [hostnamectl | awk 'NR==11{print}'] # 查看Linux內核版本信息
  • OS/Linux發行版信息
    • echo $OSTYPE # 查看操作系統類型(darwin:Windows/cygwin:Windows/msys:Windows/win32:Windows/freebsd:freebsd/linux-gnu:Linux)
    • cat /etc/os-release # 查看操作系統詳情(發行版類型、NAME/VERSION/ID/ID_LIKE/PRETTY_NAME/ANSI_COLOR/CPE_NAME/HOME_URL/BUG_REPORT_URL/...)
    • [cat /etc/system-release] / [cat /etc/redhat-release] / [cat /etc/centos-release] # 查看操作發行版類型及版本信息
  • 其他
    • [cat /proc/interrupts] 顯示中斷

[0] dmidecode -q/-t/-s

簡介

(Desktop Management Interface,幫助收集電腦系統信息的管理系統,DMI信息的收集必須在嚴格遵照SMBIOS規范的前提下進行;可輸出的Linux服務器硬件信息包括 BIOS、系統、主板、處理器、內存、緩存等)
0 BIOS

1 System

2 Base Board

3 Chassis

4 Processor

5 Memory Controller

6 Memory Module

7 Cache

8 Port Connector

9 System Slots

10 On Board Devices

11 OEM Strings

12 System Configuration Options

13 BIOS Language

14 Group Associations

15 System Event Log

16 Physical Memory Array

17 Memory Device

18 32-bit Memory Error

19 Memory Array Mapped Address

20 Memory Device Mapped Address

21 Built-in Pointing Device

22 Portable Battery

23 System Reset

24 Hardware Security

25 System Power Controls

26 Voltage Probe

27 Cooling Device

28 Temperature Probe

29 Electrical Current Probe

30 Out-of-band Remote Access

31 Boot Integrity Services

32 System Boot

33 64-bit Memory Error

34 Management Device

35 Management Device Component

36 Management Device Threshold Data

37 Memory Channel

38 IPMI Device

39 Power Supply

dmidecode -t bios

查看bios信息
Dmidecode:這款軟件允許你在 Linux 系統下獲取有關硬件方面的信息。Dmidecode 遵循 SMBIOS/DMI 標准,其輸出的信息包括 BIOS、系統、主板、處理器、內存、緩存等等。
Dmidecode命令格式 : dmidecode -t typecode (dmidecode -t 0 等效於 dmidecode -t BIOS)
dmidecode –q : 查看所有硬件信息(CPU、硬盤、網卡、磁盤等硬件)

[linux-shell]# dmidecode -t bios
# dmidecode 3.0
Getting SMBIOS data from sysfs.
SMBIOS 2.8 present.

Handle 0x0000, DMI type 0, 24 bytes
BIOS Information
	Vendor: SeaBIOS
	Version: rel-1.10.2-0-g5f4c7b1-20181220_000000-szxrtosci10000
	Release Date: 04/01/2014
	Address: 0xE8000
	Runtime Size: 96 kB
	ROM Size: 64 kB
	Characteristics:
		BIOS characteristics not supported
		Targeted content distribution is supported
	BIOS Revision: 0.0

dmidecode -t system

[linux-shell]# dmidecode -t system
# dmidecode 3.0
Getting SMBIOS data from sysfs.
SMBIOS 2.8 present.

Handle 0x0100, DMI type 1, 27 bytes
System Information
	Manufacturer: OpenStack Foundation
	Product Name: OpenStack Nova
	Version: 13.2.2-409
	Serial Number: 125c53ca-477d-43cc-ac48-512e86f01b3c
	UUID: 125C53CA-477D-43CC-AC48-512E86F01B3C
	Wake-up Type: Power Switch
	SKU Number: Not Specified
	Family: Virtual Machine

Handle 0x2000, DMI type 32, 11 bytes
System Boot Information
	Status: No errors detected

[1] lscpu

display information about the CPU architecture

Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    2
Core(s) per socket:    2
座:                 1
NUMA 節點:         1
廠商 ID:           GenuineIntel
CPU 系列:          6
型號:              58
型號名稱:        Intel(R) Core(TM) i3-3240T CPU @ 2.90GHz
步進:              9
CPU MHz:             1599.743
CPU max MHz:           2900.0000
CPU min MHz:           1600.0000
BogoMIPS:            5786.80
虛擬化:           VT-x
L1d 緩存:          32K
L1i 緩存:          32K
L2 緩存:           256K
L3 緩存:           3072K
NUMA 節點0 CPU:    0-3

[2] cat /proc/cpuinfo

查看CPU詳細信息: cat /proc/cpuinfo
查看cpu運行模式: getconf LONG_BIT

processor	: 0
...

processor	: 1
vendor_id	: GenuineIntel
cpu family	: 6
model		: 58
model name	: Intel(R) Core(TM) i3-3240T CPU @ 2.90GHz
stepping	: 9
microcode	: 0x21
cpu MHz		: 1627.709
cache size	: 3072 KB
physical id	: 0
siblings	: 4
core id		: 1
cpu cores	: 2
apicid		: 2
initial apicid	: 2
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm pcid sse4_1 sse4_2 popcnt tsc_deadline_timer xsave avx f16c lahf_lm epb ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms xsaveopt dtherm arat pln pts md_clear spec_ctrl intel_stibp flush_l1d
bogomips	: 5786.80
clflush size	: 64
cache_alignment	: 64
address sizes	: 36 bits physical, 48 bits virtual
power management:

processor	: 2
...

processor	: 3
...

[3] free -m

m: 單位為MB

              total        used        free      shared  buff/cache   available
Mem:          11903        3996        2650         265        5256        7329
Swap:          8191           0        8191

[4] cat /proc/meminfo

查看內存詳細使用

MemTotal:       12188892 kB
MemFree:         2714864 kB
MemAvailable:    7505604 kB
Buffers:            1088 kB
Cached:          5190608 kB
SwapCached:            0 kB
Active:          5281500 kB
Inactive:        3602352 kB
Active(anon):    3693968 kB
Inactive(anon):   270024 kB
Active(file):    1587532 kB
Inactive(file):  3332328 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:       8388604 kB
SwapFree:        8388604 kB
Dirty:                 4 kB
Writeback:             0 kB
AnonPages:       3692156 kB
Mapped:           369268 kB
Shmem:            271836 kB
Slab:             276088 kB
SReclaimable:     190960 kB
SUnreclaim:        85128 kB
KernelStack:       18512 kB
PageTables:        84760 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:    14483048 kB
Committed_AS:   11778012 kB
VmallocTotal:   34359738367 kB
VmallocUsed:      368152 kB
VmallocChunk:   34358947836 kB
Percpu:             1536 kB
HardwareCorrupted:     0 kB
AnonHugePages:   2482176 kB
CmaTotal:              0 kB
CmaFree:               0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:      113516 kB
DirectMap2M:    12355584 kB

[5] lsblk

查看硬盤和分區分布

NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda           8:0    0 465.8G  0 disk 
├─sda1        8:1    0   500M  0 part /boot
└─sda2        8:2    0    58G  0 part 
  ├─cl-root 253:0    0    50G  0 lvm  /
  └─cl-swap 253:1    0     8G  0 lvm  [SWAP]
sr0          11:0    1  1024M  0 rom 

[6] fdisk -l

查看硬盤和分區的詳細信息

磁盤 /dev/sda:500.1 GB, 500107862016 字節,976773168 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 4096 字節
I/O 大小(最小/最佳):4096 字節 / 4096 字節
磁盤標簽類型:dos
磁盤標識符:0x00031a77

   設備 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048   122677247    60825600   8e  Linux LVM

磁盤 /dev/mapper/cl-root:53.7 GB, 53687091200 字節,104857600 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 4096 字節
I/O 大小(最小/最佳):4096 字節 / 4096 字節


磁盤 /dev/mapper/cl-swap:8589 MB, 8589934592 字節,16777216 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 4096 字節
I/O 大小(最小/最佳):4096 字節 / 4096 字節

[7] mount | column -t

查看掛載點(OS目錄)與分區的關聯關系的狀態

sysfs                on  /sys                             type  sysfs            (rw,nosuid,nodev,noexec,relatime,seclabel)
proc                 on  /proc                            type  proc             (rw,nosuid,nodev,noexec,relatime)
devtmpfs             on  /dev                             type  devtmpfs         (rw,nosuid,seclabel,size=6077156k,nr_inodes=1519289,mode=755)
securityfs           on  /sys/kernel/security             type  securityfs       (rw,nosuid,nodev,noexec,relatime)
tmpfs                on  /dev/shm                         type  tmpfs            (rw,nosuid,nodev,seclabel)
devpts               on  /dev/pts                         type  devpts           (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs                on  /run                             type  tmpfs            (rw,nosuid,nodev,seclabel,mode=755)
tmpfs                on  /sys/fs/cgroup                   type  tmpfs            (ro,nosuid,nodev,noexec,seclabel,mode=755)
cgroup               on  /sys/fs/cgroup/systemd           type  cgroup           (rw,nosuid,nodev,noexec,relatime,seclabel,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore               on  /sys/fs/pstore                   type  pstore           (rw,nosuid,nodev,noexec,relatime)
cgroup               on  /sys/fs/cgroup/memory            type  cgroup           (rw,nosuid,nodev,noexec,relatime,seclabel,memory)
cgroup               on  /sys/fs/cgroup/cpu,cpuacct       type  cgroup           (rw,nosuid,nodev,noexec,relatime,seclabel,cpuacct,cpu)
cgroup               on  /sys/fs/cgroup/net_cls,net_prio  type  cgroup           (rw,nosuid,nodev,noexec,relatime,seclabel,net_prio,net_cls)
cgroup               on  /sys/fs/cgroup/pids              type  cgroup           (rw,nosuid,nodev,noexec,relatime,seclabel,pids)
cgroup               on  /sys/fs/cgroup/blkio             type  cgroup           (rw,nosuid,nodev,noexec,relatime,seclabel,blkio)
cgroup               on  /sys/fs/cgroup/cpuset            type  cgroup           (rw,nosuid,nodev,noexec,relatime,seclabel,cpuset)
cgroup               on  /sys/fs/cgroup/perf_event        type  cgroup           (rw,nosuid,nodev,noexec,relatime,seclabel,perf_event)
cgroup               on  /sys/fs/cgroup/devices           type  cgroup           (rw,nosuid,nodev,noexec,relatime,seclabel,devices)
cgroup               on  /sys/fs/cgroup/freezer           type  cgroup           (rw,nosuid,nodev,noexec,relatime,seclabel,freezer)
cgroup               on  /sys/fs/cgroup/hugetlb           type  cgroup           (rw,nosuid,nodev,noexec,relatime,seclabel,hugetlb)
configfs             on  /sys/kernel/config               type  configfs         (rw,relatime)
/dev/mapper/cl-root  on  /                                type  xfs              (rw,relatime,seclabel,attr2,inode64,noquota)
selinuxfs            on  /sys/fs/selinux                  type  selinuxfs        (rw,relatime)
systemd-1            on  /proc/sys/fs/binfmt_misc         type  autofs           (rw,relatime,fd=24,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=11076)
debugfs              on  /sys/kernel/debug                type  debugfs          (rw,relatime)
mqueue               on  /dev/mqueue                      type  mqueue           (rw,relatime,seclabel)
hugetlbfs            on  /dev/hugepages                   type  hugetlbfs        (rw,relatime,seclabel)
tmpfs                on  /tmp                             type  tmpfs            (rw,seclabel)
/dev/sda1            on  /boot                            type  xfs              (rw,relatime,seclabel,attr2,inode64,noquota)
sunrpc               on  /var/lib/nfs/rpc_pipefs          type  rpc_pipefs       (rw,relatime)
tmpfs                on  /run/user/1000                   type  tmpfs            (rw,nosuid,nodev,relatime,seclabel,size=1218892k,mode=700,uid=1000,gid=1000)
fusectl              on  /sys/fs/fuse/connections         type  fusectl          (rw,relatime)
gvfsd-fuse           on  /run/user/1000/gvfs              type  fuse.gvfsd-fuse  (rw,nosuid,nodev,relatime,user_id=1000,group_id=1000)
tmpfs                on  /run/user/1001                   type  tmpfs            (rw,nosuid,nodev,relatime,seclabel,size=1218892k,mode=700,uid=1001,gid=1001)
gvfsd-fuse           on  /run/user/1001/gvfs              type  fuse.gvfsd-fuse  (rw,nosuid,nodev,relatime,user_id=1001,group_id=1001)
tmpfs                on  /run/user/0                      type  tmpfs            (rw,nosuid,nodev,relatime,seclabel,size=1218892k,mode=700)

[8] df -hT

查看硬盤使用情況

文件系統            類型      容量  已用  可用 已用% 掛載點
devtmpfs            devtmpfs  5.8G     0  5.8G    0% /dev
tmpfs               tmpfs     5.9G   31M  5.8G    1% /dev/shm
tmpfs               tmpfs     5.9G   19M  5.8G    1% /run
tmpfs               tmpfs     5.9G     0  5.9G    0% /sys/fs/cgroup
/dev/mapper/cl-root xfs        50G  9.9G   41G   20% /
tmpfs               tmpfs     5.9G  684K  5.9G    1% /tmp
/dev/sda1           xfs       494M  209M  286M   43% /boot
tmpfs               tmpfs     1.2G   32K  1.2G    1% /run/user/1000
tmpfs               tmpfs     1.2G   64K  1.2G    1% /run/user/1001
tmpfs               tmpfs     1.2G     0  1.2G    0% /run/user/0

[9] lspci

查看pci信息,即 主板所有硬件槽信息

00:00.0 Host bridge: Intel Corporation Xeon E3-1200 v2/3rd Gen Core processor DRAM Controller (rev 09)
00:02.0 VGA compatible controller: Intel Corporation Xeon E3-1200 v2/3rd Gen Core processor Graphics Controller (rev 09)
00:16.0 Communication controller: Intel Corporation 6 Series/C200 Series Chipset Family MEI Controller #1 (rev 04)
00:1a.0 USB controller: Intel Corporation 6 Series/C200 Series Chipset Family USB Enhanced Host Controller #2 (rev 05)
00:1b.0 Audio device: Intel Corporation 6 Series/C200 Series Chipset Family High Definition Audio Controller (rev 05)
00:1c.0 PCI bridge: Intel Corporation 6 Series/C200 Series Chipset Family PCI Express Root Port 1 (rev b5)
00:1c.2 PCI bridge: Intel Corporation 6 Series/C200 Series Chipset Family PCI Express Root Port 3 (rev b5)
00:1c.3 PCI bridge: Intel Corporation 6 Series/C200 Series Chipset Family PCI Express Root Port 4 (rev b5)
00:1c.5 PCI bridge: Intel Corporation 6 Series/C200 Series Chipset Family PCI Express Root Port 6 (rev b5)
00:1d.0 USB controller: Intel Corporation 6 Series/C200 Series Chipset Family USB Enhanced Host Controller #1 (rev 05)
00:1f.0 ISA bridge: Intel Corporation H61 Express Chipset LPC Controller (rev 05)
00:1f.2 SATA controller: Intel Corporation 6 Series/C200 Series Chipset Family 6 port Desktop SATA AHCI Controller (rev 05)
00:1f.3 SMBus: Intel Corporation 6 Series/C200 Series Chipset Family SMBus Controller (rev 05)
02:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 07)
03:00.0 Unassigned class [ff00]: Realtek Semiconductor Co., Ltd. RTS5229 PCI Express Card Reader (rev 01)
04:00.0 Network controller: Realtek Semiconductor Co., Ltd. RTL8188EE Wireless Network Adapter (rev 01)

[10] lspci | grep -i 'eth'

查看網卡硬件信息

02:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 07)

[11] ifconfig -a

查看系統的所有網絡接口 --方式1

enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.11.143  netmask 255.255.255.0  broadcast 10.0.11.255
        inet6 fe80::225:abff:fe7b:350f  prefixlen 64  scopeid 0x20<link>
        ether 00:25:ab:7b:35:0f  txqueuelen 1000  (Ethernet)
        RX packets 1956257  bytes 758582738 (723.4 MiB)
        RX errors 0  dropped 1  overruns 0  frame 0
        TX packets 577940  bytes 61943949 (59.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 378776  bytes 35602917 (33.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 378776  bytes 35602917 (33.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlp4s0: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 0e:f1:0d:bc:25:10  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[12] cat /sys/class/net/ens192/address

查看MAC地址

# ifconfig -a
ens192: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.100.102.149  netmask 255.255.255.0  broadcast 10.100.102.255
        inet6 fe80::250:56ff:fe85:8b95  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:85:8b:95  txqueuelen 1000  (Ethernet)
        RX packets 138836  bytes 9388486 (8.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4060  bytes 1595871 (1.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 487442  bytes 177800691 (169.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 487442  bytes 177800691 (169.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

# cat /sys/class/net/ens192/address
00:50:56:85:8b:95

[13] ip link show

查看系統的所有網絡接口 --方式2

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 00:25:ab:7b:35:0f brd ff:ff:ff:ff:ff:ff
3: wlp4s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether 0e:f1:0d:bc:25:10 brd ff:ff:ff:ff:ff:ff

[14] ip addr

查看系統的所有網絡接口 --方式3

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:25:ab:7b:35:0f brd ff:ff:ff:ff:ff:ff
    inet 10.0.11.143/24 brd 10.0.11.255 scope global noprefixroute enp2s0
       valid_lft forever preferred_lft forever
    inet6 fe80::225:abff:fe7b:350f/64 scope link 
       valid_lft forever preferred_lft forever
3: wlp4s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 0e:f1:0d:bc:25:10 brd ff:ff:ff:ff:ff:ff

[15] ethtool eth0

查看某個網絡接口的詳細信息,例如:eth0的詳細參數和指標
Link detected: yes # 表示有網線連接,和路由是通的
Duplex: Full # 全雙工

Settings for enp2s0:
	Supported ports: [ TP MII ]
	Supported link modes:   10baseT/Half 10baseT/Full 
	                        100baseT/Half 100baseT/Full 
	                        1000baseT/Half 1000baseT/Full 
	Supported pause frame use: No
	Supports auto-negotiation: Yes
	Supported FEC modes: Not reported
	Advertised link modes:  10baseT/Half 10baseT/Full 
	                        100baseT/Half 100baseT/Full 
	                        1000baseT/Half 1000baseT/Full 
	Advertised pause frame use: No
	Advertised auto-negotiation: Yes
	Advertised FEC modes: Not reported
	Link partner advertised link modes:  10baseT/Half 10baseT/Full 
	                                     100baseT/Half 100baseT/Full 
	Link partner advertised pause frame use: Symmetric Receive-only
	Link partner advertised auto-negotiation: Yes
	Link partner advertised FEC modes: Not reported
	Speed: 100Mb/s
	Duplex: Full
	Port: MII
	PHYAD: 0
	Transceiver: internal
	Auto-negotiation: on
	Supports Wake-on: pumbg
	Wake-on: d
	Current message level: 0x00000033 (51)
			       drv probe ifdown ifup
	Link detected: yes

[16:shell script]

case1: 檢測自動化腳本所兼容的操作系統類型

work_dir=$(cd $(dirname $0);pwd)

menu(){
cat <<END
=================================================
        1.Install at Debian(Ubuntu) 10(buster)
        2.Install at Redhat(CentOS) 7
        3.Exit
=================================================
END
}

checkOs(){
	echo "Welcome to install XXX-Software ..."
	echo "This script support CentOS/RedHat/Debian/Ubuntu"
	echo "Please execute script at path $work_dir "

	#To be compatible with MacOS and Linux
	if [[ "$OSTYPE" == "darwin"* ]]; then
    		# Mac OSX
    		echo " install not support Mac OSX operating system"
    		exit 1
	elif [[ "$OSTYPE" == "linux-gnu" ]]; then
   		# linux
		echo "linux"
	elif [[ "$OSTYPE" == "cygwin" ]]; then
    		# POSIX compatibility layer and Linux environment emulation for Windows
    		echo "this install scripts not support Windows operating system"
    		exit 1
	elif [[ "$OSTYPE" == "msys" ]]; then
    		# Lightweight shell and GNU utilities compiled for Windows (part of MinGW)
    		echo "this install scripts not support Windows operating system"
    		exit 1
	elif [[ "$OSTYPE" == "win32" ]]; then
    		echo "this install scripts not support Windows operating system"
    		exit 1
	elif [[ "$OSTYPE" == "freebsd"* ]]; then
    		# ...
    		echo "freebsd"
	else
    		# Unknown.
    		echo "Operating system unknown, please tell us(submit issue) for better service"
    		exit 1
	fi

	# 提示信息
	menu

	read -p "Please input number(1|2|3):" num

   	case $num in
        	1)
                  [[ ! -f /etc/debian_version ]] && echo 'Operating system is not Debian or Ubuntu' && exit 1
                  [[ $(grep 'ID=debian' /etc/os-release 2>/dev/null) == 'ID=debian' ]] && [[ $(grep VERSION_ID /etc/os-release 2>/dev/null) != 'VERSION_ID="10"' ]] &&  echo 'System version not support. Debian 10' && exit 1
                  [[ $(grep 'ID=ubuntu' /etc/os-release 2>/dev/null) == 'ID=ubuntu' ]] && [[ ! $(grep VERSION_ID /etc/os-release 2>/dev/null) =~ 'VERSION_ID="18' ]] &&  echo 'System version not support. Ubuntu 18' && exit 1
                  echo "You chose Debian(Ubuntu) 10(buster)"
                	;;
       		2)
       		        [[ ! -f /etc/redhat-release ]] && echo 'Operating system is not Redhat or Centos' && exit 1
                  [[ $(grep VERSION_ID /etc/os-release 2>/dev/null) != 'VERSION_ID="7"' ]] && [[ -f /etc/redhat-release ]] && echo 'System version not support. Redhat 7' && exit 1
                  echo "You chose Redhat(Centos) 7"
                	;;
        	*)
                	echo $"Usage :bash $0"
                	exit 1
	esac
}

case2: 一鍵獲取服務器主要參數

(僅在CentOS上實測)

#【CPU】
CPU_ARCH_VAR=$(lscpu | grep -i Architecture | awk -F ' ' '{printf $2 "\n"}')
CPU_MODEL_VAR=$(lscpu | grep -i "Model name" | awk -F ':' '{printf $2 "\n"}' | sed 's/^[ \t]*//g')
CPU_IS_VIRTUAL_VAR=$(lscpu | grep -i "Hypervisor vendor" | awk -F ':' '{printf $2 "\n"}' | sed 's/^[ \t]*//g')
# echo "CPU { arch="$CPU_ARCH_VAR" / model="$CPU_MODEL_VAR" / isVirtualMechine="$CPU_IS_VIRTUAL_VAR" }"

#【內存】
MEM_TOTAL_VAR=$(free -h | grep -i "Mem" | awk -F ' ' '{printf $2 "\n"}')
MEM_USED_VAR=$(free -h | grep -i "Mem" | awk -F ' ' '{printf $3 "\n"}')
MEM_AVAIL_VAR=$(free -h | grep -i "Mem" | awk -F ' ' '{printf $4 "\n"}')
# echo "MEM { total="$MEM_TOTAL_VAR" / used="$MEM_USED_VAR" / avail="$MEM_AVAIL_VAR" }"

#【硬盤】
DISK_TOTAL_VAR=$(df -hl | grep /dev/mapper/vg | awk -F ' ' '{printf $2 "\n"}')
DISK_USED_VAR=$(df -hl | grep /dev/mapper/vg | awk -F ' ' '{printf $3 "\n"}')
DISK_USED_PERCENT_VAR=$(df -hl | grep /dev/mapper/vg | awk -F ' ' '{printf $5 "\n"}')
DISK_AVAIL_VAR=$(df -hl | grep /dev/mapper/vg | awk -F ' ' '{printf $4 "\n"}')
# echo "DISK { total="$DISK_TOTAL_VAR" / used="$DISK_USED_VAR"("$DISK_USED_PERCENT_VAR") / avail="$DISK_AVAIL_VAR" } ";

#【操作系統】
OS_KERNEL_VERSION_VAR=$(cat /proc/version)
OS_CENTOS_VAR=$(cat /etc/redhat-release)
# echo "OS { Linux-Kernel="$OS_KERNEL_VERSION_VAR" / CentOS-Version="$OS_CENTOS_VAR" }";

#【MAC】
MACs_VAR=$(ifconfig | grep -i ether | awk -F ' ' '{printf $2 "\n"}')
# echo "MACs { macs="$MACs_VAR"} ";

#【節點名(域名)/IP】
DNS_NODENAME_VAR=$(uname -n)
# 或 DNS_NODENAME_VAR=$(hostname)
DNS_IP_VAR="$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')"
DNS_IP_VAR="$(ifconfig |grep inet|awk -F "[: ]+" '{print $3}'|head -n1)"
# echo "DNS { ip="${DNS_IP_VAR}" / nodename="$DNS_NODENAME_VAR" } ";
DATE_STR=`date "+%Y-%m-%d %H:%M:%S"`;

CPU="CPU { arch="$CPU_ARCH_VAR" / model="$CPU_MODEL_VAR" / isVirtualMechine="$CPU_IS_VIRTUAL_VAR" }"
MEM="MEM { total="$MEM_TOTAL_VAR" / used="$MEM_USED_VAR" / avail="$MEM_AVAIL_VAR" }"
DISK="DISK { total="$DISK_TOTAL_VAR" / used="$DISK_USED_VAR"("$DISK_USED_PERCENT_VAR") / avail="$DISK_AVAIL_VAR" } ";
OS="OS { Linux-Kernel="$OS_KERNEL_VERSION_VAR" / CentOS-Version="$OS_CENTOS_VAR" }";
MACs="MACs { macs="$MACs_VAR"} ";
DNS="DNS { ip="${DNS_IP_VAR}" / nodename="$DNS_NODENAME_VAR" } ";

echo -e "SERVER BASE INFO
$CPU
$MEM
$DISK
$MACs
$DNS
(updated on $DATE_STR)"

Y 補充命令: lshw 【待續】

lshw(Hardware Lister)是另外一個可以查看硬件信息的工具,不僅如此,它還可以用來做一些硬件的benchmark。
這個工具其實就是用/proc里面讀取一些文件來顯示相關的信息,它用到了如下文件和目錄(下的文件):

/proc/cpuinfo 顯示CPU信息
/proc/bus/pci 顯示pci信息
/proc/scsi 顯示scsi信息
/proc/net/dev 顯示網絡設備信息
/proc/kcore 從內存映像讀取相關信息
/proc/ide 顯示IDE設備信息
/proc/devices
/proc/mounts
/proc/fstab
  • 安裝
yum install -y lshw
  • 查看版本
[root@sdc234 ~]# lshw -version
B.02.18
  • 以html/xml/json格式輸出
    -html :將設備信息以 html的格式輸出
    -xml :將設備信息以 xml的格式輸出
    -json :將設備信息以 json的格式輸出
lshw -html > info.html

lshw -xml > info.xml

lshw -json > info.xml

X 參考文獻


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM