Linux lsblk Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux lsblk command, which displays information about all available block devices on your system. Block devices include hard drives, SSDs, USB drives, and other storage media. The lsblk command presents this information in a clear, tree-like format that makes it easy to understand the relationship between different storage devices and their partitions.

Understanding storage devices is essential for anyone working with Linux systems. Whether you need to identify available storage, check partition sizes, or verify mount points, the lsblk command provides a quick and efficient way to gather this information.

By the end of this lab, you will be able to use the lsblk command with various options to obtain detailed information about the storage devices on your Linux system.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/SystemInformationandMonitoringGroup -.-> linux/df("Disk Space Reporting") linux/SystemInformationandMonitoringGroup -.-> linux/ps("Process Displaying") subgraph Lab Skills linux/ls -.-> lab-422778{{"Linux lsblk Command with Practical Examples"}} linux/df -.-> lab-422778{{"Linux lsblk Command with Practical Examples"}} linux/ps -.-> lab-422778{{"Linux lsblk Command with Practical Examples"}} end

Basic Usage of the lsblk Command

The lsblk command stands for "list block devices" and is used to display information about all available block devices on your Linux system. Block devices are storage devices such as hard drives, SSDs, and USB drives.

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

  1. Open your terminal if it's not already open.

  2. Type the following command and press Enter:

lsblk

You should see output similar to this:

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]

This output shows all block devices on your system, with the following columns:

  • NAME: The device name
  • MAJ:MIN: Major and minor device numbers
  • RM: Removable flag (1 means removable, 0 means not removable)
  • SIZE: The size of the device
  • RO: Read-only flag (1 means read-only, 0 means read-write)
  • TYPE: Device type (disk, partition, loop, etc.)
  • MOUNTPOINT: Where the device is mounted (if applicable)

In the example above, you can see:

  • Several loop devices used by snap packages
  • A main storage device sda (which is a 50GB disk)
  • Two partitions on sda: sda1 (mounted at the root /) and sda2 (used as swap space)

The tree-like structure in the output makes it easy to visualize which partitions belong to which disks. For instance, you can see that both sda1 and sda2 are partitions of the sda disk.

Now that you understand the basic output of the lsblk command, let's explore some useful options in the next steps.

Displaying Additional Information with lsblk

In this step, we will learn how to use the lsblk command with different options to display more detailed information about block devices.

Using the -f Option to Show Filesystem Information

The -f option shows filesystem information, including the filesystem type, label, UUID, and mount point. This is particularly useful when you need to identify devices by their UUID or check what filesystem types are in use.

Run the following command in your terminal:

lsblk -f

You should see output similar to this:

NAME   FSTYPE   LABEL UUID                                 MOUNTPOINT
loop0  squashfs                                            /snap/core18/2128
loop1  squashfs                                            /snap/core18/2284
loop2  squashfs                                            /snap/snapd/15534
loop3  squashfs                                            /snap/gtk-common-themes/1535
loop4  squashfs                                            /snap/snapd/16292
sda
├─sda1 ext4           5fbb8eed-12a3-4b5c-9d67-9594ff4e2d1c /
└─sda2 swap           b409ae25-7589-44eb-a909-b56f1d42c5ab [SWAP]

The output now includes additional columns:

  • FSTYPE: The filesystem type (ext4, swap, squashfs, etc.)
  • LABEL: Any filesystem label that has been assigned
  • UUID: The Universal Unique Identifier for the filesystem

Using the -m Option to Show Device Owner and Mode Information

The -m option displays permissions and owner information for the block devices. This is useful when troubleshooting permission-related issues.

Run the following command:

lsblk -m

You should see output like this:

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

The new columns show:

  • OWNER: The user who owns the device (typically root)
  • GROUP: The group owner of the device (typically disk)
  • MODE: The permission mode of the device

Using the -o Option to Select Specific Output Columns

The -o option allows you to specify which columns to display. This is useful when you only need certain information and want a cleaner, more focused output.

Let's display only the name, size, and mountpoint columns:

lsblk -o NAME,SIZE,MOUNTPOINT

The output will be much simpler:

NAME    SIZE MOUNTPOINT
loop0  55.5M /snap/core18/2128
loop1  55.4M /snap/core18/2284
loop2  43.6M /snap/snapd/15534
loop3  61.9M /snap/gtk-common-themes/1535
loop4  31.1M /snap/snapd/16292
sda     50G
├─sda1   49G /
└─sda2  976M [SWAP]

This customized output makes it easier to focus on just the information you need. You can combine multiple options to get exactly the information you want in the format that works best for your needs.

Filtering Block Devices with lsblk

In this step, we will learn how to filter the output of the lsblk command to focus on specific types of block devices or display information in different formats.

Filtering by Device Type

The lsblk command allows you to filter devices by their type using the --type or -t option. Common device types include:

  • disk: Physical disks
  • part: Partitions
  • loop: Loop devices
  • lvm: Logical volumes

To display only disk devices, run the following command:

lsblk --type disk

The output will show only the main disks, without their partitions:

NAME MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda    8:0    0   50G  0 disk

Similarly, to display only partition devices, run:

lsblk --type part

The output will show only the partitions:

NAME MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda1   8:1    0   49G  0 part /
sda2   8:2    0  976M  0 part [SWAP]

Displaying Device Paths

The --paths option displays the full device paths rather than just the device names. This is useful when you need to reference the devices in scripts or commands.

Run the following command:

lsblk --paths

The output will include the full device paths:

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

Displaying Output in JSON Format

The --json option outputs the information in JSON format, which is useful for programmatic processing or when integrating with other tools.

Run the following command:

lsblk --json

The output will be in JSON format:

{
  "blockdevices": [
    {
      "name": "loop0",
      "maj:min": "7:0",
      "rm": false,
      "size": "55.5M",
      "ro": true,
      "type": "loop",
      "mountpoint": "/snap/core18/2128"
    },
    {
      "name": "loop1",
      "maj:min": "7:1",
      "rm": false,
      "size": "55.4M",
      "ro": true,
      "type": "loop",
      "mountpoint": "/snap/core18/2284"
    },
    {
      "name": "loop2",
      "maj:min": "7:2",
      "rm": false,
      "size": "43.6M",
      "ro": true,
      "type": "loop",
      "mountpoint": "/snap/snapd/15534"
    },
    {
      "name": "loop3",
      "maj:min": "7:3",
      "rm": false,
      "size": "61.9M",
      "ro": true,
      "type": "loop",
      "mountpoint": "/snap/gtk-common-themes/1535"
    },
    {
      "name": "loop4",
      "maj:min": "7:4",
      "rm": false,
      "size": "31.1M",
      "ro": true,
      "type": "loop",
      "mountpoint": "/snap/snapd/16292"
    },
    {
      "name": "sda",
      "maj:min": "8:0",
      "rm": false,
      "size": "50G",
      "ro": false,
      "type": "disk",
      "children": [
        {
          "name": "sda1",
          "maj:min": "8:1",
          "rm": false,
          "size": "49G",
          "ro": false,
          "type": "part",
          "mountpoint": "/"
        },
        {
          "name": "sda2",
          "maj:min": "8:2",
          "rm": false,
          "size": "976M",
          "ro": false,
          "type": "part",
          "mountpoint": "[SWAP]"
        }
      ]
    }
  ]
}

Combining Options for Precise Control

You can combine multiple options to get exactly the information you need. For example, to display only disk devices with their full paths in JSON format:

lsblk --type disk --paths --json

The output will be a JSON representation of only the disk devices with their full paths:

{
  "blockdevices": [
    {
      "name": "/dev/sda",
      "maj:min": "8:0",
      "rm": false,
      "size": "50G",
      "ro": false,
      "type": "disk"
    }
  ]
}

These filtering options make the lsblk command very flexible and allow you to get precisely the information you need about block devices on your system.

Advanced Usage of lsblk for Practical Scenarios

In this step, we will explore practical scenarios where the lsblk command proves valuable for system administration and troubleshooting tasks.

Checking Disk Usage and Available Space

To check disk usage and available space, you can combine lsblk with the -b option (to show sizes in bytes) and select specific columns:

lsblk -b -o NAME,SIZE,FSAVAIL,FSUSE%,MOUNTPOINT

The output will show disk usage information:

NAME     SIZE FSAVAIL FSUSE% MOUNTPOINT
loop0  58195968        0     - /snap/core18/2128
loop1  58130432        0     - /snap/core18/2284
loop2  45719552        0     - /snap/snapd/15534
loop3  64897024        0     - /snap/gtk-common-themes/1535
loop4  32604160        0     - /snap/snapd/16292
sda   53687091200       -     -
├─sda1 52613349376 39128932352  26% /
└─sda2 1023410176        0     - [SWAP]

The columns show:

  • SIZE: The total size in bytes
  • FSAVAIL: Available space on the filesystem
  • FSUSE%: Percentage of filesystem used
  • MOUNTPOINT: Where the device is mounted

Identifying USB Drives

When you plug in a USB drive, you can use lsblk to quickly identify it by looking for removable devices:

lsblk -o NAME,SIZE,TYPE,RM,MOUNTPOINT

In the output, look for devices with RM (removable) value of 1:

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

If you had a USB drive connected, it would typically show up as an additional disk (such as sdb) with RM value of 1.

Finding Device Serial Numbers

For hardware inventory or troubleshooting, you might need to find device serial numbers. The lsblk command can display this information:

lsblk -o NAME,SIZE,TYPE,SERIAL

The output will include serial numbers for devices that provide this information:

NAME   SIZE TYPE SERIAL
loop0  55.5M loop
loop1  55.4M loop
loop2  43.6M loop
loop3  61.9M loop
loop4  31.1M loop
sda     50G disk ABCD1234
├─sda1   49G part
└─sda2  976M part

Note that not all devices report serial numbers, and virtual devices like loop devices typically don't have serial numbers.

Analyzing Disk Performance Attributes

To view disk performance attributes, you can use the --topology or -t option:

lsblk --topology

The output includes topology information like alignment and optimal I/O size:

NAME   ALIGNMENT MIN-IO OPT-IO PHY-SEC LOG-SEC ROTA SCHED     RQ-SIZE  RA WSAME
loop0          0    512      0     512     512    1 mq-deadline     256 128    0B
loop1          0    512      0     512     512    1 mq-deadline     256 128    0B
loop2          0    512      0     512     512    1 mq-deadline     256 128    0B
loop3          0    512      0     512     512    1 mq-deadline     256 128    0B
loop4          0    512      0     512     512    1 mq-deadline     256 128    0B
sda            0    512      0     512     512    1 mq-deadline     256 128    0B
├─sda1         0    512      0     512     512    1 mq-deadline     256 128    0B
└─sda2         0    512      0     512     512    1 mq-deadline     256 128    0B

This information can be useful for performance tuning and troubleshooting I/O-related issues.

These practical examples demonstrate how versatile the lsblk command is for everyday system administration tasks. By mastering these techniques, you can quickly gather important information about storage devices on your Linux system.

Summary

In this lab, we explored the Linux lsblk command, a powerful tool for listing and displaying information about block devices on your system. We covered several important aspects of the command:

  • Basic usage of lsblk to display a tree-like listing of all block devices
  • How to display additional information including filesystem details, owner/permissions, and custom column selections
  • Filtering techniques to focus on specific device types or display formats
  • Advanced usage scenarios for system administration tasks

The lsblk command is an essential tool for Linux system administrators and users who need to manage storage devices. Its flexible options allow you to quickly obtain the exact information you need about disks, partitions, and other block devices on your system.

By mastering the lsblk command, you can more effectively:

  • Identify and manage storage devices
  • Monitor disk usage and available space
  • Troubleshoot storage-related issues
  • Document your system's storage configuration

These skills form an important foundation for more advanced Linux system administration tasks related to storage management.

Linux Commands Cheat Sheet