Disk and Partition Management on Linux: Complete Guide to fdisk, parted, and gdisk

Effective disk and partition management is a fundamental skill for every Linux system administrator. Whether you're setting up a new server, expanding storage capacity, or reorganizing your disk layout, understanding the core partitioning tools—fdisk, parted, and gdisk—is essential for maintaining robust and scalable storage infrastructure.

This comprehensive guide walks you through everything you need to know about Linux disk partitioning, from basic concepts to advanced techniques using the three primary partitioning utilities available on modern Linux systems.

Introduction to Disk Partitioning on Linux

Disk partitioning is the process of dividing a physical storage device into separate logical sections called partitions. Each partition functions as an independent disk, allowing you to organize data, install multiple operating systems, or implement different file systems on a single physical drive.

Linux supports multiple partition table formats, with the most common being:

  • MBR (Master Boot Record): Legacy partition table format supporting up to 4 primary partitions and disks up to 2TB
  • GPT (GUID Partition Table): Modern partition table format supporting up to 128 partitions and disks larger than 2TB

Understanding which tool to use for which partition table type is crucial:

  • fdisk: Traditional tool for MBR partitions, newer versions support GPT
  • parted: Versatile tool supporting both MBR and GPT with advanced features
  • gdisk: Specialized tool designed specifically for GPT partitions

Prerequisites

Before working with disk partitions, ensure you have:

  • Root or sudo access to your Linux system
  • Basic understanding of Linux command-line interface
  • Knowledge of your current disk configuration
  • Complete backup of all important data (partitioning operations can result in data loss)
  • Understanding of the difference between /dev/sda, /dev/nvme0n1, and other device naming conventions

Critical Safety Warning

DANGER: Incorrect partitioning operations can result in complete data loss. Always:

  1. Create full backups before modifying partitions
  2. Double-check device names before executing commands
  3. Verify partition tables before writing changes
  4. Unmount partitions before modifying them
  5. Test procedures on non-production systems first

Understanding Linux Disk Device Naming

Before diving into partitioning tools, familiarize yourself with Linux disk device naming conventions:

  • /dev/sda, /dev/sdb: SATA/SCSI drives (traditional naming)
  • /dev/nvme0n1, /dev/nvme1n1: NVMe solid-state drives
  • /dev/vda, /dev/vdb: Virtual disks in KVM/QEMU environments
  • /dev/xvda: Virtual disks in Xen environments

Partitions are numbered sequentially:

  • /dev/sda1: First partition on first SATA drive
  • /dev/nvme0n1p1: First partition on first NVMe drive
  • /dev/sda2: Second partition on first SATA drive

Listing Available Disks and Partitions

Before creating or modifying partitions, identify your available storage devices.

View All Block Devices

lsblk

This command displays all block devices in a tree format:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   100G  0 disk
├─sda1   8:1    0     1G  0 part /boot
├─sda2   8:2    0    10G  0 part [SWAP]
└─sda3   8:3    0    89G  0 part /
sdb      8:16   0   500G  0 disk

List Partition Tables

sudo fdisk -l

This provides detailed information about all disks and their partition tables.

Check Disk Information with parted

sudo parted -l

This command shows comprehensive partition information including partition table type.

Working with fdisk: MBR and GPT Partitioning

fdisk is the most widely used disk partitioning utility on Linux systems. While historically designed for MBR partitions, modern versions support GPT as well.

Starting fdisk Interactive Mode

sudo fdisk /dev/sdb

Replace /dev/sdb with your target disk device.

Essential fdisk Commands

Once in fdisk interactive mode:

  • m: Display help menu
  • p: Print current partition table
  • n: Create new partition
  • d: Delete partition
  • t: Change partition type
  • w: Write changes to disk
  • q: Quit without saving
  • g: Create new GPT partition table
  • o: Create new MBR partition table

Creating a New GPT Partition Table with fdisk

sudo fdisk /dev/sdb

Inside fdisk:

Command (m for help): g
Created a new GPT disklabel (GUID: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX).

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Creating a New Partition with fdisk

sudo fdisk /dev/sdb

Inside fdisk:

Command (m for help): n
Partition number (1-128, default 1): 1
First sector (2048-1048575999, default 2048): [Press Enter]
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-1048575999, default 1048575999): +100G

Created a new partition 1 of type 'Linux filesystem' and of size 100 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Changing Partition Type with fdisk

sudo fdisk /dev/sdb

Inside fdisk:

Command (m for help): t
Partition number (1-128, default 1): 1
Partition type or alias (type L to list all): 8e

Changed type of partition 'Linux filesystem' to 'Linux LVM'.

Command (m for help): w

Common partition type codes:

  • 83: Linux filesystem
  • 8e: Linux LVM
  • 82: Linux swap
  • fd: Linux RAID auto

Complete fdisk Example: Creating Multiple Partitions

sudo fdisk /dev/sdb

Interactive session:

Command (m for help): g
Created a new GPT disklabel.

Command (m for help): n
Partition number (1-128, default 1): [Enter]
First sector: [Enter]
Last sector: +50G
Created a new partition 1 of type 'Linux filesystem' and of size 50 GiB.

Command (m for help): n
Partition number (2-128, default 2): [Enter]
First sector: [Enter]
Last sector: +100G
Created a new partition 2 of type 'Linux filesystem' and of size 100 GiB.

Command (m for help): n
Partition number (3-128, default 3): [Enter]
First sector: [Enter]
Last sector: [Enter]
Created a new partition 3 of type 'Linux filesystem' and of size 350 GiB.

Command (m for help): p
Disk /dev/sdb: 500 GiB
Device       Start        End    Sectors  Size Type
/dev/sdb1     2048  104859647  104857600   50G Linux filesystem
/dev/sdb2 104859648  314574847  209715200  100G Linux filesystem
/dev/sdb3 314574848 1048575999  733999152  350G Linux filesystem

Command (m for help): w
The partition table has been altered.

Working with parted: Advanced Partition Management

parted is a powerful partitioning tool that supports both MBR and GPT partition tables with more advanced features than fdisk.

Viewing Partition Information with parted

sudo parted /dev/sdb print

Output:

Model: ATA Samsung SSD 860 (scsi)
Disk /dev/sdb: 537GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  53.7GB  53.7GB  ext4         primary
 2      53.7GB  161GB   107GB   ext4         primary
 3      161GB   537GB   376GB   ext4         primary

Creating GPT Partition Table with parted

sudo parted /dev/sdb mklabel gpt

For MBR partition table:

sudo parted /dev/sdb mklabel msdos

Creating Partitions with parted

parted supports both interactive and command-line modes.

Interactive Mode

sudo parted /dev/sdb

Inside parted:

(parted) mkpart primary ext4 0% 50GB
(parted) mkpart primary ext4 50GB 150GB
(parted) mkpart primary ext4 150GB 100%
(parted) print
(parted) quit

Command-Line Mode (Scriptable)

sudo parted /dev/sdb mkpart primary ext4 0% 50GB
sudo parted /dev/sdb mkpart primary ext4 50GB 150GB
sudo parted /dev/sdb mkpart primary ext4 150GB 100%

Resizing Partitions with parted

WARNING: Always backup data before resizing partitions.

sudo parted /dev/sdb

Inside parted:

(parted) resizepart 3 200GB
(parted) quit

This resizes partition 3 to end at 200GB. Note that this only modifies the partition table; you'll need to resize the filesystem separately.

Deleting Partitions with parted

sudo parted /dev/sdb rm 2

This deletes partition 2 from /dev/sdb.

Setting Partition Flags with parted

sudo parted /dev/sdb set 1 boot on
sudo parted /dev/sdb set 2 lvm on

Common flags:

  • boot: Bootable partition
  • lvm: LVM partition
  • raid: RAID partition
  • swap: Swap partition

Working with gdisk: GPT-Specific Tool

gdisk is specifically designed for GPT partition tables and offers features not available in fdisk or parted.

Starting gdisk Interactive Mode

sudo gdisk /dev/sdb

Essential gdisk Commands

  • p: Print partition table
  • n: Create new partition
  • d: Delete partition
  • t: Change partition type
  • w: Write changes and exit
  • q: Quit without saving
  • i: Show detailed partition information
  • x: Enter expert mode
  • ?: Show help

Creating GPT Partitions with gdisk

sudo gdisk /dev/sdb

Interactive session:

Command (? for help): n
Partition number (1-128, default 1): 1
First sector: [Press Enter]
Last sector: +100G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): [Press Enter]

Command (? for help): n
Partition number (2-128, default 2): 2
First sector: [Press Enter]
Last sector: +200G
Hex code or GUID: 8e00

Command (? for help): p
Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048       209717247   100.0 GiB   8300  Linux filesystem
   2       209717248       629147647   200.0 GiB   8E00  Linux LVM

Command (? for help): w
Do you want to proceed? (Y/N): Y

Common gdisk type codes:

  • 8300: Linux filesystem
  • 8200: Linux swap
  • 8e00: Linux LVM
  • fd00: Linux RAID
  • ef00: EFI System Partition

Converting MBR to GPT with gdisk

WARNING: Backup all data before conversion.

sudo gdisk /dev/sdb

gdisk automatically detects MBR and offers conversion:

Found invalid GPT and valid MBR; converting MBR to GPT format
in memory.

Command (? for help): w

Partition Alignment Optimization with gdisk

gdisk automatically aligns partitions to optimal boundaries for SSD performance. Verify alignment:

sudo gdisk /dev/sdb
Command (? for help): x
Expert command (? for help): l
Enter the sector alignment value (1-65536, default = 2048): [Press Enter]
Expert command (? for help): m
Command (? for help): w

Creating Filesystems on New Partitions

After creating partitions, you must create filesystems before using them.

Creating ext4 Filesystem

sudo mkfs.ext4 /dev/sdb1

With custom options:

sudo mkfs.ext4 -L data_partition -m 1 /dev/sdb1

Options:

  • -L: Set partition label
  • -m: Reserved blocks percentage (default 5%, reduce to 1% for data partitions)

Creating XFS Filesystem

sudo mkfs.xfs /dev/sdb2

With label:

sudo mkfs.xfs -L backup_partition /dev/sdb2

Creating ext3 Filesystem

sudo mkfs.ext3 /dev/sdb3

Creating Btrfs Filesystem

sudo mkfs.btrfs /dev/sdb4

Creating Swap Partition

sudo mkswap /dev/sdb5
sudo swapon /dev/sdb5

Verify swap:

sudo swapon --show

Mounting and Using New Partitions

Creating Mount Point

sudo mkdir -p /mnt/data

Mounting Partition Temporarily

sudo mount /dev/sdb1 /mnt/data

Verify:

df -h /mnt/data

Mounting with Specific Options

sudo mount -o defaults,noatime /dev/sdb1 /mnt/data

Unmounting Partition

sudo umount /mnt/data

If busy:

sudo fuser -m /mnt/data
sudo umount /mnt/data

Permanent Mounting with /etc/fstab

Edit /etc/fstab:

sudo nano /etc/fstab

Add entry:

/dev/sdb1  /mnt/data  ext4  defaults,noatime  0  2

Using UUID (recommended):

sudo blkid /dev/sdb1

Output:

/dev/sdb1: UUID="a1b2c3d4-e5f6-7890-abcd-ef1234567890" TYPE="ext4"

Add to /etc/fstab:

UUID=a1b2c3d4-e5f6-7890-abcd-ef1234567890  /mnt/data  ext4  defaults,noatime  0  2

Test fstab configuration:

sudo mount -a

Verification and Testing

Verify Partition Table

sudo fdisk -l /dev/sdb
sudo parted /dev/sdb print
sudo gdisk -l /dev/sdb

Check Filesystem

sudo fsck -n /dev/sdb1

Options:

  • -n: No changes, check only
  • -y: Automatically fix errors

Verify Mount Points

df -h
mount | grep sdb
lsblk -f

Test Read/Write Performance

sudo dd if=/dev/zero of=/mnt/data/testfile bs=1G count=1 oflag=direct
sudo rm /mnt/data/testfile

Check Partition Alignment

sudo parted /dev/sdb align-check optimal 1

Troubleshooting Common Issues

Issue: "Device or resource busy" When Unmounting

Solution:

# Find processes using the partition
sudo fuser -m /mnt/data

# Kill processes if necessary
sudo fuser -km /mnt/data

# Unmount
sudo umount /mnt/data

Or use lazy unmount:

sudo umount -l /mnt/data

Issue: Partition Not Detected After Creation

Solution:

Force kernel to re-read partition table:

sudo partprobe /dev/sdb

Or:

sudo blockdev --rereadpt /dev/sdb

Issue: "GPT PMBR size mismatch" Error

This occurs when disk was cloned or resized.

Solution:

sudo gdisk /dev/sdb
Command (? for help): x
Expert command (? for help): e
Expert command (? for help): w

Issue: Alignment Warnings in parted

Solution:

Recreate partition with proper alignment:

sudo parted /dev/sdb
(parted) rm 1
(parted) mkpart primary ext4 0% 100GB
(parted) align-check optimal 1

Issue: Cannot Write Partition Table

Solution:

Ensure partitions are unmounted:

sudo umount /dev/sdb1
sudo umount /dev/sdb2

Disable swap if partition is swap:

sudo swapoff /dev/sdb5

Issue: Partition Shows Wrong Size After Extending

Solution:

Resize the filesystem after extending partition:

For ext4:

sudo resize2fs /dev/sdb1

For XFS:

sudo xfs_growfs /mnt/data

Best Practices for Disk Partition Management

1. Always Use UUID in /etc/fstab

Device names can change across reboots; UUIDs remain constant:

# Find UUID
sudo blkid /dev/sdb1

# Use in fstab
UUID=xxx-xxx-xxx /mnt/data ext4 defaults 0 2

2. Align Partitions for SSD Performance

Modern partitioning tools automatically align to 1MB boundaries, optimal for SSDs:

# Verify alignment
sudo parted /dev/sdb align-check optimal 1

3. Leave Reserved Space on SSDs

Leave 10-20% unpartitioned space on SSDs for over-provisioning and longevity:

# Instead of using 100%, use 90%
sudo parted /dev/sdb mkpart primary ext4 0% 90%

4. Use GPT for Modern Systems

GPT offers advantages over MBR:

  • Supports disks larger than 2TB
  • Up to 128 partitions
  • Redundant partition tables
  • CRC32 checksums for integrity

5. Document Partition Layouts

Maintain documentation of partition purposes and configurations:

# Create documentation file
sudo parted -l > /root/partition_layout_$(date +%Y%m%d).txt
sudo lsblk -f >> /root/partition_layout_$(date +%Y%m%d).txt

6. Regular Filesystem Checks

Schedule regular filesystem checks:

# Set maximum mount count before check
sudo tune2fs -c 30 /dev/sdb1

# Set interval between checks
sudo tune2fs -i 90d /dev/sdb1

7. Use Appropriate Filesystem Types

Choose filesystems based on use case:

  • ext4: General purpose, widely compatible
  • XFS: Large files, high performance
  • Btrfs: Advanced features, snapshots
  • swap: Virtual memory extension

8. Monitor Disk Health

Regularly check disk health with SMART:

sudo smartctl -a /dev/sdb

9. Backup Before Partitioning

Always create complete backups before any partitioning operation:

# Backup partition table
sudo sfdisk -d /dev/sdb > sdb_partition_backup.txt

# Restore if needed
sudo sfdisk /dev/sdb < sdb_partition_backup.txt

10. Test in Non-Production Environments

Always test partition modifications on test systems before implementing in production.

Advanced Partitioning Scenarios

Creating Aligned Partitions for RAID Arrays

sudo parted /dev/sdb
(parted) mklabel gpt
(parted) mkpart primary ext4 1MiB 100%
(parted) align-check optimal 1
(parted) set 1 raid on
(parted) quit

Partitioning NVMe Drives

NVMe devices use different naming:

# List NVMe devices
lsblk | grep nvme

# Partition NVMe drive
sudo parted /dev/nvme0n1
(parted) mklabel gpt
(parted) mkpart primary ext4 0% 100%
(parted) quit

# Create filesystem
sudo mkfs.ext4 /dev/nvme0n1p1

Creating LVM Physical Volumes on Partitions

# Create partition with LVM type
sudo parted /dev/sdb mkpart primary 0% 100%
sudo parted /dev/sdb set 1 lvm on

# Initialize as LVM physical volume
sudo pvcreate /dev/sdb1

# Verify
sudo pvdisplay

Comparing fdisk, parted, and gdisk

When to Use fdisk

  • Simple partitioning tasks on MBR or GPT disks
  • Familiar with traditional fdisk interface
  • Scripting basic partition operations
  • Quick partition table viewing

Advantages:

  • Widely available on all Linux distributions
  • Simple, intuitive interface
  • Fast for basic operations

Disadvantages:

  • Limited advanced features
  • Less precise than parted for resizing

When to Use parted

  • Resizing partitions
  • Scripting complex partition operations
  • Working with large disks (> 2TB)
  • Need for precise partition placement

Advantages:

  • Supports both MBR and GPT
  • Can resize partitions
  • Excellent for scripting
  • Precise sector-level control

Disadvantages:

  • Immediate writes (changes take effect immediately)
  • Steeper learning curve
  • More verbose output

When to Use gdisk

  • Working exclusively with GPT partition tables
  • Converting MBR to GPT
  • Advanced GPT features needed
  • Partition alignment optimization

Advantages:

  • GPT-specific features
  • Automatic alignment optimization
  • Safe conversion from MBR to GPT
  • Detailed partition information

Disadvantages:

  • GPT only (no MBR support)
  • Less common than fdisk
  • Limited availability on minimal systems

Conclusion

Mastering disk and partition management with fdisk, parted, and gdisk is essential for effective Linux system administration. Each tool has its strengths: fdisk for simplicity and widespread availability, parted for advanced features and scripting, and gdisk for GPT-specific operations.

Key takeaways:

  1. Always backup data before any partitioning operation
  2. Use GPT for modern systems and large disks
  3. Verify alignment for optimal SSD performance
  4. Use UUIDs in /etc/fstab for reliability
  5. Choose the right tool for your specific needs
  6. Test changes in non-production environments first
  7. Document your partition layouts
  8. Monitor disk health regularly

By following the best practices and procedures outlined in this guide, you can confidently manage disk partitions on Linux systems while minimizing the risk of data loss and maximizing storage performance.

Remember that proper partition management is not just about technical proficiency—it's about implementing safe, reliable, and maintainable storage infrastructure that supports your organization's needs both now and in the future.