Method of using lsblk and blkid to display device information in Linux system

Time:2022-5-13

Today we will show you how to use lsblk and blkid tools to find information about block devices. We use a machine with CentOS 7.0 installed.
lsblk

Lsblk is a Linux tool that displays information about all available block devices in your system. It gets information from the sysfs file system. By default, the tool will display all block devices (except memory virtual disks) in a tree format.
Lsblk default output

By default, lsblk outputs block devices to tree format:

  • Name — name of the device
  • Maj: Min — each device in the Linux operating system is represented by a file. For block (disk) devices, the primary and secondary device numbers are used to describe the devices.
  • RM – removable device. If this is a removable device, 1 will be displayed, otherwise 0 will be displayed.
  • Type – type of equipment
  • Mountpoint – the location where the equipment is mounted
  • Ro — for read-only file system, 1 will be displayed here, otherwise 0 will be displayed.
  • Size – capacity of equipment
    2015612162050741.jpg (760×171)

Displays the owner of the device

If you want to display information about the device owner, including the user, group and file system mount mode of the file, you can use the – M option, like this:

   

Copy code

The code is as follows:

lsblk -m

    2015612162157046.jpg (450×169)

List device blocks

If you only want to list devices and don’t want them to output in a tree, you can use the – L option:

   

Copy code

The code is as follows:

lsblk -l

Use in scripts

Advanced tips: if you want to use it in the script and want to eliminate the header, you can use the – N option as follows:

   

Copy code

The code is as follows:

lsblk -ln

2015612162219104.jpg (706×155)

   

Copy code

The code is as follows:

blkid

The blkid command is a command line tool that displays information about available block devices. It can identify the type of a block device content (such as file system and swap area) and obtain attributes (such as tokens and key value pairs) from the metadata of the content (such as volume label or UUID field). It has two main functions: searching a device with a specified key value pair, or displaying the key value pairs of one or more devices.
How to use blkid

Running blkid without adding any parameters will output all available devices, their universal unique identifier (UUID), file system type and volume label (if set).

   

Copy code

The code is as follows:

# blkid

2015612162240844.jpg (800×99)

List devices by name or UUID

If you only want to display the information of a specific device, you can add the name of the device after the blkid as an option:

   

Copy code

The code is as follows:

# blkid /dev/sda1

If you know the UUID of a device and want to know its device name, you can use the – U option, like this:

   

Copy code

The code is as follows:

# blkid -U d3b1dcc2-e3b0-45b0-b703-d6d0d360e524

  2015612162312204.jpg (655×76)

detailed information

If you want more details, you can use the – P and – O udev options to display them in a neat format, like this:

   

Copy code

The code is as follows:

# blkid -po udev /dev/sda1

   2015612162340145.jpg (516×227)

Reset cache

Sometimes the device list may not be updated. In this case, you can use the – G option to clean up the cache of blkid, so as to clear the devices that no longer exist.

   

Copy code

The code is as follows:

# blkid -g