Complete Guide to lsblk Command: List Block Devices
Table of Contents
1. [Introduction](#introduction) 2. [Basic Syntax and Options](#basic-syntax-and-options) 3. [Understanding Block Devices](#understanding-block-devices) 4. [Default Output Format](#default-output-format) 5. [Command Options and Examples](#command-options-and-examples) 6. [Output Formats](#output-formats) 7. [Filtering and Customization](#filtering-and-customization) 8. [Practical Use Cases](#practical-use-cases) 9. [Advanced Examples](#advanced-examples) 10. [Troubleshooting](#troubleshooting)Introduction
The lsblk command is a powerful utility in Linux systems that displays information about block devices in a tree-like format. Block devices are hardware components or logical constructs that store data in fixed-size blocks, such as hard drives, SSDs, USB drives, CD-ROMs, and logical volumes. This command is essential for system administrators, developers, and users who need to understand the storage hierarchy and device relationships on their systems.
The lsblk command reads information from the /sys filesystem and presents it in an easily readable format, showing the relationship between physical devices and their partitions, mount points, and various attributes.
Basic Syntax and Options
The basic syntax for the lsblk command is:
`bash
lsblk [OPTIONS] [DEVICE...]
`
Core Command Structure
| Component | Description | Required | |-----------|-------------|----------| | lsblk | Base command | Yes | | OPTIONS | Flags to modify output | No | | DEVICE | Specific device to examine | No |
Most Common Options
| Option | Long Form | Description | |--------|-----------|-------------| | -a | --all | Show all devices, including empty ones | | -b | --bytes | Print size in bytes | | -d | --nodeps | Don't print slaves or holders | | -f | --fs | Show filesystem information | | -h | --help | Display help message | | -J | --json | Use JSON output format | | -l | --list | Produce output in list format | | -m | --perms | Show permissions | | -n | --noheadings | Don't print headings | | -o | --output | Specify output columns | | -P | --pairs | Produce output in key="value" pairs | | -r | --raw | Use raw output format | | -S | --scsi | Show only SCSI devices | | -t | --topology | Show topology information | | -x | --sort | Sort output by specified column |
Understanding Block Devices
Types of Block Devices
Block devices in Linux can be categorized into several types:
| Device Type | Description | Example | |-------------|-------------|---------| | Physical Drives | Hard disks, SSDs | /dev/sda, /dev/nvme0n1 | | Partitions | Subdivisions of drives | /dev/sda1, /dev/sda2 | | Logical Volumes | LVM managed storage | /dev/mapper/vg-lv | | Loop Devices | File-backed block devices | /dev/loop0 | | RAM Disks | Memory-based storage | /dev/ram0 | | Optical Media | CD/DVD drives | /dev/sr0 | | USB Devices | External storage | /dev/sdb |
Device Naming Conventions
| Prefix | Device Type | Description | |--------|-------------|-------------| | sd | SCSI/SATA | Traditional hard drives and SSDs | | hd | IDE/PATA | Legacy parallel ATA drives | | nvme | NVMe | Modern NVMe SSDs | | sr | SCSI CD-ROM | Optical drives | | loop | Loop device | File-backed virtual devices | | md | Software RAID | Multi-device arrays | | dm | Device Mapper | LVM and other mapped devices |
Default Output Format
When you run lsblk without any options, it displays information in a tree format:
`bash
lsblk
`
Default Columns Explained
| Column | Description | Example | |--------|-------------|---------| | NAME | Device name | sda, sda1 | | MAJ:MIN | Major and minor device numbers | 8:0, 8:1 | | RM | Removable device indicator | 0 (no), 1 (yes) | | SIZE | Device size | 500G, 100M | | RO | Read-only indicator | 0 (read-write), 1 (read-only) | | TYPE | Device type | disk, part, lvm | | MOUNTPOINT | Where device is mounted | /, /home, [SWAP] |
Sample Default Output
`
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 498G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 249G 0 lvm /
sr0 11:0 1 1024M 0 rom
`
Command Options and Examples
Basic Information Display
#### Show All Devices
`bash
lsblk -a
`
This command displays all block devices, including empty ones that are typically hidden.
#### List Format Output
`bash
lsblk -l
`
Produces output in list format instead of tree format:
`
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 500G 0 disk
sda1 8:1 0 512M 0 part /boot/efi
sda2 8:2 0 1G 0 part /boot
sda3 8:3 0 498G 0 part
ubuntu--vg-ubuntu--lv 253:0 0 249G 0 lvm /
sr0 11:0 1 1024M 0 rom
`
#### Show Only Physical Devices
`bash
lsblk -d
`
Displays only the main devices without their partitions or dependencies.
Filesystem Information
#### Display Filesystem Details
`bash
lsblk -f
`
This shows filesystem-related information:
| Column | Description | |--------|-------------| | FSTYPE | Filesystem type (ext4, xfs, ntfs, etc.) | | LABEL | Filesystem label | | UUID | Universally unique identifier | | MOUNTPOINT | Mount location |
Sample output:
`
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 vfat 1234-5678 /boot/efi
├─sda2 ext4 abcd-efgh-ijkl-mnop /boot
└─sda3 LVM2_member qrst-uvwx-yzab-cdef
└─ubuntu--vg-ubuntu--lv ext4 wxyz-abcd-efgh-ijkl /
`
Size and Permissions
#### Show Sizes in Bytes
`bash
lsblk -b
`
Displays all sizes in bytes instead of human-readable format.
#### Display Permissions
`bash
lsblk -m
`
Shows device permissions, ownership, and mode information.
Topology Information
#### Show Device Topology
`bash
lsblk -t
`
Displays physical topology information:
| Column | Description | |--------|-------------| | ALIGNMENT | Alignment offset | | MIN-IO | Minimum I/O size | | OPT-IO | Optimal I/O size | | PHY-SEC | Physical sector size | | LOG-SEC | Logical sector size | | ROTA | Rotational device indicator | | SCHED | I/O scheduler |
Output Formats
JSON Format
`bash
lsblk -J
`
Produces machine-readable JSON output:`json
{
"blockdevices": [
{
"name": "sda",
"maj:min": "8:0",
"rm": false,
"size": "500G",
"ro": false,
"type": "disk",
"mountpoint": null,
"children": [
{
"name": "sda1",
"maj:min": "8:1",
"rm": false,
"size": "512M",
"ro": false,
"type": "part",
"mountpoint": "/boot/efi"
}
]
}
]
}
`
Key-Value Pairs Format
`bash
lsblk -P
`
Outputs information as key="value" pairs, useful for scripting:`
NAME="sda" MAJ:MIN="8:0" RM="0" SIZE="500G" RO="0" TYPE="disk" MOUNTPOINT=""
NAME="sda1" MAJ:MIN="8:1" RM="0" SIZE="512M" RO="0" TYPE="part" MOUNTPOINT="/boot/efi"
`
Raw Format
`bash
lsblk -r
`
Produces raw output without formatting, suitable for parsing by other programs.Filtering and Customization
Custom Column Selection
#### Specify Output Columns
`bash
lsblk -o NAME,SIZE,TYPE,MOUNTPOINT
`
Available columns include:
| Column | Description | |--------|-------------| | NAME | Device name | | KNAME | Internal kernel device name | | MAJ:MIN | Major:minor device number | | FSTYPE | Filesystem type | | MOUNTPOINT | Where the device is mounted | | LABEL | Filesystem label | | UUID | Filesystem UUID | | PARTTYPE | Partition type UUID | | PARTLABEL | Partition label | | PARTUUID | Partition UUID | | RA | Read-ahead of the device | | RO | Read-only device | | RM | Removable device | | MODEL | Device identifier | | SERIAL | Disk serial number | | SIZE | Size of the device | | STATE | State of the device | | OWNER | User name | | GROUP | Group name | | MODE | Device node permissions | | ALIGNMENT | Alignment offset | | MIN-IO | Minimum I/O size | | OPT-IO | Optimal I/O size | | PHY-SEC | Physical sector size | | LOG-SEC | Logical sector size | | ROTA | Rotational device | | SCHED | I/O scheduler name | | RQ-SIZE | Request queue size | | TYPE | Device type | | DISC-ALN | Discard alignment offset | | DISC-GRAN | Discard granularity | | DISC-MAX | Discard max bytes | | DISC-ZERO | Discard zeroes data |
Filtering by Device Type
#### Show Only SCSI Devices
`bash
lsblk -S
`
#### Show Specific Device
`bash
lsblk /dev/sda
`
#### Multiple Devices
`bash
lsblk /dev/sda /dev/sdb
`
Sorting Output
#### Sort by Size
`bash
lsblk -x SIZE
`
#### Sort by Name
`bash
lsblk -x NAME
`
Practical Use Cases
System Administration Tasks
#### Disk Space Monitoring
`bash
lsblk -o NAME,SIZE,AVAIL,USE%,MOUNTPOINT
`
#### Finding Unmounted Devices
`bash
lsblk -o NAME,SIZE,TYPE,MOUNTPOINT | grep -E "(disk|part)" | grep -v "/"
`
#### Checking Filesystem Types
`bash
lsblk -f -o NAME,FSTYPE,SIZE,MOUNTPOINT
`
Storage Management
#### Before Partitioning
`bash
lsblk -d -o NAME,SIZE,MODEL,SERIAL
`
#### After Adding New Storage
`bash
lsblk -f | grep -E "(disk|part)"
`
#### LVM Volume Information
`bash
lsblk -o NAME,SIZE,TYPE | grep -E "(lvm|disk)"
`
Troubleshooting Scenarios
#### Identify Boot Devices
`bash
lsblk -o NAME,SIZE,TYPE,MOUNTPOINT | grep -E "(/boot|/efi)"
`
#### Check for Read-Only Mounts
`bash
lsblk -o NAME,RO,MOUNTPOINT | grep "1"
`
#### Find Large Unused Devices
`bash
lsblk -d -o NAME,SIZE,TYPE | sort -k2 -h
`
Advanced Examples
Complex Filtering and Display
#### Show Only Mounted Filesystems with Details
`bash
lsblk -f -o NAME,FSTYPE,SIZE,USED,AVAIL,USE%,MOUNTPOINT | grep -v "^$" | grep -E "ext4|xfs|btrfs"
`
#### Display USB Devices Only
`bash
lsblk -o NAME,SIZE,TYPE,TRAN,MOUNTPOINT | grep usb
`
#### Show Devices with Serial Numbers
`bash
lsblk -o NAME,SIZE,MODEL,SERIAL,TYPE -d
`
Scripting Applications
#### Generate Device Inventory
`bash
#!/bin/bash
echo "System Storage Inventory - $(date)"
echo "=================================="
lsblk -d -o NAME,SIZE,MODEL,SERIAL,TYPE | while read name size model serial type; do
if [ "$name" != "NAME" ]; then
echo "Device: $name"
echo " Size: $size"
echo " Model: $model"
echo " Serial: $serial"
echo " Type: $type"
echo " Partitions:"
lsblk /dev/$name -o NAME,SIZE,FSTYPE,MOUNTPOINT | tail -n +2 | sed 's/^/ /'
echo ""
fi
done
`
#### Check for Unmounted Partitions
`bash
#!/bin/bash
unmounted=$(lsblk -rno NAME,TYPE,MOUNTPOINT | awk '$2=="part" && $3=="" {print $1}')
if [ -n "$unmounted" ]; then
echo "Unmounted partitions found:"
for partition in $unmounted; do
echo " /dev/$partition"
lsblk -o NAME,SIZE,FSTYPE /dev/$partition | tail -n +2
done
else
echo "All partitions are mounted."
fi
`
Performance Analysis
#### I/O Characteristics
`bash
lsblk -t -o NAME,ALIGNMENT,MIN-IO,OPT-IO,PHY-SEC,LOG-SEC,ROTA,SCHED
`
#### Storage Hierarchy Analysis
`bash
lsblk -o NAME,SIZE,TYPE,TRAN,SUBSYSTEMS | grep -E "(sata|nvme|usb|scsi)"
`
Troubleshooting
Common Issues and Solutions
#### Issue: lsblk Command Not Found
Problem: The system doesn't recognize the lsblk command.
Solution: Install the util-linux package:
`bash
Ubuntu/Debian
sudo apt-get install util-linuxRHEL/CentOS/Fedora
sudo yum install util-linuxor
sudo dnf install util-linux`#### Issue: Permission Denied
Problem: Cannot access device information.
Solution: Run with appropriate permissions:
`bash
sudo lsblk
`
#### Issue: Incomplete Information
Problem: Some columns show empty values.
Solution: Use different options or check device status:
`bash
lsblk -f -a # Show all devices with filesystem info
`
Verification Commands
#### Cross-Reference with Other Tools
`bash
Compare with fdisk
lsblk -d sudo fdisk -lCompare with df for mounted filesystems
lsblk -f | grep -v "^$" df -hVerify with /proc/partitions
lsblk -r cat /proc/partitions`Performance Considerations
The lsblk command is generally fast and lightweight, but for systems with many devices, consider:
#### Limiting Output
`bash
Show only specific device types
lsblk -t diskLimit to essential columns
lsblk -o NAME,SIZE,MOUNTPOINT`#### Caching Results
For repeated queries in scripts:
`bash
Cache output for multiple uses
LSBLK_OUTPUT=$(lsblk -J) echo "$LSBLK_OUTPUT" | jq '.blockdevices[].name'`Best Practices
Regular System Monitoring
1. Daily Checks: Include lsblk in daily system monitoring scripts
2. Change Detection: Compare current output with baseline to detect changes
3. Documentation: Maintain records of device configurations
Security Considerations
1. Sensitive Information: Be aware that device serial numbers and UUIDs may be sensitive 2. Access Control: Limit access to detailed device information in multi-user environments 3. Logging: Consider logging device changes for security auditing
Integration with Other Tools
The lsblk command works well with other Linux utilities:
`bash
Combine with grep for filtering
lsblk | grep -E "(disk|part)"Use with awk for processing
lsblk -r | awk '$6=="disk" {print $1, $4}'Integrate with find for device files
lsblk -rno NAME | while read dev; do find /dev -name "$dev"; done`This comprehensive guide covers the essential aspects of using the lsblk command effectively. The command is invaluable for understanding storage device hierarchies, managing filesystems, and troubleshooting storage-related issues in Linux systems. Regular practice with different options and scenarios will help system administrators and users become proficient in storage management tasks.