Website Vidya
Linux & ServersStep-by-Step Guide

fdisk Command in Linux: Create & Manage Disk Partitions

Learn fdisk command in linux with syntax, essential flags, practical terminal examples, real-world sysadmin workflows, security tips, and troubleshooting.

RR
Rajesh RayLead Systems Educator
Published:
10 min read
PDF Cheat Sheet
fdisk Command in Linux: Create & Manage Disk Partitions
AI Executive SummaryWebsiteVidya AI

10 min read

Learn fdisk command in linux with syntax, essential flags, practical terminal examples, real-world sysadmin workflows, security tips, and troubleshooting.

Key Highlights
  • What fdisk Does and When to Use It
  • Availability and Installation Across Linux Distributions
  • Command Syntax, Arguments, and Exit Status Codes
Grounded in full engineering runbookWebsiteVidya AI Engine

Storage initialization and volume management on bare-metal and virtualized servers begin with the fdisk command in linux, the foundational partitioning utility provided by the util-linux package. As an interactive, dialog-driven partition table editor, fdisk allows system administrators to slice raw physical disks, NVMe drives, and SAN LUNs into manageable storage blocks before filesystems or logical volume managers are applied.

While historically limited to legacy Master Boot Record (MBR) partition tables, modern iterations of util-linux fdisk seamlessly handle GUID Partition Tables (GPT) as well as Sun, SGI, and BSD disk labels. Partitioning establishes hard physical boundaries on disk media, isolating operating system boot sectors, swap memory areas, and persistent application data volumes from catastrophic cross-filesystem corruption.

In this comprehensive guide, we examine block device geometry and partition table architecture, analyze MBR versus GPT trade-offs, detail sector alignment rules for modern 4Kn/512e SSDs, walk through interactive creation and deletion workflows, and review essential safeguards for avoiding data loss.

Advertisement
fdisk Command in Linux: Create & Manage Disk Partitions — syntax, flags, and practical terminal workflow diagram
fdisk Command in Linux: Create & Manage Disk Partitions — command syntax, key options, and production workflow reference.

What fdisk Does and When to Use It

At the block device layer, the Linux kernel accesses physical storage via block device special files located in /dev (such as SCSI/SATA drives /dev/sda, /dev/sdb and NVMe drives /dev/nvme0n1). The partition table resides at Logical Block Address zero (LBA 0). Under legacy MBR, LBA 0 hosts the 512-byte Master Boot Record containing 446 bytes of bootstrap bootloader code, four 16-byte partition record entries, and a 2-byte boot signature (0x55AA), capping addressable storage at 2 Terabytes due to 32-bit sector addressing.

In modern UEFI systems, GPT replaces MBR by placing a Protective MBR at LBA 0 to shield the drive from legacy utilities, followed by a Primary GPT Header at LBA 1 and 128 partition array entries spanning LBAs 2 through 33. GPT uses 64-bit Logical Block Addressing (supporting disks up to 9.4 Zettabytes) and stores unique 128-bit GUIDs alongside CRC32 header checksums. When changes are saved with the w command, fdisk issues the BLKRRPART ioctl to the kernel driver, notifying the operating system to reread partition tables without requiring a system reboot.

Under the hood, fdisk gathers runtime telemetry or dispatches control signals by reading virtual filesystems mounted at /proc and /sys, or by communicating with PID 1 (systemd) over D-Bus. Using flags such as -l, -s <partition>, -u[=<unit>], -b <size> turns raw process and service tables into actionable diagnostics during incident response.

Availability and Installation Across Linux Distributions

The fdisk utility is packaged in util-linux across mainstream Linux distributions. It is pre-installed on standard Ubuntu, Debian, RHEL, Rocky Linux, AlmaLinux, Fedora, and Arch Linux systems, while minimal container images may require manual installation:

Advertisement

Before running administrative tasks or automation scripts, verify whether fdisk is installed and inspect which binary path and version your shell resolves:

type -a fdisk
fdisk --version 2>/dev/null || man -f fdisk

# Illustrative output (reviewed against documentation)
fdisk is /usr/bin/fdisk
fdisk (util-linux)
# Debian / Ubuntu
sudo apt-get update && sudo apt-get install -y util-linux

# RHEL / Rocky / AlmaLinux / Fedora
sudo dnf install -y util-linux

# Arch Linux
sudo pacman -S --noconfirm util-linux

# Alpine Linux
sudo apk add --no-cache util-linux

Command Syntax, Arguments, and Exit Status Codes

fdisk [options] <device>

The invocation syntax is fdisk [options] <device>. Non-interactive listing operations accept -l or --list, which prints partition tables across all attached storage controllers (e.g., sudo fdisk -l) or inspects a specific block device (sudo fdisk -l /dev/nvme0n1). Passing sector size flags (-b 4096) or units (-u) adjusts how partition geometries and sector counts are displayed on screen.

When targeting a specific block device without listing flags (sudo fdisk /dev/sdb), fdisk opens an interactive prompt (Command (m for help):). The interactive environment operates entirely in volatile RAM until committed; commands include p (print table), n (add new partition), d (delete partition), t (change partition type ID), v (verify table integrity), and q (quit without saving). Only typing w writes the modifications to disk and reloads the kernel partition map.

  • 0 — Successful execution; partition table was read or written without error.
  • 1 — Operational failure, such as invalid partition boundaries or permission denied.
  • 2 — Device is busy, locked by another process, or BLKRRPART ioctl failed.
  • 130 — Interactive session terminated by user interrupt (SIGINT, Ctrl+C).

Important Options and Flags Reference

Option / FlagLong Form / ArgumentTechnical Behavior and Purpose
-l--listLists partition tables for all detected block devices or a specified device.
-s <partition>--size=<partition>Displays the size of a specific partition in 1024-byte blocks.
-u[=<unit>]--units[=<unit>]Sets display units to sectors (default) or cylinders (legacy compatibility).
-b <size>--sectorsize=<size>Specifies physical sector size (e.g., 512, 1024, 2048, or 4096 bytes).
-w <mode>--wipe=<mode>Wipes filesystem, RAID, or partition table signatures (auto, never, always).
-W <mode>--wipe-partitions=<mode>Wipes filesystem signatures from newly created partitions (auto, never, always).
-C <cylinders>--cylinders=<cylinders>Manually specifies cylinder count for legacy disk geometry testing.
-c[=<mode>]--compatibility[=<mode>]Toggles DOS-compatible mode (dos or nondos).
-v--versionDisplays the version information of the fdisk utility and exits.

In modern storage management, sector alignment is paramount. By default, util-linux fdisk enforces 2048-sector alignment (1 MiB boundary). Aligning partitions to 1 MiB boundaries ensures that logical filesystem blocks align perfectly with physical 4KB Advanced Format (4Kn/512e) sectors and underlying NAND flash erase blocks on SSDs, preventing 50% write amplification penalties.

Advertisement

When provisioning enterprise SAN storage or cloud NVMe volumes, inspect partition layouts with sudo fdisk -l to confirm the Start sector is evenly divisible by 2048. For non-interactive automation in bash pipelines, util-linux provides sfdisk, which accepts scriptable JSON and text templates.

When combining short flags on the command line, POSIX and GNU utilities allow clustering boolean flags behind a single hyphen, provided any option taking an argument appears last. In automated shell scripts, using long-form GNU options where portable makes self-documenting code that is easier to audit during peer review.

Practical Basic and Advanced Usage Examples

1. List All Block Devices and Existing Partition Tables

View an inventory of all attached storage controllers, physical drives, partition layouts, and total sector counts.

sudo fdisk -l

# Illustrative output (reviewed against documentation)
Disk /dev/sda: 500 GiB, 536870912000 bytes, 1048576000 sectors
Disk model: Crucial CT500MX500
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 7F23B812-4C10-4822-8A90-55E8D2E490A1

Device        Start        End   Sectors  Size Type
/dev/sda1      2048    1050623   1048576  512M EFI System
/dev/sda2   1050624 1048573951 1047523328  499.5G Linux filesystem

The command displays device geometry, logical and physical sector sizes, disklabel type (GPT), and existing partition boundaries aligned to sector 2048.

2. Inspect Partition Layout on a Specific NVMe Drive

Restrict partition inspection to a specific drive identifier without scanning all system storage.

sudo fdisk -l /dev/nvme0n1

# Illustrative output (reviewed against documentation)
Disk /dev/nvme0n1: 1 TiB, 1024209543168 bytes, 2000409264 sectors
Disklabel type: gpt
Device           Start        End    Sectors  Size Type
/dev/nvme0n1p1    2048    2099199    2097152    1G EFI System
/dev/nvme0n1p2 2099200 2000408575 1998309376 952.9G Linux filesystem

Targeting /dev/nvme0n1 isolates the NVMe controller and lists partitions following the nvme0n1pX naming convention.

3. Create a New Primary or GPT Partition Interactively

Launch fdisk on an unpartitioned secondary disk (/dev/sdb) and create a new 100GB data partition.

sudo fdisk /dev/sdb
# In interactive prompt:
# n (new partition)
# Partition number: 1 (default)
# First sector: 2048 (default)
# Last sector: +100G
# w (write to disk)

# Illustrative output (reviewed against documentation)
Command (m for help): n
Partition number (1-128, default 1): 1
First sector (2048-209715166, default 2048): 2048
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-209715166, default 209715166): +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.

Entering n creates a new partition, specifying +100G calculates the ending sector automatically, and typing w writes the partition table and syncs disks.

4. Change Partition Type ID to Linux LVM

Update partition type metadata to declare the partition as a Physical Volume for Logical Volume Management.

sudo fdisk /dev/sdb
# In interactive prompt:
# t (change type)
# Partition number: 1
# Partition type: 31 (Linux LVM on GPT, or 8e on MBR)
# w (save changes)

# Illustrative output (reviewed against documentation)
Command (m for help): t
Partition number (1, default 1): 1
Partition type or alias (type L to list all): 31
Changed type of partition 'Linux filesystem' to 'Linux LVM'.

Command (m for help): w

Changing partition type to Linux LVM signals volume managers and automated installers that the partition is reserved for LVM volume groups.

5. Delete an Existing Partition Safely

Remove an obsolete or incorrectly configured partition from a disk partition table.

sudo fdisk /dev/sdb
# In interactive prompt:
# p (print existing)
# d (delete partition)
# Partition number: 1
# w (write changes)

# Illustrative output (reviewed against documentation)
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

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

The d command clears the partition entry in memory, and w commits the updated table to disk, releasing the sectors for reallocation.

6. Reload Kernel Partition Table Without Rebooting

If the kernel was unable to immediately reread the partition table due to open files, use partprobe to refresh.

sudo partprobe /dev/sdb && lsblk /dev/sdb

# Illustrative output (reviewed against documentation)
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sdb      8:16   0  100G  0 disk 
└─sdb1   8:17   0  100G  0 part

The partprobe utility informs the operating system kernel of partition table changes, making newly created partitions immediately available for formatting.

Real-World Workflows, Pipelines, and Shell Automation

End-to-End Disk Provisioning: Partitioning, Formatting, and Persistent Mount

Complete full storage setup from raw block device to persistent /etc/fstab mount point on production Linux.

# 1. Partition disk with fdisk
echo -e "g\nn\n1\n2048\n\nw" | sudo fdisk /dev/sdb

# 2. Format with ext4 filesystem
sudo mkfs.ext4 -L "data_volume" /dev/sdb1

# 3. Create mount point and configure /etc/fstab
sudo mkdir -p /mnt/data
UUID=$(sudo blkid -s UUID -o value /dev/sdb1)
echo "UUID=$UUID /mnt/data ext4 defaults,noatime 0 2" | sudo tee -a /etc/fstab
sudo mount -a
df -h /mnt/data

# Illustrative output (reviewed against documentation)
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1        98G   24K   93G   1% /mnt/data

Piping commands into fdisk initializes a GPT label and partition, mkfs.ext4 creates the filesystem, and referencing the persistent UUID in /etc/fstab prevents boot failures.

Scripted Non-Interactive Partitioning Pipeline Using sfdisk

In automated provisioning scripts, use sfdisk (the scriptable sister utility in util-linux) to deploy identical partition layouts.

#!/usr/bin/env bash
set -euo pipefail

TARGET="/dev/sdc"
echo "[STORAGE] Partitioning $TARGET with automated sfdisk layout..."
sfdisk "$TARGET" <<EOF
label: gpt
size=1GiB, type=U
size=100GiB, type=L
type=L
EOF
lsblk "$TARGET"

# Illustrative output (reviewed against documentation)
[STORAGE] Partitioning /dev/sdc with automated sfdisk layout...
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sdc      8:32   0  500G  0 disk 
├─sdc1   8:33   0    1G  0 part 
├─sdc2   8:34   0  100G  0 part 
└─sdc3   8:35   0  399G  0 part

The sfdisk tool accepts human-readable partition definitions via standard input, eliminating fragile interactive fdisk expect scripts in automation.

Pipelines, Redirection, Quoting, and Shell Expansion

When embedding fdisk inside shell pipelines (|), conditional chains (&& and ||), or command substitutions ($(...)), the calling shell performs tilde expansion, variable interpolation, and globbing before invoking the binary. Always wrap paths, patterns, and variables in quotes so whitespace and wildcard characters (*, ?) are not expanded prematurely, and use set -euo pipefail in unattended scripts.

Permissions, Security Implications, and Safe Execution

Direct manipulation of raw block devices requires full root privileges or CAP_SYS_ADMIN capabilities. Unprivileged users cannot read or write /dev/sd* or /dev/nvme* nodes because arbitrary block access allows bypassing all filesystem discretionary access control (DAC) permissions, reading unencrypted passwords from disk blocks, and compromising kernel integrity.

Before decommissioning or repurposing storage drives, simply deleting partitions in fdisk does not erase the underlying data blocks—it merely removes the table index entries. To prevent sensitive enterprise data leakage, drives must be sanitized using cryptographic disk wiping utilities (such as blkdiscard on SSDs, shred, or cryptsetup luksErase) before repartitioning.

GNU/Linux, BSD, and BusyBox Implementation Differences

Modern util-linux fdisk on Linux supports both MBR and GPT natively. However, on BSD operating systems (FreeBSD, OpenBSD), disk partitioning is traditionally managed using disklabel, bsdlabel, or gpart rather than fdisk.

On BusyBox-based systems (such as Alpine Linux), the fdisk applet is lightweight and supports basic MBR partitioning, but lacks advanced GPT GUID management, partition signature wiping (-W), and automated alignment helpers. For production partitioning on Alpine, install the util-linux package (apk add util-linux).

Troubleshooting Common Errors and Diagnostics

Re-reading the partition table failed: Device or resource busy

Root Cause: One or more partitions on the block device are currently mounted, in use by LVM, or locked by an active swap partition.

Resolution: Unmount all partitions with umount or deactivate volume groups before repartitioning, or run partprobe to force the kernel to reload.

sudo umount /dev/sdb1
sudo partprobe /dev/sdb

Partition 1 does not start on physical sector boundary

Root Cause: The starting sector is not evenly divisible by the physical sector size (typically 4096 bytes or 8 sectors), causing misaligned I/O.

Resolution: Delete the partition and recreate it accepting the default starting sector of 2048 (1 MiB alignment).

# In fdisk, specify first sector: 2048

GPT PMBR size mismatch will be corrected by write

Root Cause: A virtual disk or cloud EBS/persistent disk volume was resized in the cloud console, so the backup GPT table at the end of the disk is displaced.

Resolution: Open the disk in fdisk and press 'w' to save, or use parted /dev/sda print (which prompts to fix the backup header).

sudo fdisk /dev/sda
# Type 'w' and press Enter

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

4 FAQs
Master Boot Record (MBR) is the legacy partition scheme that supports a maximum disk size of 2TB and up to 4 primary partitions. GUID Partition Table (GPT) is the modern standard for UEFI systems, supporting disk sizes up to 9.4 Zettabytes, up to 128 partitions by default, and redundant backup partition tables at the end of the disk with CRC32 integrity checking.
Sector 2048 equals exactly 1 Megabyte (2048 sectors * 512 bytes = 1,048,576 bytes). Aligning partitions to 1 MiB boundaries ensures that logical filesystem blocks align with physical 4KB Advanced Format disk sectors and SSD NAND flash erase blocks, preventing performance degradation.
Deleting a partition removes its entry from the partition table index, making the filesystem inaccessible to the OS. However, the actual data blocks remain on disk until overwritten. Data recovery tools (like testdisk) can often restore deleted partitions if no new data has been written over them.
Type 'q' (quit) and press Enter. The fdisk utility keeps all modifications in RAM until you explicitly type 'w' (write). Pressing 'q' discards all changes and leaves the on-disk partition table untouched.
RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout