How to list block devices → lsblk
How to List Block Devices → lsblk
Table of Contents
1. [Introduction](#introduction)
2. [Prerequisites](#prerequisites)
3. [Understanding Block Devices](#understanding-block-devices)
4. [Basic lsblk Usage](#basic-lsblk-usage)
5. [Command Options and Parameters](#command-options-and-parameters)
6. [Practical Examples](#practical-examples)
7. [Output Formats and Customization](#output-formats-and-customization)
8. [Advanced Use Cases](#advanced-use-cases)
9. [Troubleshooting Common Issues](#troubleshooting-common-issues)
10. [Best Practices and Tips](#best-practices-and-tips)
11. [Alternative Commands](#alternative-commands)
12. [Conclusion](#conclusion)
Introduction
The `lsblk` command is an essential tool for Linux system administrators and users who need to understand their system's storage configuration. This powerful utility displays all available block devices in a tree format, providing crucial information about hard drives, SSDs, USB drives, optical drives, and other storage devices connected to your system.
In this comprehensive guide, you'll learn how to effectively use the `lsblk` command to list, analyze, and manage block devices on your Linux system. We'll cover everything from basic usage to advanced filtering techniques, output customization, and troubleshooting common issues.
Prerequisites
Before diving into the `lsblk` command, ensure you have:
- A Linux-based operating system (Ubuntu, CentOS, Debian, Fedora, etc.)
- Basic familiarity with the command line interface
- Understanding of fundamental Linux concepts like filesystems and mount points
- Access to a terminal or SSH connection to your Linux system
Note: The `lsblk` command is available by default on most modern Linux distributions as part of the `util-linux` package.
Understanding Block Devices
Before exploring the `lsblk` command, it's important to understand what block devices are in the Linux ecosystem.
What are Block Devices?
Block devices are storage devices that allow data to be read and written in fixed-size blocks. These include:
- Hard Disk Drives (HDDs): Traditional mechanical storage devices
- Solid State Drives (SSDs): Flash-based storage devices
- USB Flash Drives: Portable storage devices
- SD Cards: Memory cards used in cameras and mobile devices
- Optical Drives: CD/DVD/Blu-ray drives
- Network Block Devices: Remote storage accessed over a network
- Loop Devices: Virtual block devices backed by files
Device Naming Convention
Linux uses a specific naming convention for block devices:
- `/dev/sda`, `/dev/sdb`, `/dev/sdc`: SCSI/SATA drives
- `/dev/nvme0n1`, `/dev/nvme1n1`: NVMe drives
- `/dev/mmcblk0`, `/dev/mmcblk1`: MMC/SD card devices
- `/dev/vda`, `/dev/vdb`: Virtual drives (in VMs)
Partitions are numbered sequentially: `/dev/sda1`, `/dev/sda2`, etc.
Basic lsblk Usage
Simple Device Listing
The most basic usage of `lsblk` is to run it without any options:
```bash
lsblk
```
This command displays all available block devices in a tree structure. Here's an example output:
```
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 18.5G 0 part /
sdb 8:16 0 5G 0 disk
└─sdb1 8:17 0 5G 0 part /home
sr0 11:0 1 1024M 0 rom
```
Understanding the Output Columns
Each column in the default output provides specific information:
- NAME: Device name (e.g., sda, sda1)
- MAJ:MIN: Major and minor device numbers
- RM: Removable device (1 if removable, 0 if not)
- SIZE: Size of the device
- RO: Read-only device (1 if read-only, 0 if writable)
- TYPE: Device type (disk, part, rom, etc.)
- MOUNTPOINT: Where the device is mounted in the filesystem
Command Options and Parameters
The `lsblk` command offers numerous options to customize its output and behavior.
Display Options
Show All Devices
```bash
lsblk -a
```
Shows all devices, including empty ones that are normally hidden.
List Specific Device
```bash
lsblk /dev/sda
```
Display information only for the specified device.
Show Filesystem Information
```bash
lsblk -f
```
Display filesystem information including filesystem type, UUID, and labels:
```
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 vfat 1234-5678 /boot/efi
├─sda2 ext4 abcd1234-5678-90ef-ghij-klmnopqrstuv /boot
└─sda3 ext4 efgh5678-90ab-cdef-1234-567890abcdef /
```
Size and Format Options
Human-Readable Sizes
```bash
lsblk -h
```
Display sizes in human-readable format (default behavior in most distributions).
Show Sizes in Bytes
```bash
lsblk -b
```
Display all sizes in bytes instead of human-readable format.
JSON Output Format
```bash
lsblk -J
```
Output information in JSON format, useful for scripting:
```json
{
"blockdevices": [
{
"name": "sda",
"maj:min": "8:0",
"rm": false,
"size": "20G",
"ro": false,
"type": "disk",
"mountpoint": null,
"children": [
{
"name": "sda1",
"maj:min": "8:1",
"rm": false,
"size": "512M",
"ro": false,
"type": "part",
"mountpoint": "/boot/efi"
}
]
}
]
}
```
Tree Structure Options
Disable Tree Format
```bash
lsblk -l
```
Display devices in list format instead of tree format.
Show Dependencies
```bash
lsblk -D
```
Display device dependencies and relationships.
Practical Examples
Example 1: Identifying USB Drives
To identify USB drives and removable media:
```bash
lsblk -o NAME,SIZE,TYPE,MOUNTPOINT,RM | grep -E "(1|disk)"
```
This command filters for removable devices, making it easier to identify USB drives and SD cards.
Example 2: Checking Disk Usage
To see mounted filesystems with their usage:
```bash
lsblk -o NAME,SIZE,USED,AVAIL,USE%,MOUNTPOINT
```
Note: The USED, AVAIL, and USE% columns require the device to be mounted.
Example 3: Finding Device UUIDs
UUIDs are essential for configuring `/etc/fstab`:
```bash
lsblk -o NAME,UUID,MOUNTPOINT
```
Output:
```
NAME UUID MOUNTPOINT
sda
├─sda1 1234-5678 /boot/efi
├─sda2 abcd1234-5678-90ef-ghij-klmnopqrstuv /boot
└─sda3 efgh5678-90ab-cdef-1234-567890abcdef /
```
Example 4: Monitoring Storage Topology
To understand the complete storage topology including RAID and LVM:
```bash
lsblk -S
```
This shows SCSI devices with additional information:
```
NAME HCTL TYPE VENDOR MODEL REV TRAN
sda 0:0:0:0 disk ATA VBOX HARDDISK 1.0 sata
sdb 1:0:0:0 disk ATA VBOX HARDDISK 1.0 sata
```
Output Formats and Customization
Custom Column Selection
You can specify exactly which columns to display:
```bash
lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT
```
Available Columns
Some of the most useful columns include:
- NAME: Device name
- SIZE: Device size
- TYPE: Device type
- FSTYPE: Filesystem type
- MOUNTPOINT: Mount point
- UUID: Filesystem UUID
- LABEL: Filesystem label
- MODEL: Device model
- SERIAL: Device serial number
- STATE: Device state
- OWNER: Device owner
- GROUP: Device group
- MODE: Device permissions
Raw Output for Scripting
For scripting purposes, use raw output:
```bash
lsblk -rn -o NAME,SIZE,TYPE
```
The `-r` option produces raw output, and `-n` removes headers.
Advanced Use Cases
Scripting with lsblk
Here's a practical script to find the largest unmounted disk:
```bash
#!/bin/bash
Find the largest unmounted disk
largest_disk=$(lsblk -rn -o NAME,SIZE,TYPE,MOUNTPOINT | \
awk '$3=="disk" && $4=="" {print $2" "$1}' | \
sort -hr | \
head -1 | \
awk '{print $2}')
if [ -n "$largest_disk" ]; then
echo "Largest unmounted disk: /dev/$largest_disk"
lsblk /dev/$largest_disk
else
echo "No unmounted disks found"
fi
```
Monitoring Hot-Plugged Devices
To monitor for newly connected USB devices:
```bash
Before connecting device
lsblk > /tmp/before.txt
After connecting device
lsblk > /tmp/after.txt
Show differences
diff /tmp/before.txt /tmp/after.txt
```
Integration with Other Commands
Combine `lsblk` with other utilities for comprehensive system analysis:
```bash
Show disk usage alongside block device information
lsblk -o NAME,SIZE,MOUNTPOINT && echo && df -h
```
Troubleshooting Common Issues
Issue 1: lsblk Command Not Found
Problem: The system reports that `lsblk` is not found.
Solution: Install the `util-linux` package:
```bash
Ubuntu/Debian
sudo apt-get install util-linux
CentOS/RHEL/Fedora
sudo yum install util-linux
or
sudo dnf install util-linux
```
Issue 2: Permission Denied
Problem: Cannot access certain device information.
Solution: Run with elevated privileges:
```bash
sudo lsblk
```
Some device information requires root access to display properly.
Issue 3: Outdated Information
Problem: `lsblk` shows outdated or incorrect information.
Solution: Force a rescan of block devices:
```bash
Rescan SCSI bus
echo "- - -" | sudo tee /sys/class/scsi_host/host*/scan
Or use partprobe to re-read partition tables
sudo partprobe
```
Issue 4: Missing Filesystem Information
Problem: Filesystem type or UUID not showing.
Solution: Ensure the filesystem is properly formatted and recognized:
```bash
Check filesystem
sudo file -s /dev/sdX1
Force filesystem check
sudo fsck /dev/sdX1
```
Issue 5: Devices Not Showing
Problem: Expected devices don't appear in the output.
Solution: Check if devices are properly connected and recognized:
```bash
Check kernel messages
dmesg | tail -20
Check all devices including empty ones
lsblk -a
Verify device files exist
ls -la /dev/sd /dev/nvme
```
Best Practices and Tips
Regular System Monitoring
1. Create Aliases: Set up useful aliases in your `.bashrc`:
```bash
alias lsblk-full='lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT,UUID'
alias lsblk-usb='lsblk -o NAME,SIZE,TYPE,MOUNTPOINT,RM | grep -E "(1|NAME)"'
```
2. Document Your Storage: Keep a record of your storage configuration:
```bash
lsblk -f > /root/storage-config-$(date +%Y%m%d).txt
```
Security Considerations
1. Sensitive Information: Be aware that `lsblk` output may contain sensitive information like device serial numbers and UUIDs.
2. Access Control: In multi-user environments, consider who has access to storage information.
Performance Tips
1. Specific Device Queries: When possible, query specific devices rather than all devices:
```bash
lsblk /dev/sda # Faster than lsblk for single device info
```
2. Minimal Columns: Only request the columns you need:
```bash
lsblk -o NAME,SIZE,MOUNTPOINT # Faster than full output
```
Integration with System Administration
1. Backup Scripts: Use `lsblk` to identify backup targets:
```bash
Find mounted filesystems for backup
lsblk -o NAME,MOUNTPOINT | grep -v "^NAME" | grep "/"
```
2. Capacity Planning: Monitor disk usage trends:
```bash
Create a simple capacity report
echo "=== Block Device Report ===" > report.txt
date >> report.txt
lsblk -o NAME,SIZE,TYPE,MOUNTPOINT >> report.txt
```
Automation and Scripting
1. JSON Parsing: Use `jq` with JSON output for complex parsing:
```bash
lsblk -J | jq '.blockdevices[] | select(.type=="disk") | .name'
```
2. Conditional Logic: Make decisions based on device properties:
```bash
#!/bin/bash
if lsblk -o MOUNTPOINT | grep -q "/mnt/backup"; then
echo "Backup drive is mounted"
else
echo "Backup drive not found"
fi
```
Alternative Commands
While `lsblk` is comprehensive, other commands provide complementary information:
fdisk
```bash
sudo fdisk -l # List all partitions
```
parted
```bash
sudo parted -l # List partition information
```
blkid
```bash
sudo blkid # Show block device attributes
```
df
```bash
df -h # Show mounted filesystem usage
```
lshw
```bash
sudo lshw -class disk # Hardware information about disks
```
Conclusion
The `lsblk` command is an indispensable tool for Linux system administrators and users who need to understand and manage their storage devices. Its flexibility in output formatting, comprehensive device information, and integration capabilities make it essential for daily system administration tasks.
Key takeaways from this guide:
1. Basic Usage: `lsblk` provides immediate insight into your storage configuration
2. Customization: Extensive options allow you to tailor output to your specific needs
3. Scripting: JSON and raw output formats enable powerful automation
4. Troubleshooting: Understanding common issues helps resolve problems quickly
5. Best Practices: Regular monitoring and documentation improve system management
As you continue working with Linux systems, `lsblk` will prove invaluable for tasks ranging from simple device identification to complex storage management scenarios. Practice with the various options and integrate them into your regular system administration workflow to maximize their effectiveness.
Remember to always exercise caution when working with block devices, especially when making changes to partitions or filesystems. The information provided by `lsblk` is crucial for making informed decisions about storage management and system configuration.