Linux online course

Using fdisk to List, Create, Delete, and Change Linux Disk Partitions

Learn how to use Linux fdisk safely to inspect partition tables, create and delete partitions, change partition types, and prepare storage for later formatting and mounting.

fdisk is an interactive, text-based Linux utility for viewing and modifying disk partition tables. A partition table is disk metadata that records where partitions begin and end, their identifiers, and attributes.

This guide covers inspecting a disk, creating and deleting partitions, changing partition types, reviewing pending edits, and safely writing or discarding changes. Partitioning is potentially destructive: verify the target disk, maintain backups, and avoid changing a disk that contains mounted or actively used data.

What fdisk Manages

A disk device is the path for an entire storage device, such as /dev/sda or /dev/nvme0n1. A partition is a defined region of that disk, represented by a path such as /dev/sda1 or /dev/nvme0n1p1. In normal use, start fdisk with the whole-disk device, not an existing partition path.

fdisk records partition boundaries in sectors. A sector is an addressable disk unit used to describe a partition's start and end. After a partition exists, a separate tool creates a filesystem such as ext4 or XFS inside it. Linux then makes that filesystem available with a mount operation.

fdisk actions versus follow-up storage tasks

Create partition: Performed by fdisk. A follow-up filesystem or swap tool creates usable on-partition storage.

Set partition type: Performed by fdisk. This changes partition metadata, not the data format.

Create filesystem: Not performed by fdisk. Use an appropriate mkfs tool; the result is a filesystem.

Create swap signature: Not performed by fdisk. Use mkswap; the result is swap-formatted space.

Mount filesystem: Not performed by fdisk. Use mount; the result is access through a directory.

Enable swap: Not performed by fdisk. Use swapon; the result is active swap.

Identify the Correct Disk First

Never choose a device only because its name looks familiar. Device letters can change when disks are added, removed, or detected in a different order. Compare the device's capacity, model, partition layout, filesystem information, and mount points with your expectations.

lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS

This non-destructive command shows disks and partitions in a tree. The TYPE column helps distinguish a whole disk from a partition, while FSTYPE and MOUNTPOINTS reveal existing usage.

sudo fdisk -l

This lists recognized disks and partition tables without opening an editing session. Use both commands when identifying an unfamiliar disk.

SATA, NVMe, and virtual disk naming

SATA/SCSI/USB-style disk: Whole disk /dev/sda; first partition /dev/sda1. The partition number follows the disk name directly.

NVMe disk: Whole disk /dev/nvme0n1; first partition /dev/nvme0n1p1. The p separates the disk namespace from the partition number.

Virtual disk: Often whole disk /dev/vda; first partition /dev/vda1. Virtual-machine configuration can expose other naming patterns, so confirm with lsblk.

Start an fdisk Session

Use sudo because changing partition metadata normally requires administrator privileges. Replace the example device only after verifying it.

sudo fdisk /dev/sdX

For an NVMe whole disk, use the complete disk path:

sudo fdisk /dev/nvme0n1

fdisk opens an interactive prompt. Type m to display its built-in help. Most edits are held in memory during the session. They do not become persistent until you explicitly use w.

Common fdisk interactive commands

m: Show help. Typical result: a list of available commands. Safety note: non-destructive.

p: Print the current or pending partition table. Typical result: disk geometry and partition rows. Safety note: non-destructive.

n: Add a partition. Typical result: interactive prompts. Safety note: pending until written.

d: Delete a partition-table entry. Typical result: the selected entry is removed from the pending table. Safety note: verify the number carefully.

t: Change a partition type. Typical result: a new metadata identifier. Safety note: does not convert or format a filesystem.

l: List recognized partition types during type selection. Typical result: type codes and names. Safety note: non-destructive.

q: Quit without writing pending changes. Typical result: edits are discarded. Safety note: safest cancellation path.

w: Write the pending table and exit. Typical result: metadata is written to disk. Safety note: review everything first.

Print and Interpret the Partition Table

Inside fdisk, use:

p

The output identifies the disk and displays its current or proposed partitions. Typical fields include:

  • Device: The partition path, such as /dev/sda1.
  • Boot flag: A boot-related marker where applicable. Its meaning depends on the partition-table format and firmware or bootloader design.
  • Start: The first sector assigned to the partition.
  • End: The last sector assigned to the partition.
  • Sectors: The number of sectors in the partition.
  • Size: A human-readable approximation of the partition capacity.
  • Type: The identifier and descriptive name, such as a Linux filesystem or Linux swap type.

Free space appears as gaps between the disk's usable sectors and existing partition ranges. Accepting fdisk's sensible default boundaries usually preserves alignment. Alignment places partitions at efficient boundaries, which is particularly important for modern disks and solid-state storage.

Create a Partition

Before creating anything, use p and confirm that the selected disk contains the intended unallocated space. Then enter:

n

fdisk may ask for a partition number, the first sector, and the last sector or desired size. On an MBR/DOS table, it may also ask whether to create a primary or extended partition. On GPT, the available questions and partition model differ.

  1. Enter an available partition number, or accept the suggested number.
  2. For the first sector, accept the default unless you have a specific layout requirement.
  3. For the last sector, accept the default to use the available range, or enter a size with a supported suffix such as +20G.
  4. Use p to inspect the proposed table before writing.

For example, a new data partition might use the default first sector and a size of +100G, leaving later free space for another purpose. Creating the partition only creates boundaries and metadata; it does not create a filesystem.

Delete a Partition

Print the table first and identify the exact partition number. Then use:

d

When prompted, enter the number of the partition to remove. Print the pending table again with p and verify that the intended entry, not a neighboring partition, has disappeared.

Deleting an entry removes its description from the partition table. It does not necessarily overwrite every byte formerly belonging to that partition immediately, so data may remain recoverable by specialized tools. However, once the deletion is written, the operating system no longer treats that region as the original partition, and its filesystem may become inaccessible.

If you are uncertain, use:

q

This exits without applying pending deletion or other edits.

Change a Partition Type

A partition type is metadata indicating intended use. It is distinct from a filesystem. The common Linux filesystem type is intended for ordinary Linux data partitions, while the Linux swap type identifies a partition intended for swap space.

To change a type, enter:

t

Select the target partition number when prompted. During the type-selection workflow, use:

l

to display recognized type codes or names. Choose the identifier appropriate for the intended use, then print the pending table for review.

Changing a type code does not format the partition, create swap, convert ext4 to XFS, or migrate existing data. Use filesystem- or swap-specific tools afterward, and only after verifying the target path.

Save or Discard Changes

Review the proposed table with p. If it is correct, write it with:

w

This updates the partition table and exits fdisk. Writing a deletion or a changed boundary can make data inaccessible, so treat w as an operational change, not a harmless save command.

To abandon all unwritten edits:

q

After writing, the kernel may need to reread the table. When appropriate and when the disk is not actively in use, request a reread with:

sudo partprobe /dev/sdX

If the kernel cannot reload the table because partitions are mounted, used by swap, or part of the running system, rebooting may be necessary. Do not force changes to an active system disk merely to avoid a reboot.

MBR/DOS and GPT Partition Tables

MBR/DOS is a legacy partition-table format. It traditionally supports primary partitions, an extended partition, and logical partitions inside that extended container. The number of directly recorded primary entries is limited, which led to the extended/logical workaround.

GPT, or GUID Partition Table, is the modern format commonly used with UEFI systems and large disks. GPT records partitions in its own partition-entry array and does not use the MBR primary, extended, and logical organization. It normally supports many partitions without the old extended-partition container model.

MBR and GPT concepts

Partition organization: MBR/DOS uses a legacy partition table with primary entries and an optional extended container; GPT uses GUID-based partition entries.

Primary, extended, and logical partitions: These are MBR/DOS concepts. GPT does not require this hierarchy.

Typical modern use: MBR remains useful for compatibility; GPT is common on large disks and newer installations.

Firmware association: MBR is associated with traditional BIOS-style arrangements; GPT is commonly paired with UEFI, although firmware support and boot configuration are separate concerns.

fdisk's prompts and available options vary with the util-linux version and the disk's partition-table type. Do not apply an MBR tutorial mechanically to a GPT disk, or assume that a displayed type name has identical boot implications in every environment.

Practical Workflow: Inspect a SATA or Virtual Disk

  1. List devices and mount points: lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS.
  2. Compare the intended disk's capacity and model information with the output.
  3. Open the verified whole disk, for example sudo fdisk /dev/sda.
  4. Print the table with p.
  5. Interpret partition numbers, sector ranges, sizes, and types.
  6. Exit with q because this inspection made no changes.

Practical Workflow: Create a Linux Data Partition

  1. Confirm the disk has unallocated space and is not the wrong device.
  2. Open fdisk with administrator privileges.
  3. Use n and answer the partition-number, first-sector, and last-sector or size prompts.
  4. Use p to review the proposed table, including the new partition path.
  5. If anything is wrong, use q and start again. If correct, use w.
  6. Recheck the result with sudo fdisk -l or lsblk.
  7. Create a filesystem separately, only after verifying the device name:
sudo mkfs.ext4 /dev/sdX1

Formatting can destroy existing data on the selected partition. After formatting, mount the filesystem with the appropriate mount workflow and optionally configure persistent mounting in /etc/fstab.

Practical Workflow: Delete or Cancel Safely

  1. Print the table and record the exact partition number.
  2. Use d and select that number.
  3. Print again with p to verify the pending deletion.
  4. Use q to cancel without writing if there is any doubt.
  5. Use w only after confirming that the partition is backed up and no required data depends on it.

Practical Workflow: Mark a Partition for Linux Swap

  1. Create or select the intended partition and verify its path.
  2. Use t and select the partition number.
  3. Use l to display recognized types, then choose the Linux swap type.
  4. Print the pending table and write it with w after review.
  5. Create and enable swap as separate operations:
sudo mkswap /dev/sdX2 && sudo swapon /dev/sdX2

Enabling swap does not happen automatically merely because fdisk assigned the Linux swap type. Persistent swap configuration is a separate administration task.

Practical Workflow: NVMe Devices

For an NVMe disk, the whole-device path includes a namespace number, such as /dev/nvme0n1. Its first partition is normally /dev/nvme0n1p1, not /dev/nvme0n11.

sudo fdisk /dev/nvme0n1

Use the whole-disk path when starting fdisk. After creating a partition, check the resulting name with p, lsblk, or a non-destructive fdisk listing before formatting or mounting it.

Post-Partitioning Workflow

Partitioning is one stage in a storage workflow:

  1. Confirm the new partition layout.
  2. Verify the partition device name and intended purpose.
  3. Create an appropriate filesystem, or create a swap signature if it is swap space.
  4. Mount a filesystem or activate swap.
  5. Optionally configure persistent mounting or swap activation using the system's normal configuration methods.

Keep these stages separate. A partition can exist without containing a filesystem, and a filesystem can exist without being mounted. Always verify the device before using destructive commands such as mkfs.

Safety Rules

  • Back up important data before changing any partition table.
  • Confirm the disk by size, model, layout, and mount points, not by device letter alone.
  • Do not edit mounted or actively used partitions when an unmounted maintenance or rescue environment is available.
  • Be especially cautious with the system disk, active swap, RAID, LVM, encrypted volumes, removable media, and virtual-machine disks.
  • Review the pending table with p before using w.
  • After completing the operation, inspect the result non-destructively with lsblk or sudo fdisk -l.

Troubleshooting fdisk Operations

The wrong disk might be selected

If the displayed size, model, mounted partitions, or existing layout does not match your plan, use q immediately. Compare devices again with lsblk and sudo fdisk -l. Do not rely on a device letter because enumeration can change.

The new partition does not appear after writing

Recheck the table with sudo fdisk -l. If the disk is not actively in use, request a kernel reread with sudo partprobe /dev/sdX. If the kernel cannot safely reload the table, reboot and check again.

fdisk says that the disk or partition is busy

Identify mounted filesystems and active swap. Do not force changes to a running system disk. Unmount or deactivate storage only when operationally safe, or perform the change from a maintenance or rescue environment.

A created partition cannot be mounted

fdisk created only a partition boundary and metadata. It did not create a filesystem. Confirm the partition path, create the intended filesystem, and then mount that partition.

A type change did not convert the filesystem

A type code is metadata, not a conversion tool. Changing it does not alter the existing filesystem or its data. Use a planned filesystem migration or formatting procedure when conversion is actually required.

The expected MBR partition kind is unavailable

Check whether the disk uses GPT. Primary, extended, and logical terminology belongs to the traditional MBR model and may not appear for GPT disks.

Exam-Relevant Notes

  • fdisk manages partition tables, not filesystems.
  • Use the whole disk, such as /dev/sda or /dev/nvme0n1, when opening a normal fdisk session.
  • p prints, n creates, d deletes, t changes a type, q discards, and w writes.
  • A partition type indicates intended use but does not format or convert the partition.
  • MBR uses primary, extended, and logical concepts; GPT does not use that model.
  • Partitioning changes can destroy access to data, especially when written to a disk with active or mounted storage.

Related Linux Storage Topics

For surrounding Linux administration skills, see Linux, showing the full path of shell commands, Bash, and determining file types.