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:
- The basic syntax of lsblk command in Linux
- View block devices in List Format
- List All devices
- View Permissions and Owner of Device
- Print specific fields value by lsblk command
- List SCSI Devices by lsblk Command
- Conclusion
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
[vijay@localhost ~]$ 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 [vijay@localhost ~]$
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
[vijay@localhost ~]$ 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] [vijay@localhost ~]$
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
[vijay@localhost ~]$ 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 [vijay@localhost ~]$
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
[vijay@localhost ~]$ 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---- [vijay@localhost ~]$
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
[vijay@localhost ~]$ lsblk -b /dev/sda1 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda1 8:1 0 1073741824 0 part /boot [vijay@localhost ~]$
[vijay@localhost ~]$ 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] [vijay@localhost ~]$
or if you prefer:
$ lsblk –bytes /dev/sda
Print specific fields value by lsblk command
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
[vijay@localhost ~]$ 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 [vijay@localhost ~]$
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
[vijay@localhost ~]$ 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] [vijay@localhost ~]$
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