fdisk Command: Tutorial & Examples

Manage partitions on hard drives and SSDs using an interactive command-line tool

The fdisk command is a powerful Linux utility for creating, modifying, deleting, and managing disk partitions on physical or virtual storage devices. Partitions divide a disk into separate sections that can hold filesystems, swap space, or other data. Proper partitioning is essential for organizing storage, installing operating systems, and optimizing server performance on Linux servers.

This article explains how fdisk works, its common usage patterns, practical examples, and advanced techniques to help you safely manage partitions. You will also find information about potential pitfalls, troubleshooting tips, security considerations, and alternatives for more complex partitioning needs.

How fdisk Works

The fdisk command interacts directly with a disk’s partition table — a special data structure stored on the disk that defines how the disk is divided into partitions. Traditionally, fdisk supports the MBR (Master Boot Record) partition scheme, which allows up to four primary partitions or three primary partitions plus one extended partition containing multiple logical partitions.

Modern disks often use the GPT (GUID Partition Table) scheme, which supports a much larger number of partitions and includes redundancy and CRC checks for integrity. Recent versions of fdisk (util-linux 2.23 and later) have added GPT support, but for advanced GPT management, tools like gdisk or parted are usually preferred.

Linux represents disks as device files under /dev, such as /dev/sda for the first SATA disk, /dev/nvme0n1 for an NVMe disk, or /dev/loop0 for a loopback device. Since modifying partition tables affects the device at a low level, you must run fdisk with root privileges (e.g., using sudo).

Command History and Evolution

fdisk has been a standard disk partitioning tool since the early days of Unix and Linux. Originally designed for MBR partition tables, its functionality has expanded over time to include GPT support as modern storage devices adopted this standard. Despite its age, fdisk remains widely used due to its availability on almost all Linux distributions and its straightforward interactive interface.

For GPT-only environments, newer tools like gdisk were developed to offer a more specialized interface with GPT-specific features, but fdisk still supports basic GPT partitioning in recent versions.

Common Command Line Parameters

fdisk can run in two modes: interactive and command-line. Some important options include:

1. -l : List the partitions on the specified disk(s).

    sudo fdisk -l /dev/sda

This displays the partition layout, sizes, types, and other details.

2. -u : Display units (sectors or cylinders). The default is sectors.

3. -s : Print the size of a partition in sectors.

4. -v : Show version information of the fdisk utility.

When run without parameters on a disk device, fdisk enters an interactive prompt where you can issue commands to manipulate partitions.

Interactive Mode Usage

Running

sudo fdisk /dev/sda

opens an interactive prompt with the following common commands:

1. m — Show help and list all available commands.

2. p — Print the current partition table.

3. n — Create a new partition. You will be prompted to choose partition type (primary p or extended e), partition number, first sector, and last sector or size.

4. d — Delete a partition by specifying its number.

5. t — Change the partition type code (e.g., to Linux swap, NTFS).

6. a — Toggle the bootable (active) flag on a partition.

7. w — Write the partition table to disk and exit. This makes changes permanent.

8. q — Quit without saving any changes made during the session.

It is important to note that changes are only applied after using the w command. Quitting without saving (q) discards all changes made in the current session.

Practical Examples Using fdisk

Example 1: List Partitions on a Disk

sudo fdisk -l /dev/sda

Sample output:

Disk /dev/sda: 500 GiB, 536870912000 bytes, 1048576000 sectors
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: dos
Disk identifier: 0x12345678

Device     Boot  Start       End   Sectors  Size Id Type
/dev/sda1  *      2048   1050623   1048576  512M 83 Linux
/dev/sda2       1050624 1048575999 1047525376 499G  5 Extended
/dev/sda5       1052672 1048575999 1047523328 499G 8e Linux LVM

This output shows the disk size, sector size, partition type (MBR with "dos" label in this case), and the partitions with their start/end sectors, size, and type IDs.

Example 2: Create a New Primary Partition

  1. Run:

    sudo fdisk /dev/sda
    
  2. At the fdisk prompt, type:

    n
    
  3. Choose primary partition type:

    p
    
  4. Enter partition number (e.g., 3):

    3
    
  5. Press Enter to accept the default first sector.

  6. Specify the last sector or size (e.g., +10G for 10 GiB):

    +10G
    
  7. Use p to print the partition table and verify the new partition.

  8. Use w to write the changes and exit.

  9. After writing, inform the kernel of the partition table changes:

    sudo partprobe /dev/sda
    

Alternatively, reboot the system to reload partition tables.

Example 3: Delete a Partition

  1. Run:

    sudo fdisk /dev/sda
    
  2. At the prompt, enter:

    d
    
  3. Specify the partition number to delete (e.g., 3).

  4. Use w to save changes and exit.

Example 4: Change Partition Type

  1. Run:

    sudo fdisk /dev/sda
    
  2. Enter t to change a partition type.

  3. Specify the partition number.

  4. Enter the hex code for the new partition type (e.g., 82 for Linux swap).

  5. Use p to verify changes.

  6. Use w to write changes.

Example 5: Toggle Bootable Flag

  1. Run:

    sudo fdisk /dev/sda
    
  2. Enter a to toggle the bootable flag.

  3. Specify the partition number.

  4. Use p to verify.

  5. Use w to save changes.

Advanced Usage

fdisk commands can be scripted by passing input via standard input. For example, to create a new primary partition non-interactively, you can use a here-document:

sudo fdisk /dev/sda <<EOF
n
p
3

+10G
w
EOF

This sequence creates a primary partition number 3 of size 10 GiB and writes the changes.

fdisk can also be used on virtual disk images:

sudo fdisk /path/to/disk-image.img

This allows partition manipulation on disk image files used by virtual machines or containers.

Potential Problems and Pitfalls

  • Data Loss Risk: Modifying partitions can cause data loss if done incorrectly. Always back up important data before using fdisk to avoid data corruption.

  • Mounted Partitions: Avoid deleting or modifying partitions that are currently mounted, as this can cause system instability or data corruption.

  • Kernel Not Aware of Changes: After modifying partitions, the kernel might not immediately recognize the changes. Use partprobe or reboot to reload partition tables.

  • MBR Limitations: MBR partition tables support only four primary partitions. To create more, you must use extended and logical partitions or switch to GPT.

  • Using on GPT Disks: For GPT disks, fdisk may have limited functionality. Use gdisk or parted for full GPT support.

  • Accidental Data Overwrite: Writing the partition table with wrong parameters can overwrite data. Double-check partition boundaries and sizes.

Common Errors and Troubleshooting

  • “Device or resource busy” Error: Happens if the partition is mounted or in use. Unmount the partition before modifying.

  • “Partition Doesn’t Exist” Error: When deleting or modifying partitions, ensure the correct partition number is used.

  • Changes Not Reflected After Writing: Use sudo partprobe /dev/sda or reboot to notify the kernel.

  • Invalid Partition Table: Can occur if partitions overlap or boundaries are incorrect. Use p frequently to verify.

  • Permission Denied: Remember to run fdisk as root or with sudo.

Security Considerations

Partitioning commands like fdisk require root privileges because they modify critical disk structures. Unauthorized or incorrect use can lead to system instability or data loss. Always ensure you have proper backups and verify commands before executing. Avoid running scripts blindly without understanding the commands.

Performance Considerations

Proper partition alignment to physical disk sectors or SSD erase block sizes improves performance and reduces wear on SSDs. fdisk allows specifying sectors or sizes; using default alignment is usually safe, but advanced users should verify alignment especially in RAID or SSD setups.

Tips and Best Practices

  • Always back up important data before partitioning.

  • Use sudo or run as root because modifying partitions requires elevated permissions.

  • Frequently use p inside the interactive prompt to check the partition table state before writing.

  • Understand the difference between primary, extended, and logical partitions; extended partitions act as containers for logical partitions.

  • After writing changes, run sudo partprobe or reboot to update kernel partition information.

  • For GPT disks, consider using gdisk or parted for better support.

  • Avoid modifying partitions that are mounted or in use.

  • Use descriptive partition types and set the bootable flag only when necessary.

Possible Alternatives and Related Commands

  • parted: A modern and flexible partitioning tool supporting GPT and scripting.

  • gdisk: GPT partitioning tool similar to fdisk but designed specifically for GPT.

  • lsblk: Lists block devices and partitions in a tree-like format.

  • blkid: Shows block device attributes such as filesystem type.

  • partprobe: Reloads the kernel partition table without rebooting.

See Also

Further Reading

As an Amazon Associate, I earn from qualifying purchases.

The text above is licensed under CC BY-SA 4.0 CC BY SA