fdisk Partition Tool

fdisk cheatsheet — list, create, delete partitions. fdisk -l to list disks, fdisk /dev/sdb to edit, n for new, d for delete, w to write. Linux partition management.

6 min read

What it is

fdisk is a command-line utility for manipulating disk partition tables on Linux and Unix-like systems. You reach for it when you need to create, delete, resize, or view partitions on a storage device.

Installation

fdisk is typically pre-installed on most Linux distributions. If not, you can install it using your distribution’s package manager:

Debian/Ubuntu:

sudo apt update
sudo apt install fdisk

Fedora/CentOS/RHEL:

sudo dnf install fdisk
# or
sudo yum install fdisk

macOS: fdisk is available on macOS, but diskutil is the more common and recommended tool for partition management. If you specifically need fdisk, it might require manual installation or might be present in older systems.

Windows: fdisk is not a native Windows command. Windows uses diskpart for partition management.

Core Concepts

  • Partition Table: A structure on a storage device (hard drive, SSD) that stores information about how the disk is divided into partitions. fdisk primarily works with MBR (Master Boot Record) and GPT (GUID Partition Table) partition tables.
  • MBR (Master Boot Record): An older partition table scheme. It has limitations: a maximum of 4 primary partitions (or 3 primary and 1 extended), and a maximum disk size of 2TB.
  • GPT (GUID Partition Table): A newer, more flexible partition table scheme. It supports a much larger number of partitions (typically 128) and much larger disk sizes (effectively unlimited for practical purposes). fdisk can manage both.
  • Partition Types: Different types of partitions exist, such as primary, extended, logical, Linux filesystem, Linux swap, EFI System Partition, etc. fdisk allows you to set these types.
  • Device Names: Disks are identified by device names like /dev/sda, /dev/sdb, /dev/nvme0n1. Partitions on these disks are then named like /dev/sda1, /dev/sda2, etc.

Commands / Usage

fdisk is an interactive utility. You typically run it with the disk device as an argument, then enter commands within its interactive prompt.

Viewing Partitions:

  • List partitions on a disk:

    sudo fdisk -l /dev/sda
    

    Displays the partition table for /dev/sda, including partition type, start/end sectors, and size.

  • List all disks and their partitions:

    sudo fdisk -l
    

    Scans all block devices and prints their partition information.

Manipulating Partitions (Interactive Mode):

Run fdisk with the disk device to enter interactive mode. Common commands within the fdisk prompt:

  • n - Add a new partition:

    Command (m for help): n
    Partition type
       p   primary (0 primary, 0 extended, 4 free)
       e   extended (container for logical partitions)
    Select (default p): p
    Partition number (1-4, default 1): 1
    First sector (2048-209715199, default 2048): 2048
    Last sector, +sectors or +size{K,M,G,T,P} (2048-209715199, default 209715199): +10G
    

    Creates a new partition. You’ll be prompted for partition type (primary/extended), partition number, and the start/end sector or size.

  • d - Delete a partition:

    Command (m for help): d
    Partition number (1-4, default 1): 1
    

    Deletes the specified partition.

  • p - Print the partition table:

    Command (m for help): p
    

    Displays the current state of the partition table.

  • t - Change a partition’s type:

    Command (m for help): t
    Selected partition (default 1): 1
    Hex code (type L to list all): 83
    

    Changes the partition type ID. You’ll be prompted for the partition number and the hex code for the new type (e.g., 83 for Linux filesystem, 82 for Linux swap). Use L to list known hex codes.

  • w - Write table to disk and exit:

    Command (m for help): w
    

    Crucial: This command saves all your changes to the disk’s partition table. If you exit without writing, no changes will be applied.

  • q - Quit without saving:

    Command (m for help): q
    

    Exits fdisk without applying any changes made during the session.

  • m - Display help menu:

    Command (m for help): m
    

    Shows a list of available commands within the fdisk prompt.

  • a - Toggle the bootable flag:

    Command (m for help): a
    Partition number (1-4): 1
    

    Marks or unmarks a partition as bootable.

  • g - Create a new empty GPT partition table:

    Command (m for help): g
    

    Initializes the disk with a new GPT partition table.

  • o - Create a new empty MBR partition table:

    Command (m for help): o
    

    Initializes the disk with a new MBR partition table.

Specific Disk Operations (Non-Interactive):

  • Create a new primary partition (MBR only):
    sudo fdisk -N 1 /dev/sdb # Specify partition number
    echo -e 'o\nn\np\n1\n\n+10G\nw' | sudo fdisk /dev/sdb
    
    This is a more advanced usage where you pipe commands to fdisk. o creates an MBR table, n creates a new primary partition, p defaults to primary, 1 defaults to partition 1, the empty lines accept defaults for start/end, +10G sets the size, and w writes and exits. Use with extreme caution.

Common Patterns

  • Adding a new partition and formatting it:

    1. Enter fdisk /dev/sdX.
    2. Press n to create a new partition.
    3. Follow prompts to define size/location.
    4. Press w to write changes.
    5. After fdisk exits, format the new partition (e.g., /dev/sdXN):
      sudo mkfs.ext4 /dev/sdX1
      
  • Resizing a partition (requires unmounting and often external tools): fdisk itself is not ideal for resizing mounted partitions or shrinking them. For expanding, you might delete and recreate a partition of the same type and starting sector with a larger size, then use resize2fs (for ext filesystems) to expand the filesystem into the new space. Shrinking is more complex and risky. Tools like parted or GUI tools are often better for resizing.

  • Checking partition table type (MBR vs GPT):

    sudo fdisk -l /dev/sda | grep 'Disklabel type'
    

    This will output either dos (for MBR) or gpt.

  • Creating a swap partition:

    1. Enter fdisk /dev/sdX.
    2. Press n to create a new partition.
    3. Define size (e.g., +4G).
    4. Press t to change type.
    5. Enter 82 for Linux swap.
    6. Press w to write changes.
    7. Create the swap space:
      sudo mkswap /dev/sdXN # Replace sdXN with your new partition
      sudo swapon /dev/sdXN
      
    8. Add to /etc/fstab to make it permanent.

Gotchas

  • Data Loss Risk: fdisk modifies the partition table directly. Incorrect commands or accidental deletions can lead to permanent data loss. Always back up critical data before using fdisk.
  • Changes are not immediate: Changes made within fdisk are not applied to the disk until you use the w (write) command. Using q (quit) discards all changes.
  • Kernel Re-reading: After writing changes with w, the kernel might not immediately recognize the new partition layout. You may need to reboot or use partprobe /dev/sdX (if installed) or kpartx -a /dev/sdX to force the kernel to re-read the partition table.
  • Partition Numbering: Be careful when deleting and adding partitions. If you delete /dev/sda2 and then add a new partition, it might be numbered /dev/sda2 again, but it’s a new entity.
  • MBR vs. GPT: fdisk can manage both. Ensure you are working with the correct partition table type for your disk and needs. Converting between MBR and GPT usually requires data backup and re-partitioning. fdisk can create new MBR or GPT tables (o for MBR, g for GPT).
  • Mounted Partitions: fdisk can usually view partitions even if they are mounted, but it’s generally unsafe to modify partitions that are currently in use or mounted. Unmount them first if possible.
  • Device Names: Ensure you are targeting the correct disk device (e.g., /dev/sda vs /dev/sdb). Operating on the wrong disk can be catastrophic.
  • Sector Alignment: While fdisk generally handles alignment well on modern systems, be mindful of sector sizes if dealing with older drives or specific performance requirements.