0 引言
在进行系统安全检测时,我们需要收集块设备的信息,这些可以通过blkid命令来获取。
1 blkid命令的安装
blkid命令是基于libblkid库的命令行工具,可以在大多数Linux发行版中使用。
如果你的Linux系统中没有安装blkid命令,那么你可以使用以下命令来安装:
1.1 对于基于RPM的发行版(如Red Hat Enterprise Linux、CentOS)
可以使用yum或dnf命令来安装util-linux包,它包含了blkid命令:
sudo yum install util-linux
或者
sudo dnf install util-linux
1.2 对于基于Debian的发行版(如Ubuntu、Debian)
可以使用apt或apt-get命令来安装util-linux包,它也包含了blkid命令:
udo apt install util-linux
或者
sudo apt-get install util-linux
2 blkid命令的帮助信息、功能、命令格式、选项和参数说明
2.1 blkid命令 的帮助信息
我们可以使用 blkid -help 命令来查看该命令的帮助信息。
2.1.1 bash中的blkid命令
[purpleendurer @ bash ~] blkid -help
blkid from util-linux 2.23.2 (libblkid 2.23.0, 25-Apr-2013)
Usage:
blkid -L <label> | -U <uuid>
blkid [-c <file>] [-ghlLv] [-o <format>] [-s <tag>]
[-t <token>] [<dev> ...]
blkid -p [-s <tag>] [-O <offset>] [-S <size>]
[-o <format>] <dev> ...
blkid -i [-s <tag>] [-o <format>] <dev> ...
Options:
-c <file> read from <file> instead of reading from the default
cache file (-c /dev/null means no cache)
-d don't encode non-printing characters
-h print this usage message and exit
-g garbage collect the blkid cache
-o <format> output format; can be one of:
value, device, export or full; (default: full)
-k list all known filesystems/RAIDs and exit
-s <tag> show specified tag(s) (default show all tags)
-t <token> find device with a specific token (NAME=value pair)
-l look up only first device with token specified by -t
-L <label> convert LABEL to device name
-U <uuid> convert UUID to device name
-V print version and exit
<dev> specify device(s) to probe (default: all devices)
Low-level probing options:
-p low-level superblocks probing (bypass cache)
-i gather information about I/O limits
-S <size> overwrite device size
-O <offset> probe at the given offset
-u <list> filter by "usage" (e.g. -u filesystem,raid)
-n <list> filter by filesystem type (e.g. -n vfat,ext3)
2.1.2 kylin系统中的blkid命令
[purpleendurer @ kylin ~] blkid -help
blkid from util-linux 2.27.1 (libblkid 2.27.0, 02-Nov-2015)
Usage:
blkid -L <label> | -U <uuid>
blkid [-c <file>] [-ghlLv] [-o <format>] [-s <tag>]
[-t <token>] [<dev> ...]
blkid -p [-s <tag>] [-O <offset>] [-S <size>]
[-o <format>] <dev> ...
blkid -i [-s <tag>] [-o <format>] <dev> ...
Options:
-c <file> read from <file> instead of reading from the default
cache file (-c /dev/null means no cache)
-d don't encode non-printing characters
-h print this usage message and exit
-g garbage collect the blkid cache
-o <format> output format; can be one of:
value, device, export or full; (default: full)
-k list all known filesystems/RAIDs and exit
-s <tag> show specified tag(s) (default show all tags)
-t <token> find device with a specific token (NAME=value pair)
-l look up only first device with token specified by -t
-L <label> convert LABEL to device name
-U <uuid> convert UUID to device name
-V print version and exit
<dev> specify device(s) to probe (default: all devices)
Low-level probing options:
-p low-level superblocks probing (bypass cache)
-i gather information about I/O limits
-S <size> overwrite device size
-O <offset> probe at the given offset
-u <list> filter by "usage" (e.g. -u filesystem,raid)
-n <list> filter by filesystem type (e.g. -n vfat,ext3)
2.2 blkid命令 的功能
lsblk命令是block identifier的缩写,可以用来显示系统里所有可用块设备的信息。这些信息包括:
- 设备名称
- UUID:文件系统的通用唯一识别码(Universally Unique Identifier)
- TYPE:文件系统类型
- LABEL:设备标签
- PARTUUID:分区UUID(Universally Unique Identifier)
- PARTLABEL:分区标签
- SEC_TYPE:安全文件系统类型
- ……
2.3 blkid命令格式
blkid [选项] [设备名]
2.4 blkid命令选项说明
2.4.1 常规选项
选项说明-c <file>读取<file>而不是从默认缓存文件中读取(-c /dev/null 表示没有缓存)-d不对非打印字符进行编码-h打印用法消息并退出-g垃圾回收 BLKID 缓存-o <format>
输出格式。可以是以下之一:
- value
- device
- export
- full
默认值:full。
-k列出所有已知的文件系统/RAID 并退出-s <tag>显示指定的标签 (默认 显示所有标签)-t <token>查找具有特定令牌的设备(NAME=值对)-l仅查找 -t 指定的令牌的第一个设备-L <label>将 LABEL 转换为设备名称-U <uuid>将 UUID 转换为设备名称-V打印版本信息并退出
2.4.2 低级探测选项
选项说明-p低级超级块探测(绕过缓存)-i收集有关 I/O 限制的信息-S <size>覆盖设备大小-O <offset>给定偏移量的探针-u按 “usage” 过滤(例如 -u filesystem、raid)-n按文件系统类型过滤(例如 -n vfat,ext3)
2.5 blkid命令的参数说明
<dev>:指定要探测的设备(默认值:所有设备)
3 blkid命令使用实例
3.1 blkid:显示所有的设备信息
[purpleendurer @ kylin ~] blkid
/dev/sda1: UUID="3637ecaf-0f1f-49ca-****-************" TYPE="ext4" PARTUUID="abcdf165-800d-4136-****-************"
/dev/sda2: UUID="617746af-df9e-4ab7-****-************" TYPE="ext4" PARTUUID="8250cc36-ced7-4b23****-************"
/dev/sda3: LABEL="KYLIN-BACKUP" UUID="35a92c0a-bfbf-4fe6-****-************" TYPE="ext4" PARTUUID="945c25d1-41cb-4107-****-************6"
/dev/sda4: LABEL="M-fM-^UM-0M-fM-^MM-.M-gM-^[M-^X" UUID="476a5af9-f192-4578-****-************" TYPE="ext4" PARTUUID="9c311144-55e2-4097-****-************"
/dev/sda5: UUID="1043ec35-b969-4a8b-****-************" TYPE="swap" PARTUUID="57003ffa-e59d-4ee2-****-************"
/dev/sda6: SEC_TYPE="msdos" UUID="194D-****" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="f6acb26c-54c2-4735-****-************"
/dev/sdb: LABEL="M-fM-^UM-0M-fM-^MM-.M-gM-^[M-^X" UUID="4f20f41e-62d1-4512-****-************" TYPE="ext4"
[purpleendurer @ kylin ~]
可以看到,系统中有7个块设备。
设备名称如下:
- /dev/sda1
- /dev/sda2
- /dev/sda3
- /dev/sda4
- /dev/sda5
- /dev/sda6
- /dev/sdb
文件系统类型有:
- ext4
- swap
- vfat
在其它系统可能还有LVM2_member等类型。
3.2 blkid -o export:指定export输出格式
[purpleendurer @ kylin ~] blkid -o export
DEVNAME=/dev/sda1
UUID=3637ecaf-0f1f-49ca-900d-****-************
TYPE=ext4
PARTUUID=abcdf165-800d-4136-****-************
DEVNAME=/dev/sda2
UUID=617746af-df9e-4ab7-****-************
TYPE=ext4
PARTUUID=8250cc36-ced7-4b23-****-************
DEVNAME=/dev/sda3
LABEL=KYLIN-BACKUP
UUID=35a92c0a-bfbf-4fe6-****-************
TYPE=ext4
PARTUUID=945c25d1-41cb-4107-****-************
DEVNAME=/dev/sda4
LABEL=M-fM-^UM-0M-fM-^MM-.M-gM-^[M-^X
UUID=476a5af9-f192-4578-****-************
TYPE=ext4
PARTUUID=9c311144-55e2-4097-****-************
DEVNAME=/dev/sda5
UUID=1043ec35-b969-4a8b-****-************
TYPE=swap
PARTUUID=57003ffa-e59d-4ee2-****-************
DEVNAME=/dev/sda6
SEC_TYPE=msdos
UUID=194D-****
TYPE=vfat
PARTLABEL=EFI\ System\ Partition
PARTUUID=f6acb26c-54c2-4735-****-************
DEVNAME=/dev/sdb
LABEL=M-fM-^UM-0M-fM-^MM-.M-gM-^[M-^X
UUID=4f20f41e-62d1-4512-****-************
TYPE=ext4
[purpleendurer @ kylin ~]
这种格式可以用来在shell脚本中设置变量。
3.3 blkid -o list:以'list'格式显示设备的属性
[purpleendurer @ kylin ~] blkid -o list
device fs_type label mount point UUID
-------------------------------------------------------------------------------
/dev/sda1 ext4 /boot 3637ecaf-0f1f-49ca-****-************
/dev/sda2 ext4 / 617746af-df9e-4ab7-****-************
/dev/sda3 ext4 KYLIN-BACKUP (not mounted) 35a92c0a-bfbf-4fe6-****-************
/dev/sda4 ext4 数据盘 /data 476a5af9-f192-4578-****-************
/dev/sda5 swap [SWAP] 1043ec35-b969-4a8b-****-************
/dev/sda6 vfat /boot/efi 194D-****
/dev/sdb ext4 数据盘 /media/****/数据盘1 4f20f41e-62d1-4512-****-************
/dev/ram0 (not mounted)
/dev/ram1 (not mounted)
/dev/ram2 (not mounted)
/dev/ram3 (not mounted)
/dev/ram4 (not mounted)
/dev/ram5 (not mounted)
/dev/ram6 (not mounted)
/dev/ram7 (not mounted)
/dev/ram8 (not mounted)
/dev/ram9 (not mounted)
/dev/ram10 (not mounted)
/dev/ram11 (not mounted)
/dev/ram12 (not mounted)
/dev/ram13 (not mounted)
/dev/ram14 (not mounted)
/dev/ram15 (not mounted)
[purpleendurer @ kylin ~]
3.4 blkid -t TYPE=swap:显示文件系统为swap的块设备信息
[purpleendurer @ kylin ~] blkid -t TYPE=swap
/dev/sda5: UUID="1043ec35-b969-4a8b-****-************" TYPE="swap" PARTUUID="57003ffa-e59d-4ee2-****-************"
[purpleendurer @ kylin ~]
版权归原作者 紫郢剑侠 所有, 如有侵权,请联系我们删除。