lsblk – Command To Display Block Device Information In Linux

Have you ever dealt with block devices in Linux? Well, it is unlikely if you are not a file system pro, but as a system admin you should know some basic commands that can help you debug a file system-related problem in Linux. In this article, we will discuss the lsblk command, which displays block device related information in Linux.

NOTE – To know basics of block devices in Linux, read this tutorial.

lsblk Command in Linux

Here is a snapshot of the description of lsblk command from its man page :

 

Testing Environment

  • OS – Ubuntu 13.04
  • Shell – Bash 4.2.45
  • Application – lsblk 2.20.1-5.1ubuntu8

A Brief Tutorial

Lets understand its usage through some practical examples.

1. List block devices

To list block devices using this command, just run it without any option :

$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 232.9G  0 disk ├─sda1   8:1    0    50G  0 part ├─sda2   8:2    0    20G  0 part ├─sda3   8:3    0 132.9G  0 part ├─sda4   8:4    0     1K  0 part ├─sda5   8:5    0   1.3G  0 part [SWAP]
└─sda6   8:6    0  28.7G  0 part /
sr0     11:0    1  1024M  0 rom

So you can see that a lot of information related to block devices is displayed in the output.

If it is required to display information corresponding to all the devices, use the -a option.

Here is an example :

$ lsblk -a
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 232.9G  0 disk ├─sda1   8:1    0    50G  0 part ├─sda2   8:2    0    20G  0 part ├─sda3   8:3    0 132.9G  0 part ├─sda4   8:4    0     1K  0 part ├─sda5   8:5    0   1.3G  0 part [SWAP]
└─sda6   8:6    0  28.7G  0 part /
sr0     11:0    1  1024M  0 rom  
ram0     1:0    0    64M  0 disk ram1     1:1    0    64M  0 disk ram2     1:2    0    64M  0 disk ram3     1:3    0    64M  0 disk ram4     1:4    0    64M  0 disk ram5     1:5    0    64M  0 disk ram6     1:6    0    64M  0 disk ram7     1:7    0    64M  0 disk ram8     1:8    0    64M  0 disk ram9     1:9    0    64M  0 disk loop0    7:0    0         0 loop loop1    7:1    0         0 loop loop2    7:2    0         0 loop loop3    7:3    0         0 loop loop4    7:4    0         0 loop loop5    7:5    0         0 loop loop6    7:6    0         0 loop loop7    7:7    0         0 loop ram10    1:10   0    64M  0 disk ram11    1:11   0    64M  0 disk ram12    1:12   0    64M  0 disk ram13    1:13   0    64M  0 disk ram14    1:14   0    64M  0 disk ram15    1:15   0    64M  0 disk

So you can see that the information related to all the block devices is displayed in output.

2. Print the SIZE column in bytes

Use the -b option to achieve this :

$ lsblk -b
NAME   MAJ:MIN RM         SIZE RO TYPE MOUNTPOINT
sda      8:0    0 250059350016  0 disk ├─sda1   8:1    0  53686370304  0 part ├─sda2   8:2    0  21476206080  0 part ├─sda3   8:3    0 142683932160  0 part ├─sda4   8:4    0         1024  0 part ├─sda5   8:5    0   1372585984  0 part [SWAP]
└─sda6   8:6    0  30836523008  0 part /
sr0     11:0    1   1073741312  0 rom

So you can see that the SIZE column displays values in bytes.

3. Hide the information related to slaves

In the last example, observe that information related to sda and its slaves was displayed in the output. If you do not want to display slave related information, use the -d option.

$ lsblk -d
NAME MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda    8:0    0 232.9G  0 disk sr0   11:0    1  1024M  0 rom

So you can see that the information related to slaves is not displayed in the output.

4. Output information about the owner, group and mode

To display information related to the owner, group and mode of the block device, use the -m option.

$ lsblk -m
NAME     SIZE OWNER GROUP MODE
sda    232.9G root  disk  brw-rw----
├─sda1    50G root  disk  brw-rw----
├─sda2    20G root  disk  brw-rw----
├─sda3 132.9G root  disk  brw-rw----
├─sda4     1K root  disk  brw-rw----
├─sda5   1.3G root  disk  brw-rw----
└─sda6  28.7G root  disk  brw-rw----
sr0     1024M root  cdrom brw-rw----

5. Use key=value output format

This can be achieved by using -P options.

Here is an example :

$ lsblk -P
NAME="sda" MAJ:MIN="8:0" RM="0" SIZE="232.9G" RO="0" TYPE="disk" MOUNTPOINT=""
NAME="sda1" MAJ:MIN="8:1" RM="0" SIZE="50G" RO="0" TYPE="part" MOUNTPOINT=""
NAME="sda2" MAJ:MIN="8:2" RM="0" SIZE="20G" RO="0" TYPE="part" MOUNTPOINT=""
NAME="sda3" MAJ:MIN="8:3" RM="0" SIZE="132.9G" RO="0" TYPE="part" MOUNTPOINT=""
NAME="sda4" MAJ:MIN="8:4" RM="0" SIZE="1K" RO="0" TYPE="part" MOUNTPOINT=""
NAME="sda5" MAJ:MIN="8:5" RM="0" SIZE="1.3G" RO="0" TYPE="part" MOUNTPOINT="[SWAP]"
NAME="sda6" MAJ:MIN="8:6" RM="0" SIZE="28.7G" RO="0" TYPE="part" MOUNTPOINT="/"
NAME="sr0" MAJ:MIN="11:0" RM="1" SIZE="1024M" RO="0" TYPE="rom" MOUNTPOINT=""

So you can see that the output is displayed in a key=value format.

This command provides a lot of other options, read this man page for more options.

Download/Install/Configure

Here are some of the important links related to the lsblk command :

  • Home Page [Let me know if you find home page of this utility]
  • Download Link

The lsblk command comes as a part of util-linux package which is pre-installed in most of the Linux distributions.

Pros

  • Pre-installed in most Linux distributions
  • Provides lots of options

Cons

  • Some options require good knowledge of block devices in Linux

Conclusion

lsblk is a good utility for fetching information related to block devices. Though it is not for normal users but a handy tool for system administrators and Linux pros. Keep it in your tool set, it’ll definitely help you some day.

Have you ever used lsblk command or any other similar command line utility? Share your experience with us.

The post lsblk – Command To Display Block Device Information In Linux appeared first on MyLinuxBook.

Leave a Reply