use-lsblk-command-in-linux-to-list-block-device-image

lsblk command in Linux to List Block Devices

The lsblk command in Linux is a useful command for administrators, which is used to list information about all available block devices.

It does not list information about RAM disks.

lsblk command gets the information from the /sys virtual file system to obtain the information and display on the screen.

By default, you will get the information about all block devices in tree-like formate excluding except RAM disks.

lsblk is pre-installed in most of Linux destributions as part of the util-Linux package.

The “util-Linux” package contains a large variety of low-level system utilities that are necessary for a Linux system to function.

Article Contents:

Basic syntax of lsblk command in linux

Open the terminal and run lsblk command without option, You will get information all attached devices including hard disk, DVD Device, Pen Drive etc.

You can see in the following example:

lsblk

[[email protected] ~]$ lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   32G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   31G  0 part 
  ├─cl-root 253:0    0 28.9G  0 lvm  /
  └─cl-swap 253:1    0  2.1G  0 lvm  [SWAP]
sdb           8:16   0    8G  0 disk 
sdc           8:32   0   16G  0 disk 
sr0          11:0    1 56.9M  0 rom  /run/media/vijay/VBox_GAs_6.1.4
[[email protected] ~]$  

There are seven columns namely:

NAME : First column has the device name .

MAJ:MIN : This column shows the major and minor device number.

RM: RM is a short form of removal, yes you can get the information about device is removable or not. If the RM value is 1, then the device is removable and 0 value for the non-removable disk.

SIZE: This column works as its name, It consists of information on the size of the device. For example, 298.1G indicates the device is 298.1GB and 1K indicate the device size is 1KB.

RO : RO stands for Read-Only, yes this indicates whether a device is read-only. In this case all devices have a RO=0, indicating they are not read-only.

TYPE :This column shows information whether the block device is a disk or a partition(part) within a disk.

MOUNTPOINT : This column indicates mount point on which the device is mounted.

View block devices in List Format

If you want to print device information in list format, then use lsblk command followed by -l option.

It only happened you don’t want to print output in a tree-like format, Use command as follows:

#lsblk -l

[[email protected] ~]$ lsblk -l
NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda       8:0    0   32G  0 disk 
sda1      8:1    0    1G  0 part /boot
sda2      8:2    0   31G  0 part 
sdb       8:16   0    8G  0 disk 
sdc       8:32   0   16G  0 disk 
sr0      11:0    1 56.9M  0 rom  /run/media/vijay/VBox_GAs_6.1.4
cl-root 253:0    0 28.9G  0 lvm  /
cl-swap 253:1    0  2.1G  0 lvm  [SWAP]
[[email protected] ~]$ 

List All devices

For your kind information, By default, lsblk command doesn’t print list of empty devices.

If you want to view all devices information including empty files then use command followed by -a option. So the command can be used as follows:

lsblk -a

[[email protected] ~]$ lsblk -a
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   32G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   31G  0 part 
  ├─cl-root 253:0    0 28.9G  0 lvm  /
  └─cl-swap 253:1    0  2.1G  0 lvm  [SWAP]
sdb           8:16   0    8G  0 disk 
sdc           8:32   0   16G  0 disk 
sr0          11:0    1 56.9M  0 rom  /run/media/vijay/VBox_GAs_6.1.4
[[email protected] ~]$  

View Permissions and Owner of Device

-m option can be used with lsblk command to list the ownership of a particular device as well as the group the mode.

The syntax will be as follows:

lsblk -m

[[email protected] ~]$ lsblk -m
NAME         SIZE OWNER GROUP MODE
sda           32G root  disk  brw-rw----
├─sda1         1G root  disk  brw-rw----
└─sda2        31G root  disk  brw-rw----
  ├─cl-root 28.9G root  disk  brw-rw----
  └─cl-swap  2.1G root  disk  brw-rw----
sdb            8G root  disk  brw-rw----
sdc           16G root  disk  brw-rw----
sr0         56.9M root  cdrom brw-rw----
[[email protected] ~]$ 

List Information of Specific Devices

It is possible to get information about a specific device only. You can use print information of spcific device.

You can use lsblk command followed by the device name. For example, you would be interested to know your hard drive size in bytes.

You can use command syntax as follows:

$ lsblk -b /dev/sda

[[email protected] ~]$ lsblk -b /dev/sda1 
NAME MAJ:MIN RM       SIZE RO TYPE MOUNTPOINT
sda1   8:1    0 1073741824  0 part /boot
[[email protected] ~]$
[[email protected] ~]$ lsblk -b /dev/sda
NAME        MAJ:MIN RM        SIZE RO TYPE MOUNTPOINT
sda           8:0    0 34359738368  0 disk 
├─sda1        8:1    0  1073741824  0 part /boot
└─sda2        8:2    0 33284947968  0 part 
  ├─cl-root 253:0    0 31058821120  0 lvm  /
  └─cl-swap 253:1    0  2222981120  0 lvm  [SWAP]
[[email protected] ~]$ 

or if you prefer:

$ lsblk –bytes /dev/sda

You can use the -o flag to filter out only specific fields For example, Name, SIZE etc.

In the following example, I am going to filter fields with the NAME, SIZE and MOUNPOINT attributes for the block devices.

Commands is:

lsblk -o NAME,SIZE,MOUNTPOINT

[[email protected] ~]$ lsblk -o NAME,SIZE,MOUNTPOINT
NAME         SIZE MOUNTPOINT
sda           32G 
├─sda1         1G /boot
└─sda2        31G 
  ├─cl-root 28.9G /
  └─cl-swap  2.1G [SWAP]
sdb            8G 
sdc           16G 
sr0         56.9M /run/media/vijay/VBox_GAs_6.1.4
[[email protected] ~]$ 

List Devices Without Header in List Form

You can also combine two or more options to get the desired output.

For example, If you want to print the list of the devices in a list format instead of the default tree format, I have told you already about -l option.

But if you don’t want to display a header with the name of the different columns, then you can use -n or –noheadings options with lsblk command.

I am going to combine both options -n and -l, So the command syntax and output as follows:

$ lsblk -nl

[[email protected] ~]$ lsblk -nl
sda       8:0    0   32G  0 disk 
sda1      8:1    0    1G  0 part /boot
sda2      8:2    0   31G  0 part 
sdb       8:16   0    8G  0 disk 
sdc       8:32   0   16G  0 disk 
sr0      11:0    1 56.9M  0 rom  /run/media/vijay/VBox_GAs_6.1.4
cl-root 253:0    0 28.9G  0 lvm  /
cl-swap 253:1    0  2.1G  0 lvm  [SWAP]
[[email protected] ~]$  

or still you can use the option which will give the same output.

$ lsblk –noheadings –list

List SCSI Devices by lsblk Command

If you are looming to get a list of SCSI devices only, Then you can use command followed by the option -S.

This option is capital S and it should not be confused with the option -s which prints dependencies in inverse order.

$ lsblk -S

Conclusion:

I’ve tried my best to cover most of the basic uses of lsblk command in Linux, but still, lsblk contains a variety of other expert commands you can use them.

For more detailed information, you can check the manual page. To display the manual page use man command from the terminal.

If I’ve missed any important command, please do share it with me via the comment section.

Special Offer on Ethical hacking Course in Hindi

Special Offer on Ethical hacking Course in Hindi

Ethical-hacking-course-in-hindi-50-off

If Appreciate My Work, You should consider: