How to show block devices with lsblk

How to Show Block Devices with lsblk The `lsblk` command is one of the most essential tools for Linux system administrators and users who need to understand their system's storage architecture. This comprehensive guide will teach you everything you need to know about using `lsblk` to display block devices, from basic usage to advanced formatting options and troubleshooting techniques. Table of Contents 1. [Introduction to lsblk](#introduction-to-lsblk) 2. [Prerequisites](#prerequisites) 3. [Basic lsblk Usage](#basic-lsblk-usage) 4. [Understanding lsblk Output](#understanding-lsblk-output) 5. [Advanced lsblk Options](#advanced-lsblk-options) 6. [Formatting and Customization](#formatting-and-customization) 7. [Practical Examples and Use Cases](#practical-examples-and-use-cases) 8. [Filtering and Sorting](#filtering-and-sorting) 9. [Integration with Other Commands](#integration-with-other-commands) 10. [Troubleshooting Common Issues](#troubleshooting-common-issues) 11. [Best Practices and Tips](#best-practices-and-tips) 12. [Conclusion](#conclusion) Introduction to lsblk The `lsblk` (list block devices) command is a powerful utility that displays information about all available or specified block devices in a tree-like format. Block devices are storage devices that can be accessed in fixed-size blocks, including hard drives, SSDs, USB drives, CD/DVD drives, and virtual storage devices. Unlike other disk utility commands such as `fdisk` or `parted`, `lsblk` provides a clear, hierarchical view of storage devices and their relationships, making it easier to understand complex storage configurations including RAID arrays, LVM setups, and encrypted partitions. Key Benefits of lsblk - Non-destructive: `lsblk` only reads information and never modifies disk structures - Comprehensive: Shows relationships between physical devices, partitions, and mount points - Flexible output: Supports various output formats and customizable columns - No root privileges required: Most functionality works with regular user permissions - Cross-platform: Available on most Linux distributions by default Prerequisites Before diving into `lsblk` usage, ensure you have: System Requirements - A Linux-based operating system (most distributions include `lsblk` by default) - Terminal or command-line access - Basic familiarity with Linux command-line interface Package Installation On most modern Linux distributions, `lsblk` is included in the `util-linux` package, which is typically installed by default. If for some reason it's missing, you can install it: Ubuntu/Debian: ```bash sudo apt update sudo apt install util-linux ``` RHEL/CentOS/Fedora: ```bash sudo yum install util-linux or for newer versions sudo dnf install util-linux ``` Arch Linux: ```bash sudo pacman -S util-linux ``` Verification Verify that `lsblk` is installed and accessible: ```bash lsblk --version ``` This should display version information, confirming the command is available. Basic lsblk Usage Simple lsblk Command The most basic usage of `lsblk` requires no additional parameters: ```bash lsblk ``` This command displays all available block devices in a tree format, showing the hierarchical relationship between devices, partitions, and their mount points. Example Basic 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 └─ubuntu--vg-ubuntu--lv 253:0 0 18.5G 0 lvm / sdb 8:16 0 5G 0 disk └─sdb1 8:17 0 5G 0 part /mnt/backup sr0 11:0 1 1024M 0 rom ``` Command Syntax The general syntax for `lsblk` is: ```bash lsblk [OPTIONS] [DEVICE...] ``` Where: - `OPTIONS`: Various flags to modify output behavior - `DEVICE`: Specific device(s) to display (optional) Understanding lsblk Output Default Column Meanings The default `lsblk` output includes several columns that provide essential information about each block device: | Column | Description | Example Values | |--------|-------------|----------------| | NAME | Device name | sda, sda1, nvme0n1 | | MAJ:MIN | Major and minor device numbers | 8:0, 8:1, 259:0 | | RM | Removable device indicator | 0 (no), 1 (yes) | | SIZE | Device size | 20G, 512M, 1T | | RO | Read-only indicator | 0 (read-write), 1 (read-only) | | TYPE | Device type | disk, part, lvm, raid1 | | MOUNTPOINT | Where device is mounted | /, /boot, /home | Device Type Explanations Understanding device types is crucial for interpreting `lsblk` output: - disk: Physical storage device (HDD, SSD, USB drive) - part: Partition on a disk - lvm: Logical Volume Manager volume - raid: Software RAID array - crypt: Encrypted device (LUKS) - loop: Loop device (mounted file as block device) - rom: Read-only memory (CD/DVD drives) Tree Structure Interpretation The tree structure uses special characters to show relationships: - `├─`: Indicates a partition or logical volume that branches from parent device - `└─`: Shows the last partition or volume in a branch - Indentation levels indicate hierarchy depth Advanced lsblk Options Displaying All Devices By default, `lsblk` may not show empty or unused devices. To display all devices: ```bash lsblk -a ``` This option shows all devices, including empty ones that might not appear in the standard output. Including Filesystem Information To display filesystem types and labels: ```bash lsblk -f ``` Example output: ``` NAME FSTYPE LABEL UUID MOUNTPOINT sda ├─sda1 vfat EFI 1234-5678 /boot/efi ├─sda2 ext4 boot 12345678-1234-1234-1234-123456789012 /boot └─sda3 LVM2_member 87654321-4321-4321-4321-210987654321 └─ubuntu--vg-ubuntu--lv ext4 root 11111111-2222-3333-4444-555555555555 / ``` Displaying Permissions and Ownership To show device permissions and ownership: ```bash lsblk -m ``` This displays permission information for each device, which can be useful for troubleshooting access issues. Raw Output Mode For script-friendly output without tree formatting: ```bash lsblk -r ``` This produces output where each device appears on a separate line without tree characters, making it easier to parse in scripts. JSON Output Format For structured data processing: ```bash lsblk -J ``` This generates JSON-formatted output, perfect for integration with other tools or programming languages that can parse JSON. Example JSON output: ```json { "blockdevices": [ {"name": "sda", "maj:min": "8:0", "rm": "0", "size": "20G", "ro": "0", "type": "disk", "mountpoint": null, "children": [ {"name": "sda1", "maj:min": "8:1", "rm": "0", "size": "512M", "ro": "0", "type": "part", "mountpoint": "/boot/efi"}, {"name": "sda2", "maj:min": "8:2", "rm": "0", "size": "1G", "ro": "0", "type": "part", "mountpoint": "/boot"} ] } ] } ``` Formatting and Customization Custom Column Selection You can specify exactly which columns to display using the `-o` option: ```bash lsblk -o NAME,SIZE,TYPE,MOUNTPOINT ``` Available Columns `lsblk` supports numerous columns. To see all available options: ```bash lsblk --help ``` Common useful columns include: - KNAME: Kernel name - FSTYPE: Filesystem type - LABEL: Filesystem label - UUID: Filesystem UUID - MODEL: Device model - SERIAL: Device serial number - VENDOR: Device vendor - TRAN: Transport type (SATA, USB, etc.) - HOTPLUG: Hot-pluggable device indicator Advanced Column Example Display comprehensive device information: ```bash lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT,LABEL,UUID,MODEL ``` Setting Column Widths Control output formatting with specific column widths: ```bash lsblk -o +NAME:20,SIZE:10,TYPE:8,MOUNTPOINT:30 ``` The `+` prefix adds columns to the default set, while numbers after colons specify column widths. Practical Examples and Use Cases Example 1: System Storage Overview Get a complete overview of system storage: ```bash lsblk -f -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT,LABEL,AVAIL,USE% ``` This command provides filesystem information along with available space and usage percentages. Example 2: USB Drive Detection Identify newly connected USB drives: ```bash lsblk -d -o NAME,SIZE,TYPE,TRAN,VENDOR,MODEL | grep usb ``` The `-d` flag shows only top-level devices (no partitions), making it easier to identify physical USB devices. Example 3: RAID Array Status Check RAID array configuration: ```bash lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT | grep -E "(raid|md)" ``` This helps identify software RAID arrays and their current status. Example 4: LVM Volume Analysis Examine LVM (Logical Volume Manager) setup: ```bash lsblk -o NAME,SIZE,TYPE,MOUNTPOINT | grep -E "(lvm|LVM)" ``` Useful for understanding complex LVM configurations with multiple volume groups and logical volumes. Example 5: Encrypted Device Discovery Find encrypted (LUKS) devices: ```bash lsblk -f | grep crypto ``` This helps identify encrypted partitions and their unlock status. Example 6: Loop Device Investigation Examine loop devices (mounted files): ```bash lsblk | grep loop ``` Useful for identifying mounted ISO files, container storage, or other file-backed block devices. Filtering and Sorting Filtering by Device Type Display only specific device types: ```bash Show only disk devices (no partitions) lsblk -d Show only partitions lsblk | grep part ``` Filtering by Size Find devices within specific size ranges using additional tools: ```bash Devices larger than 10GB lsblk -b -o NAME,SIZE,TYPE | awk '$2 > 10737418240 {print $1, $2/1073741824 "GB", $3}' ``` Sorting Output While `lsblk` doesn't have built-in sorting options, you can combine it with `sort`: ```bash Sort by size (requires raw output) lsblk -r -o NAME,SIZE,TYPE | sort -k2 -h ``` The `-h` flag enables human-readable numeric sorting. Filtering by Mount Status Find unmounted devices: ```bash lsblk -o NAME,SIZE,TYPE,MOUNTPOINT | grep -E "part|disk" | grep -v "/" ``` This identifies partitions and disks that aren't currently mounted. Integration with Other Commands Combining with df Get comprehensive storage information: ```bash echo "=== Block Devices ===" && lsblk && echo -e "\n=== Disk Usage ===" && df -h ``` Using with findmnt Cross-reference mount information: ```bash Show device and detailed mount options lsblk -o NAME,MOUNTPOINT && echo "=== Mount Details ===" && findmnt ``` Integration with grep and awk Extract specific information: ```bash Get all mounted ext4 filesystems lsblk -f | grep ext4 | awk '{print $1, $7}' Find devices by vendor lsblk -d -o NAME,VENDOR,MODEL | grep -i "samsung" ``` Script Integration Example bash script using `lsblk`: ```bash #!/bin/bash Check for unmounted drives echo "Checking for unmounted storage devices..." unmounted=$(lsblk -rn -o NAME,TYPE,MOUNTPOINT | grep -E "disk|part" | awk '$3=="" {print $1}') if [ -n "$unmounted" ]; then echo "Found unmounted devices:" echo "$unmounted" else echo "All storage devices are mounted." fi ``` Troubleshooting Common Issues Issue 1: Command Not Found Problem: `bash: lsblk: command not found` Solution: ```bash Check if util-linux is installed which lsblk Install util-linux package sudo apt install util-linux # Ubuntu/Debian sudo yum install util-linux # RHEL/CentOS ``` Issue 2: Permission Denied Errors Problem: Cannot access certain device information Solution: ```bash Run with sudo for complete device information sudo lsblk -f Check device permissions ls -la /dev/sd* ``` Issue 3: Missing Device Information Problem: Expected devices don't appear in output Solutions: ```bash Show all devices including empty ones lsblk -a Force kernel to rescan devices sudo partprobe Check if device is recognized by kernel dmesg | tail -20 ``` Issue 4: Incorrect Size Display Problem: Device sizes appear wrong or in unexpected units Solutions: ```bash Display sizes in bytes for precision lsblk -b Use different size units lsblk --output=+SIZE-T # Display in multiple units ``` Issue 5: Tree Format Issues Problem: Tree structure appears corrupted or unclear Solutions: ```bash Use raw output for scripts lsblk -r Adjust terminal width export COLUMNS=120 && lsblk Use alternative formatting lsblk -P # Key-value pairs ``` Issue 6: Outdated Information Problem: `lsblk` shows outdated device information Solutions: ```bash Refresh device information sudo udevadm trigger sudo udevadm settle Force filesystem detection refresh sudo blkid -g ``` Best Practices and Tips Regular System Monitoring Create aliases for common `lsblk` commands: ```bash Add to ~/.bashrc or ~/.bash_aliases alias lsblk-full='lsblk -f -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT,LABEL,UUID' alias lsblk-simple='lsblk -o NAME,SIZE,TYPE,MOUNTPOINT' alias lsblk-usb='lsblk -d -o NAME,SIZE,VENDOR,MODEL,TRAN | grep usb' ``` Documentation and Logging Document your system's storage configuration: ```bash Create storage documentation echo "System Storage Configuration - $(date)" > storage_config.txt lsblk -f >> storage_config.txt echo -e "\n=== Detailed Device Info ===" >> storage_config.txt lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT,LABEL,UUID,MODEL >> storage_config.txt ``` Security Considerations - Be cautious when sharing `lsblk` output as it may reveal system configuration details - UUIDs and device serial numbers in output could be considered sensitive information - Use `lsblk -o NAME,SIZE,TYPE,MOUNTPOINT` for basic information sharing Performance Tips - Use specific device names when possible: `lsblk /dev/sda` instead of `lsblk` - For scripts, use raw output (`-r`) to avoid parsing tree characters - Cache output in variables for multiple operations: ```bash Store output for multiple uses DEVICES=$(lsblk -rn -o NAME,TYPE,SIZE) echo "$DEVICES" | grep disk echo "$DEVICES" | grep part ``` Automation and Monitoring Create monitoring scripts: ```bash #!/bin/bash Storage monitoring script Alert if any filesystem is over 90% full lsblk -o NAME,MOUNTPOINT,FSUSE% | awk '$3 > 90 {print "WARNING: " $2 " is " $3 " full"}' Check for new devices CURRENT_DEVICES=$(lsblk -d -n -o NAME | sort) if [ -f /tmp/previous_devices ]; then NEW_DEVICES=$(comm -13 /tmp/previous_devices <(echo "$CURRENT_DEVICES")) [ -n "$NEW_DEVICES" ] && echo "New devices detected: $NEW_DEVICES" fi echo "$CURRENT_DEVICES" > /tmp/previous_devices ``` Integration with System Administration Use `lsblk` in system administration workflows: ```bash Pre-maintenance storage check echo "=== Pre-Maintenance Storage Status ===" | tee maintenance.log lsblk -f | tee -a maintenance.log df -h | tee -a maintenance.log Backup verification echo "=== Backup Device Check ===" lsblk | grep -E "(backup|usb)" || echo "No backup devices found" ``` Conclusion The `lsblk` command is an indispensable tool for Linux system administrators, developers, and power users who need to understand and manage storage devices. This comprehensive guide has covered everything from basic usage to advanced formatting options, practical examples, and troubleshooting techniques. Key Takeaways 1. Versatility: `lsblk` provides multiple output formats suitable for both human reading and script processing 2. Safety: As a read-only command, `lsblk` is safe to use and won't modify your storage devices 3. Comprehensive Information: The command reveals relationships between physical devices, partitions, filesystems, and mount points 4. Customization: Extensive formatting options allow you to display exactly the information you need 5. Integration: `lsblk` works well with other Linux commands and can be easily incorporated into scripts and automation workflows Next Steps To further enhance your Linux storage management skills, consider exploring: - Advanced storage concepts: Learn about LVM, RAID, and filesystem management - Related commands: Master `fdisk`, `parted`, `blkid`, and `findmnt` for comprehensive storage administration - Automation: Create monitoring scripts and automated storage management workflows - Security: Understand encryption, secure mounting, and storage security best practices Final Recommendations - Practice using different `lsblk` options in a safe environment before using them in production - Create documentation templates using `lsblk` output for system inventory and maintenance - Combine `lsblk` with other system monitoring tools for comprehensive infrastructure management - Stay updated with the latest `util-linux` package versions for new features and improvements Remember that effective storage management is crucial for system reliability, performance, and data protection. The `lsblk` command provides the foundation for understanding your system's storage architecture, enabling you to make informed decisions about disk usage, partitioning, and storage optimization. By mastering `lsblk` and incorporating it into your regular system administration practices, you'll be better equipped to maintain robust, well-organized storage systems and quickly diagnose storage-related issues when they arise.