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.
fdiskprimarily 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).
fdiskcan manage both. - Partition Types: Different types of partitions exist, such as primary, extended, logical, Linux filesystem, Linux swap, EFI System Partition, etc.
fdiskallows 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/sdaDisplays the partition table for
/dev/sda, including partition type, start/end sectors, and size. -
List all disks and their partitions:
sudo fdisk -lScans 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): +10GCreates 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): 1Deletes the specified partition.
-
p- Print the partition table:Command (m for help): pDisplays 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): 83Changes the partition type ID. You’ll be prompted for the partition number and the hex code for the new type (e.g.,
83for Linux filesystem,82for Linux swap). UseLto list known hex codes. -
w- Write table to disk and exit:Command (m for help): wCrucial: 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): qExits
fdiskwithout applying any changes made during the session. -
m- Display help menu:Command (m for help): mShows a list of available commands within the
fdiskprompt. -
a- Toggle the bootable flag:Command (m for help): a Partition number (1-4): 1Marks or unmarks a partition as bootable.
-
g- Create a new empty GPT partition table:Command (m for help): gInitializes the disk with a new GPT partition table.
-
o- Create a new empty MBR partition table:Command (m for help): oInitializes the disk with a new MBR partition table.
Specific Disk Operations (Non-Interactive):
- Create a new primary partition (MBR only):
This is a more advanced usage where you pipe commands tosudo fdisk -N 1 /dev/sdb # Specify partition number echo -e 'o\nn\np\n1\n\n+10G\nw' | sudo fdisk /dev/sdbfdisk.ocreates an MBR table,ncreates a new primary partition,pdefaults to primary,1defaults to partition 1, the empty lines accept defaults for start/end,+10Gsets the size, andwwrites and exits. Use with extreme caution.
Common Patterns
-
Adding a new partition and formatting it:
- Enter
fdisk /dev/sdX. - Press
nto create a new partition. - Follow prompts to define size/location.
- Press
wto write changes. - After
fdiskexits, format the new partition (e.g.,/dev/sdXN):sudo mkfs.ext4 /dev/sdX1
- Enter
-
Resizing a partition (requires unmounting and often external tools):
fdiskitself 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 useresize2fs(for ext filesystems) to expand the filesystem into the new space. Shrinking is more complex and risky. Tools likepartedor 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) orgpt. -
Creating a swap partition:
- Enter
fdisk /dev/sdX. - Press
nto create a new partition. - Define size (e.g.,
+4G). - Press
tto change type. - Enter
82for Linux swap. - Press
wto write changes. - Create the swap space:
sudo mkswap /dev/sdXN # Replace sdXN with your new partition sudo swapon /dev/sdXN - Add to
/etc/fstabto make it permanent.
- Enter
Gotchas
- Data Loss Risk:
fdiskmodifies the partition table directly. Incorrect commands or accidental deletions can lead to permanent data loss. Always back up critical data before usingfdisk. - Changes are not immediate: Changes made within
fdiskare not applied to the disk until you use thew(write) command. Usingq(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 usepartprobe /dev/sdX(if installed) orkpartx -a /dev/sdXto force the kernel to re-read the partition table. - Partition Numbering: Be careful when deleting and adding partitions. If you delete
/dev/sda2and then add a new partition, it might be numbered/dev/sda2again, but it’s a new entity. - MBR vs. GPT:
fdiskcan 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.fdiskcan create new MBR or GPT tables (ofor MBR,gfor GPT). - Mounted Partitions:
fdiskcan 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/sdavs/dev/sdb). Operating on the wrong disk can be catastrophic. - Sector Alignment: While
fdiskgenerally handles alignment well on modern systems, be mindful of sector sizes if dealing with older drives or specific performance requirements.