lsblk Command in Linux

Introduction to the lsblk Command

This tutorial delves into the Linux command-line utility lsblk, a vital tool for any systemadmin. It provides a comprehensive overview of block devices connected to your system. These devices encompass a wide range, including traditional hard drives, modern SSDs, portable USB drives, and other forms of storage media. lsblk neatly presents this data in a hierarchical, tree-like structure, simplifying the understanding of relationships between storage devices and their respective partitions.

A solid understanding of storage devices is paramount for effective Linux system management. Whether it's identifying available storage resources, verifying partition sizes, or validating mount points, lsblk offers a fast and efficient method for collecting this crucial information. As a systemadmin, this is a command you will use daily.

Upon completion of this guide, you will be proficient in using lsblk with its diverse set of options to retrieve detailed insights into the storage infrastructure of your Linux system.

Understanding Basic lsblk Usage

The acronym lsblk stands for "list block devices." This command serves the primary purpose of displaying information about all accessible block devices on a Linux-based system. These encompass physical storage mediums such as HDDs, solid-state drives (SSDs), and removable USB drives.

Begin by executing the base lsblk command within your terminal environment:

  1. Ensure your terminal is active and ready.

  2. Enter the following command and confirm with the Enter key:

lsblk

Expect output resembling the following example:

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 above output details each block device, with the following key data points:

  • NAME: The designated name of the device.
  • MAJ:MIN: The major and minor device numbers, uniquely identifying the device within the kernel.
  • RM: The removable media flag (1 indicates removable, 0 indicates non-removable).
  • SIZE: The total storage capacity of the device.
  • RO: The read-only status flag (1 signifies read-only, 0 signifies read-write access).
  • TYPE: The device classification (e.g., disk, partition, loop).
  • MOUNTPOINT: The directory path where the device is mounted (if applicable).

From the preceding illustration, observe:

  • Multiple loop devices, typically associated with snap packages.
  • A primary storage device identified as sda, representing a 50GB hard drive.
  • Two partitions residing on sda: sda1 (mounted as the root directory /) and sda2 (configured for swap space).

The command's output structure visually represents the hierarchical relationships, making it easy to discern the partition layout on a particular disk. For instance, both sda1 and sda2 are clearly identified as partitions belonging to the sda disk.

With a basic understanding of the lsblk command's output, let's explore useful options in the following sections.

Advanced lsblk Usage: Displaying Additional Information

This section covers leveraging lsblk with various options to uncover more granular details concerning block devices.

Filesystem Information with the -f Option

The -f option reveals filesystem-specific information, including the filesystem type, volume label, UUID (Universally Unique Identifier), and mount point. This is particularly helpful for identifying devices by UUID or verifying the specific filesystem type in use.

Execute the following command within your terminal:

lsblk -f

Expect output similar to the following:

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 contains the following additional columns:

  • FSTYPE: The filesystem format (e.g., ext4, swap, squashfs).
  • LABEL: Any assigned filesystem label.
  • UUID: The unique identifier for the filesystem.

Device Owner and Mode Information Using the -m Option

The -m option provides details about the permissions and ownership of block devices. It is useful for debugging permission-related problems.

Run the following command to see this information:

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 indicate:

  • 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.

Customizing Output Columns with the -o Option

The -o option empowers you to specify precisely which columns to display, optimizing the output for clarity and relevance. When you are a systemadmin, you will love this option.

Consider displaying only the name, size, and mount point columns:

lsblk -o NAME,SIZE,MOUNTPOINT

The output will be streamlined:

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 streamlines data interpretation, allowing you to concentrate on essential details. You can combine options to fine-tune the output according to your exact requirements.

lsblk Output Filtering Techniques

This section focuses on techniques for filtering the output of lsblk to isolate specific block device types or modify the displayed format.

Filtering by Device Classification

The lsblk command provides the --type or -t option for filtering devices based on their type. Supported device classifications include:

  • disk: Represents physical disk drives.
  • part: Denotes disk partitions.
  • loop: Represents loopback devices.
  • lvm: Represents Logical Volume Manager (LVM) volumes.

To display only disk devices, execute the following:

lsblk --type disk

The output displays only the primary disks, excluding their partitions:

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

To display only partitions, use the following:

lsblk --type part

The output will contain 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 Complete Device Paths

The --paths option instructs lsblk to output the full device paths instead of just the device names. This is particularly advantageous when referencing devices in scripts or commands.

Execute the following command:

lsblk --paths

The output incorporates the complete 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]

JSON Output Format

The --json option formats the output as JSON (JavaScript Object Notation), facilitating programmatic processing and integration with external tools.

Execute this command:

lsblk --json

The resulting output is 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

lsblk's flexibility is amplified by the ability to combine options. For example, retrieve JSON output of only disk devices, including full device paths:

lsblk --type disk --paths --json

The result is a JSON representation of solely the disk devices, featuring their complete paths:

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

These filtering options greatly enhance the command's utility, enabling the retrieval of precisely the required block device information.

Practical lsblk Scenarios for System Administrators

This section explores practical applications of lsblk in common system administration and troubleshooting scenarios.

Disk Usage and Space Availability

Combine lsblk with the -b option (to display sizes in bytes) and selected columns to assess disk usage and available space:

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

The output displays 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 displayed columns show:

  • SIZE: The total size in bytes.
  • FSAVAIL: Available space on the filesystem.
  • FSUSE%: The percentage of filesystem space currently utilized.
  • MOUNTPOINT: The mount point of the device.

USB Drive Identification

Quickly identify a newly connected USB drive by searching for removable devices:

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

Inspect the output for devices with an 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]

A connected USB drive typically appears as a new disk (e.g., sdb) with an RM value of 1.

Retrieving Device Serial Numbers

For hardware inventory or troubleshooting, the lsblk command can reveal device serial numbers:

lsblk -o NAME,SIZE,TYPE,SERIAL

The output includes serial numbers for devices that report them:

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: Not all devices expose serial numbers. Virtual devices like loop devices generally lack serial numbers.

Disk Performance Analysis

Inspect disk performance attributes using the --topology or -t option:

lsblk --topology

The output provides topology information, including alignment and optimal I/O sizes:

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 data is invaluable for performance tuning and resolving I/O bottlenecks.

These examples showcase the versatility of lsblk in routine system administration tasks. Mastering these techniques enables swift retrieval of critical storage device information on your Linux systems.

Summary

This guide has explored the Linux command lsblk, a powerful utility for listing and displaying details about block devices on a Linux system. We have looked at the following areas:

  • Basic usage of lsblk to display a tree-like listing of all block devices.
  • Displaying extra information, including filesystem details, owner/permissions, and custom column selections.
  • Filtering strategies to concentrate on certain device types or display formats.
  • Real-world application cases for system administration tasks.

The lsblk command is an important tool for Linux system administrators and users needing to manage storage devices. Its flexible options enable quickly obtaining the exact information needed regarding disks, partitions, and other block devices on your system. Knowledge of this command is a must for any competent systemadmin.

By mastering the lsblk command, you can more effectively:

  • Recognize and manage storage devices.
  • Monitor disk utilization and accessible space.
  • Address storage-related issues.
  • Document system's storage arrangement.

These skills build a solid base for advanced Linux system administration activities linked to storage management and the role of a systemadmin.

400+ Linux Commands