[TOC]

0x00 硬件查看

hwinfo 命令

描述:hwinfo 意即硬件信息工具,是另外一种很好的实用工具。它被用来检测系统中已存在的硬件,并且以可读的格式显示各种硬件组件的细节信息。
系统发行版安装 hwinfo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#示例1.Arch Linux
$ sudo pacman -S hwinfo

#示例2.Fedora
$ sudo dnf config-manager --add-repo http://download.opensuse.org/repositories/home:/zhonghuaren/Fedora_26/home:zhonghuaren.repo
$ sudo dnf makecache
$ sudo dnf install hwinfo

#示例3.centos
$ sudo rpm -Uvh http://mirror.ghettoforge.org/distributions/gf/gf-release-latest.gf.el6.noarch.rpm
$ sudo rpm -Uvh http://mirror.ghettoforge.org/distributions/gf/gf-release-latest.gf.el7.noarch.rpm
$ sudo yum makecache
$ sudo yum install hwinfo

#示例4.Debian/Ubuntu
$ sudo apt-get install hwinfo

#示例5.openSUSE
$ sudo zypper install hwinfo

hwinfo支持的检测的硬件设备:

1
2
3
4
5
6
7
8
9
CPU : manufacturer, model, frequency
Motherboard : manufacturer, model, BIOS version, on-board controller
RAM : RAM size, RAM type (FP, EDO, SDRAM, DDRAM)
Hard disk controller : SCSI, IDE, on-board/card, model, BIOS version, chipset
Hard disks : type, manufacturer, model, size, bus system (SCSI, IDE), partitioning
CD-ROM drives : type, manufacturer, model, speed, internal/external, parallel port?
Other media : CD writer, streamer (model, manufacturer, SCSI/EIDE/floppy streamer, capacity), ZIP drives, Jaz drives, MO drives,flopticals
Graphics card : model, manufacturer, chipset, memory (DRAM, VRAM), memory size
Other peripherals : printer, parallel ports, serial ports, modem, ISDN card

语法参数:

1
2
3
4
5
6
7
8
9
10
11
12
hwinfo [OPTIONS] [HARDWARE_ITEM]
#指定项查看
--<HARDWARE_ITEM>
all, arch, bios, block, bluetooth, braille, bridge, camera,
cdrom, chipcard, cpu, disk, dsl, dvb, fingerprint, floppy,
framebuffer, gfxcard, hub, ide, isapnp, isdn, joystick, keyboard,
memory, mmc-ctrl, modem, monitor, mouse, netcard, network, partition,
pci, pcmcia, pcmcia-ctrl, pppoe, printer, redasd,
reallyall, scanner, scsi, smp, sound, storage-ctrl, sys, tape,
tv, uml, usb, usb-ctrl, vbe, wlan, xen, zip

-- short #显示摘要信息

基础示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#示例1.获取网络信息
hwinfo --network
45: None c0.0: 10701 Ethernet
[Created at net.126]
Unique ID: c0d5.ndpeucax6V1
Parent ID: wMMD.PlGB53gtlv4
SysFS ID: /class/net/ens192
SysFS Device Link: /devices/pci0000:00/0000:00:16.0/0000:0b:00.0
Hardware Class: network interface
Model: "Ethernet network interface"
Driver: "vmxnet3"
Driver Modules: "vmxnet3"
Device File: ens192
HW Address: 00:50:56:b3:dc:b4
Permanent HW Address: 00:50:56:b3:dc:b4
Link detected: yes
Config Status: cfg=new, avail=yes, need=no, active=unknown
Attached to: #44 (Ethernet controller)


#显示摘要信息
hwinfo --short
cpu: Intel(R) Xeon(R) CPU E3-1271 v3 @ 3.60GHz, 3591 MHz
keyboard: /dev/input/event1 AT Translated Set 2 keyboard
mouse:
/dev/input/mice VirtualPS/2 VMware VMMouse
/dev/input/mice VirtualPS/2 VMware VMMouse
graphics card: VMware VMWARE0405


lshw 命令 - 硬件列表

描述:lshw(list hardware)列出硬件设备提取对机器的硬件配置的详细信息,相当于 Windows 下的设备管理器列出系统里的硬件设备。
它可以在DMI-能够x86或IA-64系统和一些PowerPC上(的PowerMac G4是已知的工作)报告精确的存储器配置,固件版本,主板配置,CPU版本和速度,高速缓存配置,总线速度等。
它目前支持DMI(x86和IA-64只),的OpenFirmware设备树(仅用于PowerPC),PCI / AGP,CPUID(86),IDE / ATA / ATAPI,PCMCIA(仅在x86测试),SCSI和USB

软件安装:

1
2
3
4
5
#RHEL/CentOS
yum install -y lshw

#Debian/Ubuntu
apt install lshw

基础语法:

1
2


基础示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 示例1
$lshw | more
vm-1575613390.novalocal
description: Computer
product: OpenStack Compute
vendor: RDO
version: 17.0.3-1.el7
serial: 9315fa46-d2a5-4f79-b22d-3e1cc9ae719b
width: 64 bits
capabilities: smbios-2.8 dmi-2.8 smp vsyscall32
configuration: boot=normal family=Virtual Machine uuid=63032450-4D69-A249-A359-FA387FD1E17D

# 示例2.列举出当前系统可显示的设备信息
$lshw | grep "*"
*-core
*-firmware
*-cpu:0
*-cpu:1
*-cpu:2
*-cpu:3
*-memory
*-bank
*-pci
*-isa
*-ide
*-cdrom
*-medium
*-usb
*-usbhost
*-usb
*-bridge
*-display
*-network
*-virtio0
*-communication
*-virtio1 UNCLAIMED
*-scsi
*-virtio2
*-volume
*-generic
*-virtio3 UNCLAIMED
*-pnp00:00
*-pnp00:01
*-pnp00:02
*-pnp00:03
*-pnp00:04

# 实例2.EXAMPLES
lshw -short #Lists hardware in a compact format.
lshw -class disk -class storage -short
# H/W path Device Class Description
# ===========================================================
# /0/100/1/0 storage MegaRAID SAS-3 3108 [Invader]
# /0/100/2/0 storage SAS3008 PCI-Express Fusion-MPT SAS-3
# /0/100/11.4 storage C610/X99 series chipset sSATA Controller [AHCI mode]
# /0/100/1f.2 scsi11 storage C610/X99 series chipset 6-Port SATA Controller [AHCI mode]
# /0/100/1f.2/0.0.0 /dev/cdrom disk DVD+-RW DU-8A5LH
# WARNING: output may be incomplete or inaccurate, you should run this program as super-user.

lshw -class disk -class storage # Lists all disks and storage controllers in the system.
# WARNING: you should run this program as super-user.
# *-raid
# description: RAID bus controller
# product: MegaRAID SAS-3 3108 [Invader]
# vendor: Broadcom / LSI
# physical id: 0
# bus info: [email protected]:01:00.0
# version: 02
# width: 64 bits
# clock: 33MHz
# capabilities: raid bus_master cap_list rom
# configuration: driver=megaraid_sas latency=0
# resources: irq:34 ioport:3000(size=256) memory:91f00000-91f0ffff memory:91e00000-91efffff memory:91f20000-91f3ffff
# *-sas
# description: Serial Attached SCSI controller
# product: SAS3008 PCI-Express Fusion-MPT SAS-3
# vendor: Broadcom / LSI
# physical id: 0
# bus info: [email protected]:04:00.0
# version: 02
# width: 64 bits
# clock: 33MHz
# capabilities: sas bus_master cap_list
# configuration: driver=mpt3sas latency=0
# resources: irq:78 ioport:2000(size=256) memory:93600000-9360ffff memory:92500000-925fffff memory:93610000-9370ffff memory:92600000-935fffff

lshw -html -class network # Lists all network interfaces in HTML.

lshw -disable dmi # Don't use DMI to detect hardware.


losetup 命令

描述:该命令用于设置循环设备。

Q: 什么是循环设备?

答: 循环设备可把文件虚拟成区块设备,籍以模拟整个文件系统,让用户得以将其视为硬盘驱动器,光驱或软驱等设备,并挂入当作目录来使用。

基于语法:

1
2
3
4
5
losetup [-d][-e <加密方式>][-o <平移数目>][循环设备代号][文件]
#参数:
-d 卸除设备。
-e<加密方式> 启动加密编码。
-o<平移数目> 设置数据平移的数目。

基础示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 1.列出当前系统中所有使用的设备,其中用来虚拟出块设备(文件系统)
$ losetup -a
/dev/loop0: [0038]:23855612 (/disk/docker/200/devicemapper/devicemapper/data)
/dev/loop1: [0038]:23855719 (/disk/docker/200/devicemapper/devicemapper/metadata)

# 2.简单示例
# (1) 首先创建一个 1G 大小的空文件:
dd if=/dev/zero of=loopfile.img bs=1G count=1
# 1+0 records in
# 1+0 records out
# 1073741824 bytes (1.1 GB) copied, 69.3471 s, 15.5 MB/s
# (2) 对该文件格式化为 ext4 格式:
mkfs.ext4 loopfile.img
# (3) 用 file 命令查看下格式化后的文件类型:
file loopfile.img
# loopfile.img: Linux rev 1.0 ext4 filesystem data, UUID=a9dfb4a0-6653-4407-ae05-7044d92c1159 (extents) (large files) (huge files
# (4) 挂载块设备然后通过/tmp目录像访问真实快设备一样来访问磁盘镜像文件floppy.img。
$ losetup /dev/loop0 loopfile.img
$ mount /dev/loop0 /mnt/loopback
#实际等价于下面两条命令
# $ mkdir /mnt/loopback
# $ mount -o loop loopfile.img /mnt/loopback
# (5) 卸载loop设备
$ umount /tmp
$ losetup -d /dev/loop1

注意事项:对于第一种方法(mount -o loop)并不能适用于所有的场景。
比如我们想创建一个硬盘文件然后对该文件进行分区,接着挂载其中一个子分区,这时就不能用 -o loop 这种方法了。

1
2
losetup /dev/loop1 loopfile.img
fdisk /dev/loop1


dmsetup 命令

描述:该命令是一个用来与 Device Mapper 沟通的命令行封装器(wrapper)。可使用 dmsetup 命令的 info、ls、status 和 deps 选项查看 LVM 设备的常规信息;

基础语法:

示例1.Device Mapper 设备概述查看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$ dmsetup info
# Name 设备名称LVM 设备以用小横线分隔的卷组名称和逻辑卷名称表示即源名称-设备号:次设备号-inode数据量;
# 在标准 LVM 操作过程中,不应使用这种格式的 LVM 设备名称直接指定 LVM 设备,而是应该使用 vg/lv 指定。
Name: docker-0:38-23863364-574e9e5fad5388c883ad2c6ad19c6bd0b1f3022fa03e8ab52bec6e8e1ed190b3
# State 可能的设备状态是 SUSPENDED(dmsetup suspend)当挂起某个设备时会停止对该设备的所有 I/O 操作、ACTIVE(dmsetup resume) 和 READ-ONLY
State: ACTIVE
# Read Ahead 系统对正在进行读取操作的任意打开文件的预读数据块数目
Read Ahead: 8192
# Tables present 该这个类型的可能状态为 LIVE 和 INACTIVE。INACTIVE 状态表示已经载入了表格,且会在 dmsetup resume 命令将某个设备状态恢复为 ACTIVE 时进行切换,届时表格状态将为 LIVE
Tables present: LIVE
# Open count 打开参考计数表示打开该设备的次数
Open count: 1
# 目前收到的事件数目
Event number: 0
# Major, minor 主设备号码和副设备号码
Major, minor: 253, 8
# Number of targets 组成某个设备的片段数目。
# 例如:一个跨三个磁盘的线性设备会有三个目标。线性设备由某个磁盘起始和结尾,而不是中间组成的线性设备有两个目标。
Number of targets: 1
------------------------------------------------------------------------
$dmsetup info centos-root #查看dm-name设备的基本信息
Name: centos-root
State: ACTIVE
Read Ahead: 8192
Tables present: LIVE
Open count: 1
Event number: 0
Major, minor: 253, 0
Number of targets: 1
UUID: LVM-NzOjfUT1AfxkbvBPhhTf9aBT1r9Rtsou7mtVdynEO0VfrLOYiffjd7KL9benyU4o #设备的UUID

示例2.命令常用参数演示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# (0) 查看系统支持的target_type及版本信息  
$dmsetup targets
# mirror v1.13.2
# striped v1.6.0
# linear v1.3.0
# error v1.5.0


# (1) 列出映射的设备的设备名称列表
$dmsetup ls
# centos-app (253:2)
# centos-swap (253:1)
# centos-root (253:0)
# 列出至少有一个指定类型目标的设备,比如列出目前配置的镜像映射设备名称的命令。
$dmsetup ls --target target_type
$dmsetup ls --target mirror
# lock_stress-grant--02.1722 (253, 34)
# lock_stress-grant--01.1720 (253, 18)
# lock_stress-grant--03.1718 (253, 52)
# 以树形式显示设备间的相依性
$dmsetup ls --tree
# centos-app (253:2)
# └─ (8:2)
# centos-swap (253:1)
# └─ (8:2)
# centos-root (253:0)
# └─ (8:2)


# (2) 查看设备的依赖关系
$dmsetup deps #为指定设备的映射列表参考的设备提供(major,minor)对列表
centos-app: 1 dependencies : (8, 2)
centos-swap: 1 dependencies : (8, 2)
centos-root: 1 dependencies : (8, 2)
# 只列出设备 centos-root相依性的命令
dmsetup deps centos-root
3 dependencies : (253, 33) (253, 32) (253, 31)


# (3) 用来列出在所有目前配置映射设备中目标状态的命令
$dmsetup status
# centos-app: 0 20971520 linear
# centos-swap: 0 8126464 linear
# centos-root: 0 73646080 linear
$dmsetup status --target linear
centos-app: 0 20971520 linear
centos-swap: 0 8126464 linear
centos-root: 0 73646080 linear

# (4)设备创建
dmsetup create dm-name dm-table    #根据dm-table建立一个dm-name的设备
dmsetup create dm-name --table "dm-table-txt" #根据"dm-table-txt" 建立一个dm-name的设备

# (5)重载dm-table
dmsetup load/reload dm-name dm-table   

# (6)暂停dm-name设备且设备状态是 SUSPENDED
dmsetup suspend dm-name   

# (7)重新使用dm-name设备  
dmsetup resume dm-name  

# (8)删除dm-name   
dmsetup remove dm-name dm-name


dmidecode 命令 - 可读格式转储计算机DMI表内容的工具

描述: dmidecode 是一种以可读格式转储计算机DMI(有人说是SMBIOS)表内容的工具。此表包含系统硬件组成的说明以及其他有用的信息,如序列号和BIOS版本。由于有了这个表您可以检索这些信息,而无需探测实际的硬件。虽然这在报告速度和安全性方面是一个很好的观点,但这也使得提供的信息可能不可靠。

  • DMI 表不仅描述了系统当前的组成,它还可以报告可能的演变(例如支持的最快CPU或最大内存量)支持内存)。
  • SMBIOS 代表系统管理BIOS而DMI代表桌面管理接口,这两个标准紧密相关由DMTF(桌面管理任务)开发力)。


基础语法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Usage: 
dmidecode [OPTIONS]

# 选项
-d, --dev-mem FILE
Read memory from device FILE (default: /dev/mem)

-q, --quiet
Be less verbose. Unknown, inactive and OEM-specific entries are not displayed. Meta-data and handle references are hidden.

-s, --string KEYWORD
Only display the value of the DMI string identified by KEYWORD. KEYWORD must be a keyword from the following list: bios-vendor, bios-version, bios-release-date, system-
manufacturer, system-product-name, system-version, system-serial-number, system-uuid, system-family, baseboard-manufacturer, baseboard-product-name, baseboard-version,
baseboard-serial-number, baseboard-asset-tag, chassis-manufacturer, chassis-type, chassis-version, chassis-serial-number, chassis-asset-tag, processor-family, processor-
manufacturer, processor-version, processor-frequency. Each keyword corresponds to a given DMI type and a given offset within this entry type. Not all strings may be mean‐
ingful or even defined on all systems. Some keywords may return more than one result on some systems (e.g. processor-version on a multi-processor system). If KEYWORD is
not provided or not valid, a list of all valid keywords is printed and dmidecode exits with an error. This option cannot be used more than once.

Note: on Linux, most of these strings can alternatively be read directly from sysfs, typically from files under /sys/devices/virtual/dmi/id. Most of these files are even
readable by regular users.

-t, --type TYPE
Only display the entries of type TYPE. TYPE can be either a DMI type number, or a comma-separated list of type numbers, or a keyword from the following list: bios, system,
baseboard, chassis, processor, memory, cache, connector, slot. Refer to the DMI TYPES section below for details. If this option is used more than once, the set of dis‐
played entries will be the union of all the given types. If TYPE is not provided or not valid, a list of all valid keywords is printed and dmidecode exits with an error.

-H, --handle HANDLE
Only display the entry whose handle matches HANDLE. HANDLE is a 16-bit integer.

-u, --dump
Do not decode the entries, dump their contents as hexadecimal instead. Note that this is still a text output, no binary data will be thrown upon you. The strings attached
to each entry are displayed as both hexadecimal and ASCII. This option is mainly useful for debugging.

--dump-bin FILE
Do not decode the entries, instead dump the DMI data to a file in binary form. The generated file is suitable to pass to --from-dump later.

--from-dump FILE
Read the DMI data from a binary file previously generated using --dump-bin.

--no-sysfs
Do not attempt to read DMI data from sysfs files. This is mainly useful for debugging.

--oem-string N
Only display the value of the OEM string number N. The first OEM string has number 1. With special value "count", return the number of OEM strings instead.


# DMI TYPES : SMBIOS规范定义了以下DMI类型
Type Information
────────────────────────────────────────────
0 BIOS
1 System
2 Baseboard
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
40 Additional Information
41 Onboard Devices Extended Information
42 Management Controller Host Interface

# FILES
* /dev/mem
* /sys/firmware/dmi/tables/smbios_entry_point (Linux only)
* /sys/firmware/dmi/tables/DMI (Linux only)

# 关键字可以用--type代替数字类型,每个关键字都相当于一个类型编号列表:
Keyword Types
──────────────────────────────
bios 0, 13
system 1, 12, 15, 23, 32
baseboard 2, 10, 41
chassis 3
processor 4
memory 5, 6, 16, 17
cache 7
connector 8
slot 9


Tips: 记录相关信息可包括handle/type/size/Decoded values.
• A handle. 这是一个唯一的标识符,允许记录相互引用。例如,处理器记录通常使用其句柄引用缓存记录。
• A type. SMBIOS规范定义了计算机可以使用的不同类型的元素。在本例中,类型是2,这意味着记录包含“基板”
信息”。
• A size. 每个记录都有一个4字节的头(2代表句柄,1代表类型,1代表大小),其余的由记录数据使用。此值不将文本字符串带入计数(这些字符串放在记录的末尾),因此记录的实际长度可能(通常)大于显示的值。
• Decoded values. 提供的信息当然取决于记录的类型。在这里,我们将了解电路板的制造商、型号、版本和序列号。


基础示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# (1) 判断机器是否为虚拟机
dmidecode -s system-product-name
# VMware7,1
dmidecode -s system-serial-number
# VMware-56 4d 8c f9 b2 9f 92 bc-98 be 26 48 c8 3e b7 b5

# (2) 查看BIOS相关信息
$ dmidecode -t BIOS
Handle 0x0000, DMI type 0, 26 bytes
BIOS Information
Vendor: VMware, Inc.
Version: VMW71.00V.17369862.B64.2012240522
Release Date: 12/24/2020
ROM Size: 2048 kB
Characteristics:
ISA is supported
PCI is supported
PNP is supported
BIOS is upgradeable
Targeted content distribution is supported
UEFI is supported

# (3) 查看SLOT卡槽相关信息
$ dmidecode -t "slot"
Handle 0x0088, DMI type 9, 17 bytes
System Slot Information
Designation: PCI Slot J11
Type: 32-bit PCI
Current Usage: In Use
Length: Long
ID: 1
Characteristics:
5.0 V is provided
3.3 V is provided
Bus Address: 0000:00:0f.0

Handle 0x0089, DMI type 9, 17 bytes
System Slot Information
Designation: PCI Slot J12
Type: 32-bit PCI
Current Usage: In Use
Length: Long
ID: 2
Characteristics:
5.0 V is provided
3.3 V is provided
Bus Address: 0000:00:10.0

# (4) 查看Baseboard相关信息
$ dmidecode -t "baseboard"
Handle 0x0002, DMI type 2, 15 bytes
Base Board Information
Manufacturer: Intel Corporation
Product Name: 440BX Desktop Reference Platform
Version: None
Serial Number: None
Asset Tag: Not Specified
Features: None
Location In Chassis: Not Specified
Chassis Handle: 0x0000
Type: Other
Contained Object Handles: 0

Handle 0x008E, DMI type 10, 8 bytes
On Board Device 1 Information
Type: Video
Status: Disabled
Description: VMware SVGA II
On Board Device 2 Information
Type: Sound
Status: Disabled
Description: ES1371


# (5) 以Handle查看相关信息
$ dmidecode -H 0x008E
Handle 0x008E, DMI type 10, 8 bytes
On Board Device 1 Information
Type: Video
Status: Disabled
Description: VMware SVGA II
On Board Device 2 Information
Type: Sound
Status: Disabled
Description: ES1371


# (6) 关键字不区分大小写匹配以下命令行是等效的:
• dmidecode --type 0 --type 13
• dmidecode --type 0,13
• dmidecode --type bios
• dmidecode --type BIOS

Tips : 注意如果dmidecode在具有新的SMBIOS规范的BIOS系统上运行,该工具还不支持该规范,它将在中打印相关消息


0x01 消息队列

它们是shell中管理IPC对象的命令是ipcs、ipcmk和ipcrm

ipcmk 命令

描述:创建一个消息队列或者共享内存段;

基础语法:

1
2
3
4
5
6
7
8
用法:
ipcmk [选项]

选项:
-M, --shmem <size> 创建大小为 <size> 的共享内存段。
-S, --semaphore <nsems> 创建包含 <nsems> 个元素的信号量数组
-Q, --queue 创建消息队列
-p, --mode <模式> 资源的权限模式掩码(默认为 0644)

基础示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 1. 创建一个消息队列
ipcmk -Q -p 0644
#消息队列 id:65538
ipcs | grep "65538"
# --------- 消息队列 -----------
# 键 msqid 拥有者 权限 已用字节数 消息
# 0x651dbb47 0 root 644 0 0
# 0x6a915d28 32769 root 644 0 0
# 0xb166ebf2 65538 root 644 0 0

# 2.创建一个消息队列共享内存段
ipcmk -M 512
# 共享内存 id:0

# 3.创建包含10个元素的信号量数组
ipcmk -S 10
#信号量 id:98304


ipcs 命令

描述:查看当前消息队列以及共享内存段和信息量数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
用法:
ipcs [resource ...] [output-format]
ipcs [resource] -i <id>

选项:
-i, --id <id> 打印由 id 标识的资源的详细信息

资源选项:
-m, --shmems 共享内存段
-q, --queues 消息队列
-s, --semaphores 信号量
-a, --all 全部(默认)

输出格式:
-t, --time 显示附加、脱离和更改时间
-p, --pid 显示 PID 的创建者和最后操作
-c, --creator 显示创建者和拥有者
-l, --limits 显示资源限制
-u, --summary 显示状态摘要
--human 以易读格式显示大小
-b, --bytes 以字节数显示大小

基础示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 0.默认显示所有
--------- 消息队列 -----------
键 msqid 拥有者 权限 已用字节数 消息
0xfdfc00fe 98304 root 644 0 0
------------ 共享内存段 --------------
键 shmid 拥有者 权限 字节 nattch 状态
--------- 信号量数组 -----------
键 semid 拥有者 权限 nsems

# 1.查看消息队列共享内存段和信息量数组
$ipcs -p -c
---------- 消息队列 创建者/拥有者 ------------
msqid 权限 cuid cgid uid gid
65538 644 root root root root
------------- 共享内存段创建者/拥有者 ---------------
shmid 权限 cuid cgid uid gid
0 644 root root root root
----------- 信号量数组创建者/拥有者 ------------
semid 权限 cuid cgid uid gid
98304 644 root root root root

# 2.查看消息队列带上时间
$ipcs -t
----------- 消息队列 发送/接收/更改 时间 ------------
65538 root 未设置 未设置 Jul 6 11:52:36
------------ 共享内存 添加/脱离/更改 时间 --------------
shmid 拥有者 已连上 已断开 已更改
0 root 未设置 未设置 Jul 6 14:06:26
------------ 信号量 操作/更改 时间 -------------
semid 拥有者 上一操作 上次更改
98304 root 未设置 Mon Jul 6 14:07:17 2020

# 3.显示消息状态,共享内存状态,信号量
$ipcs -l
# ---------- 消息限制 -----------
# 系统最大队列数量 = 3770
# 最大消息尺寸 (字节) = 8192
# 默认的队列最大尺寸 (字节) = 16384
# ---------- 同享内存限制 ------------
# 最大段数 = 4096
# 最大段大小 (千字节) = 18014398509465599
# 最大总共享内存 (千字节) = 18014398442373116
# 最小段大小 (字节) = 1
# --------- 信号量限制 -----------
# 最大数组数量 = 128
# 每个数组的最大信号量数目 = 250
# 系统最大信号量数 = 32000
# 每次信号量调用最大操作数 = 32
# 信号量最大值 = 32767
$ipcs -l -u # 易读格式
# ---------- 消息状态 -----------
# 已分配队列数 = 3
# 已用消息头(header)数 = 0
# 已用空间 = 0 字节
# ---------- 共享内存状态 ------------
# 段已分配 1
# 页已分配 1
# 页驻留 0
# 页交换 0
# 交换性能:0 次尝试 0 次成功
# --------- 信号量状态 -----------
# 已使用数组 = 1
# 已分配信号量数 = 10


ipcrm 命令

描述:删除一个或更多的消息队列、信号量集或者共享内存标识。
基础用法:

1
2
3
4
5
6
7
8
9
10
11
12
ipcrm [options]
ipcrm <shm|msg|sem> <id> [...]

选项:
-m, --shmem-id <id> 按 id 号移除共享内存段
-M, --shmem-key <键> 按键值移除共享内存段
-q, --queue-id <id> 按 id 号移除消息队列
-Q, --queue-key <键> 按键值移除消息队列
-s, --semaphore-id <id> 按 id 号移除信号量
-S, --semaphore-key <键> 按键值移除信号量
-a, --all[=<shm|msg|sem>] 全部移除
-v, --verbose 解释正在进行的操作

基础示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ ipcrm -m 262144       #通过id删除共享内存

$ ipcrm -M 0x55fc00fe #通过key删除共享内存

$ ipcrm -q 252432 #通过id删除消息队列

$ ipcrm -Q 0xfdfc00fe #通过key删除消息队列

$ ipcrm -s 242438 #通过id删除信号量

$ ipcrm -S 0x65 #通过key删除信号量

$ ipcrm -a #删除所有共享内存、信号量和消息队列
$ ipcrm -v -a #删除所有共享内存、信号量和消息队列,并且显示过程
# --------- 消息队列 -----------
# 键 msqid 拥有者 权限 已用字节数 消息
# ------------ 共享内存段 --------------
# 键 shmid 拥有者 权限 字节 nattch 状态
# --------- 信号量数组 -----------
# 键 semid 拥有者 权限 nsems