Linux lsblk Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux lsblk command, which is used to list information about block devices on the system. The lsblk command provides a tree-like listing of devices, making it easy to visualize the relationships between different storage devices and partitions. We will start by understanding the purpose of the lsblk command, then learn how to list all block devices on the system, and finally explore additional options and filters to customize the output.

The lsblk command is a useful tool for system administrators and developers who need to manage and monitor storage devices on their Linux systems. It can be used to quickly identify the storage devices available on the system, their types, sizes, and other relevant information.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/mount("`File System Mounting`") subgraph Lab Skills linux/df -.-> lab-422778{{"`Linux lsblk Command with Practical Examples`"}} linux/mount -.-> lab-422778{{"`Linux lsblk Command with Practical Examples`"}} end

Understand the lsblk Command and Its Purpose

In this step, we will learn about the lsblk command in Linux, which is used to list information about block devices on the system. The lsblk command provides a tree-like listing of devices, making it easy to visualize the relationships between different storage devices and partitions.

The lsblk command is a useful tool for system administrators and developers who need to manage and monitor storage devices on their Linux systems. It can be used to quickly identify the storage devices available on the system, their types, sizes, and other relevant information.

Let's start by running the lsblk command in the terminal:

lsblk

Example output:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0    7:0    0  55.5M  1 loop /snap/core18/2128
loop1    7:1    0  55.4M  1 loop /snap/core18/2284
loop2    7:2    0  43.6M  1 loop /snap/snapd/15534
loop3    7:3    0  61.9M  1 loop /snap/gtk-common-themes/1535
loop4    7:4    0  31.1M  1 loop /snap/snapd/16292
sda      8:0    0   50G  0 disk
├─sda1   8:1    0   49G  0 part /
└─sda2   8:2    0  976M  0 part [SWAP]

The output shows the available block devices on the system, including loop devices (used for mounting snap packages) and the system's main storage device sda, which has two partitions (sda1 and sda2).

The lsblk command provides the following information for each block device:

  • NAME: The name of the block device.
  • MAJ:MIN: The major and minor device numbers.
  • RM: Indicates whether the device is removable (1) or not (0).
  • SIZE: The size of the device.
  • RO: Indicates whether the device is read-only (1) or not (0).
  • TYPE: The type of the block device (e.g., disk, partition, loop).
  • MOUNTPOINT: The mount point of the device, if it is mounted.

This information can be very useful when managing and troubleshooting storage devices on your Linux system.

List All Block Devices on the System

In this step, we will explore the different options and filters available with the lsblk command to list all the block devices on the system.

By default, the lsblk command only displays a limited set of information about the block devices. To see more details, we can use the -a or --all option:

lsblk -a

This will display all the block devices, including those that are not currently mounted.

If you want to see the device types, you can use the -t or --topology option:

lsblk -t

This will show the device hierarchy and the relationships between different block devices.

To get a more detailed view of the block devices, you can use the -l or --list option:

lsblk -l

This will display each block device on a separate line, with more columns of information.

You can also combine multiple options to customize the output. For example, to see all block devices with their size and mount point:

lsblk -alP

The P option stands for "pairs" and will display the information in a key-value format, making it easier to read.

Finally, you can filter the output by device type using the -t or --types option. For example, to only see disk devices:

lsblk -t disk

This will display only the disk devices and hide other types of block devices, such as partitions or loop devices.

Explore Additional Options and Filters with lsblk

In this final step, we will explore some additional options and filters available with the lsblk command to customize the output and get more detailed information about the block devices on your system.

One useful option is --output, which allows you to specify the columns you want to display. For example, to see the device name, size, and mount point:

lsblk --output NAME,SIZE,MOUNTPOINT

This will display only the selected columns, making the output more concise and easier to read.

You can also use the --output-all option to see all the available columns:

lsblk --output-all

This will give you a comprehensive view of all the information available for each block device.

Another handy option is --json, which outputs the block device information in JSON format. This can be useful if you need to programmatically process the data:

lsblk --json

To filter the output by device type, you can use the --types option. For example, to only see partitions:

lsblk --types part

Or to see both partitions and logical volumes:

lsblk --types part,lvm

Finally, you can use the --inverse option to reverse the tree-like hierarchy and show the parent-child relationships in the opposite direction:

lsblk --inverse

This can be helpful when you're trying to understand the overall storage configuration of your system.

By combining these various options and filters, you can tailor the lsblk output to your specific needs and get a better understanding of the block devices on your Linux system.

Summary

In this lab, we learned about the lsblk command in Linux, which is used to list information about block devices on the system. The lsblk command provides a tree-like listing of devices, making it easy to visualize the relationships between different storage devices and partitions. We explored how to use the lsblk command to list all block devices on the system, including their names, device numbers, removability, size, read-only status, device type, and mount points. This information can be very useful when managing and troubleshooting storage devices on a Linux system.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like