How to check disk partitions in Linux

How to Check Disk Partitions in Linux Understanding your system's disk partitions is fundamental to effective Linux system administration. Whether you're setting up a new server, troubleshooting storage issues, or planning system upgrades, knowing how to check disk partitions is an essential skill. This comprehensive guide will walk you through various methods to view, analyze, and understand disk partitions in Linux systems. Table of Contents 1. [Understanding Disk Partitions](#understanding-disk-partitions) 2. [Prerequisites and Preparation](#prerequisites-and-preparation) 3. [Method 1: Using lsblk Command](#method-1-using-lsblk-command) 4. [Method 2: Using fdisk Command](#method-2-using-fdisk-command) 5. [Method 3: Using parted Command](#method-3-using-parted-command) 6. [Method 4: Using df Command](#method-4-using-df-command) 7. [Method 5: Using cat /proc/partitions](#method-5-using-cat-procpartitions) 8. [Method 6: Using blkid Command](#method-6-using-blkid-command) 9. [Advanced Partition Analysis](#advanced-partition-analysis) 10. [Troubleshooting Common Issues](#troubleshooting-common-issues) 11. [Best Practices](#best-practices) 12. [Conclusion](#conclusion) Understanding Disk Partitions Before diving into the commands, it's crucial to understand what disk partitions are and why they matter. A disk partition is a logical division of a physical storage device that allows you to organize data, install multiple operating systems, or separate system files from user data. Types of Partition Tables Linux systems typically use one of two partition table formats: - MBR (Master Boot Record): Traditional format supporting up to 4 primary partitions - GPT (GUID Partition Table): Modern format supporting up to 128 partitions with larger disk support Common Partition Types - Primary partitions: Can be bootable and contain operating systems - Extended partitions: Container for logical partitions (MBR only) - Logical partitions: Created within extended partitions - Swap partitions: Used for virtual memory Prerequisites and Preparation Before checking disk partitions, ensure you have: - Access to a Linux terminal - Basic understanding of Linux command-line interface - Root or sudo privileges for some commands - Knowledge of your system's storage devices Most partition-checking commands are available by default on Linux distributions, but you may need to install additional packages: ```bash Ubuntu/Debian sudo apt update && sudo apt install util-linux parted CentOS/RHEL/Fedora sudo yum install util-linux parted or for newer versions sudo dnf install util-linux parted ``` Method 1: Using lsblk Command The `lsblk` (list block devices) command is one of the most user-friendly tools for viewing disk partitions. It displays block devices in a tree format, making it easy to understand the relationship between disks and their partitions. Basic lsblk Usage ```bash lsblk ``` Example Output: ``` NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 100G 0 disk ├─sda1 8:1 0 512M 0 part /boot/efi ├─sda2 8:2 0 1G 0 part /boot └─sda3 8:3 0 98.5G 0 part ├─ubuntu--vg-root 253:0 0 97.5G 0 lvm / └─ubuntu--vg-swap_1 253:1 0 980M 0 lvm [SWAP] sdb 8:16 0 50G 0 disk └─sdb1 8:17 0 50G 0 part /home ``` Understanding lsblk Output - NAME: Device name - MAJ:MIN: Major and minor device numbers - RM: Removable device (1 if removable) - SIZE: Device size - RO: Read-only device (1 if read-only) - TYPE: Device type (disk, part, lvm) - MOUNTPOINT: Where the partition is mounted Advanced lsblk Options ```bash Show filesystem information lsblk -f Show all devices including empty ones lsblk -a Show additional columns lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT,UUID Show specific device lsblk /dev/sda ``` Example with filesystem information: ```bash lsblk -f ``` ``` NAME FSTYPE LABEL UUID FSAVAIL FSUSE% MOUNTPOINT sda ├─sda1 vfat C12D-E156 505.8M 1% /boot/efi ├─sda2 ext4 a1b2c3d4-e5f6-7890-abcd-ef1234567890 854.3M 10% /boot └─sda3 LVM2_member xyz789-abc123-def456-ghi789-jkl012 ├─ubuntu--vg-root ext4 / a1b2c3d4-e5f6-7890-abcd-ef1234567890 85.2G 10% / └─ubuntu--vg-swap_1 swap b2c3d4e5-f6g7-8901-bcde-f23456789012 [SWAP] ``` Method 2: Using fdisk Command The `fdisk` command is a powerful disk partitioning utility that can also display detailed partition information. It's particularly useful for understanding partition types and disk layout. Basic fdisk Usage ```bash List all partitions on all disks sudo fdisk -l Show partitions for a specific disk sudo fdisk -l /dev/sda Interactive mode for a specific disk (be careful!) sudo fdisk /dev/sda ``` Example Output: ```bash sudo fdisk -l /dev/sda ``` ``` Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors Disk model: Virtual Disk Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: gpt Disk identifier: 12345678-1234-5678-9abc-def123456789 Device Start End Sectors Size Type /dev/sda1 2048 1050623 1048576 512M EFI System /dev/sda2 1050624 3147775 2097152 1G Linux filesystem /dev/sda3 3147776 209713151 206565376 98.5G Linux LVM ``` Understanding fdisk Output - Disk information: Shows total size, model, and sector information - Disklabel type: Indicates MBR (dos) or GPT partition table - Device columns: Start/end sectors, size, and partition type - Partition types: EFI System, Linux filesystem, Linux LVM, etc. Useful fdisk Options ```bash Show partition table in different formats sudo fdisk -l -o Device,Start,End,Sectors,Size,Type /dev/sda List partition types sudo fdisk -T dos # for MBR sudo fdisk -T gpt # for GPT ``` Method 3: Using parted Command The `parted` command provides more detailed partition information and supports both MBR and GPT partition tables effectively. Basic parted Usage ```bash List all block devices sudo parted -l Show partition table for specific disk sudo parted /dev/sda print Show detailed information sudo parted /dev/sda print free ``` Example Output: ```bash sudo parted /dev/sda print ``` ``` Model: ATA Virtual Disk (scsi) Disk /dev/sda: 107GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 538MB 537MB fat32 boot, esp 2 538MB 1612MB 1074MB ext4 3 1612MB 107GB 106GB lvm ``` Understanding parted Output - Model: Drive manufacturer and model - Disk size: Total capacity - Partition Table: MBR (msdos) or GPT - Number: Partition number - File system: Detected filesystem type - Flags: Special attributes (boot, esp, lvm, etc.) Advanced parted Usage ```bash Show free space sudo parted /dev/sda print free Unit options (MB, GB, TB, %) sudo parted /dev/sda unit GB print Show all information sudo parted /dev/sda print all ``` Method 4: Using df Command The `df` (disk free) command shows mounted filesystems and their usage statistics. While primarily used for checking disk space, it's excellent for seeing which partitions are currently active. Basic df Usage ```bash Show all mounted filesystems df -h Show filesystem types df -T Show inodes usage df -i ``` Example Output: ```bash df -h ``` ``` Filesystem Size Used Avail Use% Mounted on /dev/mapper/ubuntu--vg-root 96G 8.5G 83G 10% / /dev/sda2 976M 78M 832M 10% /boot /dev/sda1 511M 5.1M 506M 1% /boot/efi /dev/sdb1 49G 2.1G 45G 5% /home tmpfs 2.0G 0 2.0G 0% /dev/shm ``` Understanding df Output - Filesystem: Device path or logical volume - Size: Total partition size - Used: Space currently used - Avail: Available free space - Use%: Percentage of space used - Mounted on: Mount point directory Useful df Options ```bash Show specific filesystem type df -t ext4 Exclude specific filesystem types df -x tmpfs -x devtmpfs Show grand total df -h --total Show only local filesystems df -l ``` Method 5: Using cat /proc/partitions The `/proc/partitions` file contains kernel information about all recognized partitions. This method provides a quick overview of all partitions known to the system. ```bash cat /proc/partitions ``` Example Output: ``` major minor #blocks name 8 0 104857600 sda 8 1 524288 sda1 8 2 1048576 sda2 8 3 103284736 sda3 8 16 52428800 sdb 8 17 52427776 sdb1 253 0 101711872 dm-0 253 1 1003520 dm-1 ``` This output shows: - major/minor: Device numbers - #blocks: Size in 1KB blocks - name: Device name Method 6: Using blkid Command The `blkid` command displays block device attributes, including UUID, filesystem type, and labels. Basic blkid Usage ```bash Show all block devices sudo blkid Show specific device sudo blkid /dev/sda1 Show only UUIDs sudo blkid -s UUID Show only filesystem types sudo blkid -s TYPE ``` Example Output: ```bash sudo blkid ``` ``` /dev/sda1: UUID="C12D-E156" TYPE="vfat" PARTUUID="12345678-01" /dev/sda2: UUID="a1b2c3d4-e5f6-7890-abcd-ef1234567890" TYPE="ext4" PARTUUID="12345678-02" /dev/sda3: UUID="xyz789-abc123-def456-ghi789-jkl012" TYPE="LVM2_member" PARTUUID="12345678-03" /dev/mapper/ubuntu--vg-root: UUID="b2c3d4e5-f6g7-8901-bcde-f23456789012" TYPE="ext4" /dev/mapper/ubuntu--vg-swap_1: UUID="c3d4e5f6-g7h8-9012-cdef-345678901234" TYPE="swap" ``` Advanced Partition Analysis Checking Partition Alignment Proper partition alignment is crucial for optimal performance, especially with SSDs: ```bash Check partition alignment sudo parted /dev/sda align-check optimal 1 Show detailed alignment information sudo fdisk -l /dev/sda | grep -E "(Start|Units|Sector)" ``` Analyzing LVM Partitions For systems using LVM (Logical Volume Manager): ```bash Show physical volumes sudo pvs Show volume groups sudo vgs Show logical volumes sudo lvs Detailed LVM information sudo pvdisplay sudo vgdisplay sudo lvdisplay ``` Checking RAID Arrays For systems with software RAID: ```bash Show RAID status cat /proc/mdstat Detailed RAID information sudo mdadm --detail /dev/md0 ``` Troubleshooting Common Issues Partition Not Showing Up If a partition isn't visible: 1. Rescan partition table: ```bash sudo partprobe /dev/sda ``` 2. Check if the disk is recognized: ```bash ls -la /dev/sd* dmesg | grep -i "sd[a-z]" ``` 3. Verify disk health: ```bash sudo smartctl -a /dev/sda ``` Permission Denied Errors If you encounter permission errors: ```bash Use sudo for system commands sudo fdisk -l sudo parted -l Check if you're in the disk group groups $USER ``` Inconsistent Partition Information If different commands show conflicting information: 1. Update partition table: ```bash sudo partprobe ``` 2. Reread partition table: ```bash sudo blockdev --rereadpt /dev/sda ``` 3. Check for filesystem errors: ```bash sudo fsck -n /dev/sda1 # Read-only check ``` Missing Filesystem Information If filesystem type isn't detected: ```bash Force filesystem detection sudo file -s /dev/sda1 Check for filesystem signature sudo blkid -p /dev/sda1 Manual filesystem check sudo fsck -N /dev/sda1 ``` Best Practices Safety Guidelines 1. Always backup data before modifying partitions 2. Use read-only commands when just checking information 3. Verify device names before running commands 4. Test commands on non-production systems first Regular Monitoring Create scripts to monitor partition usage: ```bash #!/bin/bash partition_monitor.sh echo "=== Disk Usage Report ===" df -h | grep -E "(Filesystem|/dev/)" echo -e "\n=== Partition Information ===" lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT echo -e "\n=== High Usage Partitions ===" df -h | awk 'NR==1 || /^\/dev/ && $5+0 > 80 { print $0 }' echo -e "\n=== System Partition Health ===" for device in $(lsblk -rno NAME,TYPE | awk '$2=="part"{print "/dev/"$1}'); do echo "Checking $device..." sudo tune2fs -l $device 2>/dev/null | grep -E "(Mount count|Maximum mount count|Check interval)" || echo "Not an ext filesystem" done ``` Documentation Standards Maintain proper documentation: 1. Document partition layouts in system documentation 2. Keep track of partition purposes and mount points 3. Record filesystem types and special configurations 4. Note any custom partition schemes or RAID configurations Performance Optimization Consider these performance factors: ```bash Check I/O scheduler for each device cat /sys/block/sda/queue/scheduler Monitor I/O statistics iostat -x 1 5 Check partition alignment (especially important for SSDs) sudo parted /dev/sda align-check optimal 1 sudo parted /dev/sda align-check minimal 1 ``` Security Considerations Implement security best practices: 1. Encrypt sensitive partitions using LUKS 2. Set proper permissions on mount points 3. Use separate partitions for different system components 4. Monitor disk usage to prevent denial-of-service attacks ```bash Check for encrypted partitions lsblk -f | grep -i crypt Verify mount options mount | grep -E "(nosuid|noexec|nodev)" Check filesystem permissions ls -la / | grep -E "(tmp|var|home)" ``` Automation and Scripting Create automated partition checking routines: ```bash #!/bin/bash comprehensive_disk_check.sh LOG_FILE="/var/log/disk_check.log" DATE=$(date '+%Y-%m-%d %H:%M:%S') { echo "[$DATE] Starting comprehensive disk check" echo "=== Block Device Tree ===" lsblk -f echo -e "\n=== Partition Tables ===" for disk in $(lsblk -rno NAME,TYPE | awk '$2=="disk"{print "/dev/"$1}'); do echo "Disk: $disk" sudo fdisk -l $disk 2>/dev/null || echo "Cannot read partition table" echo "" done echo -e "\n=== Mount Information ===" df -hT echo -e "\n=== UUID Information ===" sudo blkid | sort echo -e "\n=== Potential Issues ===" # Check for high usage df -h | awk 'NR>1 && $5+0 > 90 { print "WARNING: " $0 " is over 90% full" }' # Check for unmounted partitions comm -23 <(lsblk -rno NAME,TYPE | awk '$2=="part"{print "/dev/"$1}' | sort) <(df | awk 'NR>1{print $1}' | sort) | while read partition; do echo "INFO: Partition $partition is not mounted" done echo "[$DATE] Disk check completed" } >> "$LOG_FILE" echo "Disk check completed. Results logged to $LOG_FILE" ``` Common Command Combinations Useful command combinations for comprehensive partition analysis: ```bash Complete system overview { echo "=== System Overview ===" lsblk -f echo -e "\n=== Disk Usage ===" df -hT echo -e "\n=== Block Device IDs ===" sudo blkid } > system_partition_report.txt Quick health check alias diskcheck='df -h && echo "" && lsblk && echo "" && sudo fdisk -l' Find largest files on full partitions function find_large_files() { local partition=${1:-/} echo "Finding largest files in $partition..." sudo find $partition -type f -size +100M -exec ls -lh {} + 2>/dev/null | sort -k5 -hr | head -20 } ``` Conclusion Understanding how to check disk partitions in Linux is a fundamental skill for system administrators, developers, and power users. This comprehensive guide has covered multiple methods for viewing and analyzing partition information, each with its own strengths and use cases. Key Takeaways - Use `lsblk` for quick, tree-formatted partition overviews - Use `fdisk` for detailed partition table information and low-level details - Use `parted` for comprehensive partition analysis with GPT support - Use `df` to monitor mounted filesystems and disk usage - Use `/proc/partitions` for kernel-level partition information - Use `blkid` to identify filesystem types, UUIDs, and labels Choosing the Right Tool Different scenarios call for different tools: - System monitoring: `df -h` and `lsblk` - Partition planning: `parted -l` and `fdisk -l` - Troubleshooting: Combination of `lsblk -f`, `blkid`, and `dmesg` - Scripting: `/proc/partitions` and `lsblk` with specific output formats - Performance analysis: `iostat` combined with partition information Future Learning To deepen your knowledge of Linux storage management, consider exploring: - LVM (Logical Volume Manager) for advanced volume management - RAID configuration for redundancy and performance - Filesystem tuning and optimization techniques - Storage security with encryption and access controls - Container storage with Docker and Kubernetes persistent volumes Remember that storage management is a critical aspect of system administration that directly impacts performance, reliability, and data safety. Always approach partition-related tasks with caution, maintain regular backups, and thoroughly test procedures in non-production environments before implementing them on critical systems. Regular monitoring and maintenance of your partition layout will help ensure optimal system performance and prevent storage-related issues before they become critical problems. By mastering these partition checking techniques, you'll be well-equipped to handle the storage management challenges that come with modern Linux system administration.