fdisk Command in Linux With Examples

Use the fdisk command in Linux to inspect disks, practice GPT/MBR edits in images, change types, delete entries, and fix errors.

PublishedAuthorJoshua JamesRead time12 minGuide typeLinux Commands

Partition-table mistakes are expensive because they can make a disk unbootable, hide data, or point formatting tools at the wrong device. The fdisk command in Linux edits GPT and MBR partition tables, but the safest way to learn it is on a disposable disk image before touching a real block device.

The write examples use disposable image files in your home directory, so you can practice interactive commands without changing a physical disk. They follow util-linux fdisk, the implementation shipped by common Linux distributions; use man fdisk or the fdisk(8) manual page for the full option reference for your installed version.

Know What fdisk Changes

fdisk edits the partition table on a whole disk or disk image. It does not create filesystems, mount partitions, copy data, resize filesystems, or show which directories are using space. If the problem is a full filesystem, check directory usage with du disk usage analysis examples before changing partitions.

fdisk Command Syntax

fdisk [options] disk
fdisk [options] -l [disk...]
  • disk should be a whole-disk device such as /dev/sdb, /dev/nvme0n1, or a disk image file such as fdisk-demo.img.
  • Partitions have their own device names, such as /dev/sdb1 or /dev/nvme0n1p1. Open the whole disk with fdisk, not an individual partition, when you are editing the table.
  • The -l option lists partition tables and exits. Opening fdisk without -l starts the interactive editor.
  • Changes stay in memory until you write them with w. Use q to quit without saving when the printed table does not match your plan.

Choose GPT or MBR Before Partitioning

Most modern Linux systems should use GPT, especially UEFI systems and disks larger than 2 TiB. MBR, also called DOS in fdisk, remains useful for old BIOS workflows, old operating systems, or removable media that must work with legacy tools.

Disk Labelfdisk KeyBest ForCommon Limit
GPTgModern Linux installs, UEFI boot, large disks, and normal new layouts.Old BIOS-only tools may not understand it.
DOS/MBRoLegacy BIOS compatibility, simple old-system media, and some firmware-specific workflows.Four primary entries and practical size limits on large disks.

fdisk Quick Reference

TaskCommand or KeyWhat It Does
List partition tablessudo fdisk -lShows disks, labels, sectors, sizes, and partition entries.
List one disk or imagefdisk -l fdisk-demo.imgPrints one target and exits.
Create a GPT labelgStarts a new GPT partition table in memory.
Create an MBR labeloStarts a new DOS/MBR partition table in memory.
Create a partitionnAdds a new partition entry.
Print the pending tablepShows changes before writing.
Change partition typetSets metadata such as Linux filesystem, Linux swap, or EFI System.
List partition type codesLDisplays available type aliases and numeric codes.
Delete a partition entrydRemoves a partition entry from the in-memory table.
Write changeswCommits the in-memory table to the target.
Quit without savingqLeaves fdisk and discards pending changes.

Verify or Install fdisk

Most full Linux installations already include fdisk. Confirm the resolved command path and util-linux version before relying on newer options such as --lock, --wipe, or --wipe-partitions:

command -v fdisk
fdisk --version

Example output can look like this on a Debian-family system:

/usr/sbin/fdisk
fdisk from util-linux 2.41.3

If the command is missing, install the package that provides util-linux partitioning tools for your distribution. Debian and Ubuntu use the fdisk package:

sudo apt update
sudo apt install fdisk

Fedora and many RHEL-family systems provide the command from util-linux:

sudo dnf install util-linux

Arch Linux also uses util-linux:

sudo pacman -S util-linux

openSUSE provides the same command through util-linux:

sudo zypper install util-linux

Debian and Ubuntu package the classic fdisk, sfdisk, and cfdisk tools in the fdisk binary package. Fedora, Arch Linux, openSUSE, and many RHEL-family systems provide fdisk from util-linux. Do not remove or reinstall util-linux casually because it owns many core system tools.

Identify Disks Before Using fdisk

Always identify the disk from more than one signal before editing a real partition table. Device names can change between boots, USB reinsertions, VM controller changes, and NVMe/SATA layouts.

List Whole Disks with lsblk

Start with a disk-only view when you need to distinguish internal drives, removable media, and read-only devices:

lsblk -d -o NAME,SIZE,MODEL,SERIAL,TRAN,RM,RO,TYPE

The -d option keeps the output at the whole-disk level. Use size, model, serial, transport, removable state, and read-only state together; do not pick a disk from size alone when several devices are similar.

Check Mounted Partitions

Use a tree view before partitioning so active filesystems stand out:

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

Look for the TYPE column. A whole disk usually appears as disk, while existing partitions appear as part. Do not edit the disk that contains /, /boot, /home, or another active mount unless you are following a planned recovery or installer workflow.

List Partition Tables with fdisk

Use read-only listing mode when you need the partition table details from fdisk itself:

sudo fdisk -l

Limit the listing to one confirmed disk when the host has several drives:

sudo fdisk -l /dev/sdb

Use the whole-disk path. For NVMe drives, that path looks like /dev/nvme0n1; partitions under it use a p separator, such as /dev/nvme0n1p1.

Read fdisk Output Columns

FieldMeaningWhy It Matters
Disklabel typePartition table format, usually gpt or dos.GPT is the normal choice for modern UEFI systems; DOS/MBR remains useful for legacy compatibility.
DevicePartition entry path or image partition name.Confirms which partition entry the row describes.
Start and EndFirst and final sector for the partition entry.Needed when preserving starts, checking gaps, or recreating a partition entry.
SectorsNumber of sectors in the partition.Maps to the partition size with the disk sector size.
SizeHuman-readable partition size.Quick check that the partition size matches the intended layout.
TypePartition type label.Helps bootloaders, installers, and tools recognize roles such as Linux filesystem, Linux swap, or EFI System.

Preview Old Signatures Before Reusing a Disk

Old filesystem, swap, RAID, LVM, or partition-table signatures can confuse installers and recovery tools. Preview signatures with wipefs -n before deciding whether the disk is truly disposable:

sudo wipefs -n /dev/sdb
sudo wipefs -n /dev/sdb1

The -n option is a no-act preview. Remove signatures only when you intentionally want to reuse the partition or disk and already have backups; removing signatures from a partition you are trying to recover can make recovery harder.

Practice Common fdisk Workflows with a Disk Image

A disk image lets fdisk work against a regular file that behaves like a small blank disk. The practice workflow creates, inspects, modifies, formats, mounts, and removes partition tables inside disposable image files.

Create a Blank Disk Image

Create a 512 MiB sparse image in your home directory:

cd ~
truncate -s 512M fdisk-demo.img

List the image with fdisk before creating a partition table:

fdisk -l fdisk-demo.img

The blank image has disk geometry but no partition label yet:

Disk fdisk-demo.img: 512 MiB, 536870912 bytes, 1048576 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Create a GPT Table with Two Partitions

The interactive key sequence creates a GPT label, adds a 128 MiB first partition, adds a second partition that uses the remaining aligned space, prints the pending table, and writes the result:

fdisk --wipe never --wipe-partitions never fdisk-demo.img <<'FDISK'
g
n
1

+128M
n
2


p
w
FDISK

The g command creates GPT, n creates each partition, blank first-sector prompts accept fdisk alignment defaults, +128M sets a relative size for partition 1, and a blank final-sector prompt lets partition 2 use the remaining available space.

The --wipe never and --wipe-partitions never options are used here only for repeatable disposable-image practice. They keep old-signature prompts from interrupting reruns of the demo commands. On a real disk, leave signature warnings visible unless you have already inspected the target and decided the old signatures are safe to remove.

Confirm the new table with focused columns:

fdisk -l -o Device,Start,End,Sectors,Size,Type fdisk-demo.img

Relevant lines include the GPT-aligned start sectors and both partition rows:

Device           Start     End Sectors  Size Type
fdisk-demo.img1   2048  264191  262144  128M Linux filesystem
fdisk-demo.img2 264192 1046527  782336  382M Linux filesystem

Change a Partition Type to Linux Swap

Partition type is metadata. Changing a partition to Linux swap helps tools recognize its role, but it does not create a swap signature by itself. Use L inside fdisk when you need the current type list for your installed version.

Change partition 2 in the demo image to Linux swap:

fdisk --wipe never --wipe-partitions never fdisk-demo.img <<'FDISK'
t
2
19
p
w
FDISK

Verify the type change:

fdisk -l -o Device,Start,End,Sectors,Size,Type fdisk-demo.img

Relevant lines include the updated type for partition 2:

Device           Start     End Sectors  Size Type
fdisk-demo.img1   2048  264191  262144  128M Linux filesystem
fdisk-demo.img2 264192 1046527  782336  382M Linux swap

For GPT, current util-linux fdisk uses type code 19 for Linux swap and type code 1 for EFI System. For MBR, Linux data partitions commonly show type ID 83. Verify with L instead of assuming codes from an old article, especially when switching between GPT and MBR.

Create an EFI System Partition Example

UEFI boot media normally needs an EFI System Partition. This small demo image shows the fdisk type change only; real boot disks commonly use a larger ESP and still need the correct filesystem and bootloader steps after partitioning.

truncate -s 128M fdisk-efi-demo.img
fdisk --wipe never --wipe-partitions never fdisk-efi-demo.img <<'FDISK'
g
n
1

+32M
t
1
p
w
FDISK

Because the image has only one partition, fdisk selects that partition when t starts the type change. The following 1 is the GPT type code for EFI System.

Verify that the first GPT partition uses the EFI System type:

fdisk -l -o Device,Start,End,Sectors,Size,Type fdisk-efi-demo.img

Relevant lines include the EFI System label:

Device              Start   End Sectors Size Type
fdisk-efi-demo.img1  2048 67583   65536  32M EFI System

Format and Mount the Demo Partition

fdisk creates partition entries, not filesystems. Map the image through a loop device when you want to prove the first demo partition can be formatted and mounted safely:

loopdev=$(sudo losetup --find --show --partscan "$HOME/fdisk-demo.img")
sudo udevadm settle 2>/dev/null || true
lsblk -rno NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS "$loopdev"

Example output uses a loop number that will vary on your system:

loop0 512M loop
loop0p1 128M part
loop0p2 382M part

Create an ext4 filesystem on the first loop partition and a swap signature on the second loop partition:

sudo mkfs.ext4 -F "${loopdev}p1"
sudo mkswap "${loopdev}p2"

Mount the ext4 partition, then verify the source and filesystem type:

mkdir -p "$HOME/fdisk-mount-demo"
sudo mount "${loopdev}p1" "$HOME/fdisk-mount-demo"
findmnt -no SOURCE,FSTYPE "$HOME/fdisk-mount-demo"

Example output should show the loop partition and ext4:

/dev/loop0p1 ext4

Unmount the demo filesystem and detach the loop device before continuing:

sudo umount "$HOME/fdisk-mount-demo"
sudo losetup -d "$loopdev"
rmdir "$HOME/fdisk-mount-demo"

Create an MBR Partition Table for Legacy Workflows

Use a separate image when you need to practice DOS/MBR partitioning. The o command creates the MBR label, and the p answer to the partition-type prompt creates a primary partition:

truncate -s 128M fdisk-mbr-demo.img
fdisk --wipe never --wipe-partitions never fdisk-mbr-demo.img <<'FDISK'
o
n
p
1

+64M
p
w
FDISK

Verify the MBR table and type ID:

fdisk -l -o Device,Boot,Start,End,Sectors,Size,Id,Type fdisk-mbr-demo.img

Relevant lines include the MBR type ID and partition row:

Device              Boot Start    End Sectors Size Id Type
fdisk-mbr-demo.img1       2048 133119  131072  64M 83 Linux

Use fdisk Listing and Output Options

fdisk has several read-only options that help you inspect a table without entering the editor. Use these before changing a disk, and use them after writing to verify the final table.

Choose Specific Output Columns

Use -o when you need a focused report. The available columns depend on the disk label type:

fdisk -l -o Device,Start,End,Sectors,Size,Type fdisk-demo.img

Use fdisk --help to list the columns supported by your installed version and label type.

Print Sizes in Bytes

Add --bytes when a script or inventory report needs exact byte counts instead of human-readable units:

fdisk --bytes -l -o Device,Size,Type fdisk-demo.img

Relevant lines include byte counts in the Size column:

Device               Size Type
fdisk-demo.img1 134217728 Linux filesystem
fdisk-demo.img2 400556032 Linux swap

Show Detailed Partition Metadata

Add -x when GPT UUIDs, type UUIDs, names, attributes, or usable LBA limits matter:

fdisk -x fdisk-demo.img

The detailed view is useful before troubleshooting boot entries, partition type mismatches, cloned images, or recovery notes. UUID values are generated per image, so do not copy another system’s UUID row into your own layout.

Restrict Recognition to One Label Type

Use -t to recognize only one partition-table type when you are checking for a specific label:

fdisk -t gpt -l fdisk-demo.img

If the target is not GPT, this check fails instead of silently accepting a different label type.

Use sfdisk for Repeatable Scripts

fdisk is designed primarily for human interaction. Its input commands are intended to stay compatible, but list output is not a stable scripting interface. Use sfdisk for repeatable partition-table scripts, and keep fdisk for interactive inspection and manual edits.

Delete a Demo Partition Entry

Use d when you need to remove a partition entry from a table. This example deletes partition 2 from the disposable GPT image after the earlier listing examples are complete:

fdisk --wipe never --wipe-partitions never fdisk-demo.img <<'FDISK'
d
2
p
w
FDISK

Confirm that only partition 1 remains in the demo image:

fdisk -l -o Device,Start,End,Sectors,Size,Type fdisk-demo.img

Relevant lines include the remaining partition row:

Device          Start    End Sectors  Size Type
fdisk-demo.img1  2048 264191  262144  128M Linux filesystem

Deleting an entry removes the map row, not every byte that used to live inside the old partition. Stop here if the deletion was accidental and you are trying to recover the old filesystem.

Clean Up the Demo Images

Remove only the practice images created in your home directory:

cd ~
rm -f -- fdisk-demo.img fdisk-mbr-demo.img fdisk-efi-demo.img

The -- marker stops option parsing before the filenames, and the target list is limited to the three demo images.

Use fdisk on a Real Disk Carefully

Writing a partition table can make existing data unreachable. Back up the disk first, identify the whole-disk path with lsblk, and quit with q if any prompt does not match the layout you intended.

Set a shell variable only after you have confirmed the target disk. The value should point to the whole disk, not a mounted partition:

disk=/dev/sdb
lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS "$disk"
sudo fdisk -l "$disk"

If any partition on that disk is mounted, unmount the exact partition path shown by lsblk. A SATA-style partition can look like this:

sudo umount /dev/sdb1

An NVMe partition uses a p before the partition number:

sudo umount /dev/nvme0n1p1

Open fdisk with an exclusive nonblocking lock so another tool does not edit the same device at the same time:

sudo fdisk --lock=nonblock "$disk"

Inside fdisk, print the table with p before making changes. Use g for a new GPT table on modern systems, o only when you intentionally need a DOS/MBR table, and n to add a partition. Size entries such as +512M, +20G, or +100MiB are easier to audit than raw sector numbers. Write only when the printed table matches the plan.

Reread the Partition Table After Writing

After writing changes, ask the kernel to refresh partition entries when the disk is not busy:

sudo partx -u "$disk"
lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS "$disk"

If the kernel cannot reread the table because a partition is mounted, used as swap, or held by another subsystem, cleanly stop the owner and retry. A reboot is the safer answer when the disk is the system disk or you cannot prove what is holding it.

Format New Partitions Only After Confirming the Paths

Formatting belongs after partitioning, and it targets a partition path, not the whole disk. Replace these example paths only after lsblk and fdisk -l show the intended new entries:

sudo mkfs.ext4 /dev/sdb1
sudo mkswap /dev/sdb2

Do not run mkfs or mkswap on a partition you are trying to recover. Partition type labels and filesystem signatures are separate: t changes the partition type, while mkfs.ext4 or mkswap writes new data structures inside the partition.

Troubleshoot Common fdisk Errors

Most fdisk failures come from the wrong path, insufficient permissions, mounted partitions, old signatures, or forgetting that changes stay in memory until w writes them.

fdisk Cannot Open the Target

A missing file or device path produces a direct error:

fdisk -l missing.img
fdisk: cannot open missing.img: No such file or directory

Check the path with lsblk for block devices or ls -lh for image files, then rerun fdisk against the exact whole-disk or image path.

fdisk Reports Permission Denied

Listing or editing protected paths without the needed privilege can fail like this:

fdisk -l /root/fdisk-demo.img
fdisk: cannot open /root/fdisk-demo.img: Permission denied

Use sudo only after confirming the target path. Do not widen permissions on system directories to make fdisk work.

The Target Is a Partition Instead of a Disk

Opening /dev/sdb1 or /dev/nvme0n1p1 edits the wrong layer for partition-table work. Confirm whether the path is a whole disk or a partition:

lsblk -o NAME,PKNAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS /dev/sdb /dev/sdb1

Use the row where TYPE is disk for fdisk table edits. Use the row where TYPE is part for filesystem commands such as mkfs, mount, or mkswap.

Changes Disappear After Quitting fdisk

fdisk keeps edits in memory until you write them. If you quit with q, close the terminal, or answer a prompt without writing, the on-disk partition table remains unchanged.

Reopen the target, print the table with p, repeat the intended changes, then use w only when the printed layout is correct. Verify afterward with fdisk -l.

The Device or Resource Is Busy

A busy-device error usually means a partition is mounted, used as swap, held by LVM, used by a filesystem tool, or watched by another process. Start with read-only checks, replacing /dev/sdb1 with the mounted partition shown by lsblk:

disk=/dev/sdb
lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS "$disk"
findmnt -S /dev/sdb1
swapon --show=NAME

Unmount active partitions, disable swap only when the partition is actually swap, and stop the owning service before retrying. If the partition table was written but the kernel could not reread it, use sudo partx -u "$disk" when safe or reboot when the disk is still in use.

fdisk Warns About Old Signatures

fdisk can warn when a partition contains an old filesystem, swap, RAID, or LVM signature. Preview the signatures before deciding what to do:

sudo wipefs -n /dev/sdb1

Choose the non-wipe path when you are recreating a lost partition entry and want to preserve the data inside it. Remove signatures only when the partition is intentionally being reused for a new filesystem or swap area.

The Partition Type Is Wrong

Partition types are metadata labels; they do not format the partition. If fdisk -l shows the wrong type, open the whole disk, use t, choose the partition number, list codes with L when needed, print with p, and write with w only after the row is correct.

A Deleted Partition Entry Needs Recovery

Deleting a partition entry with d removes the map entry; it does not immediately erase every byte inside the old partition. Stop writing to the disk if the deletion was accidental. Do not create a filesystem, do not run signature cleanup, and do not create a larger overlapping partition.

Recovery is safest from backups or a dedicated recovery tool. If you recorded the exact Start, End, and type before deletion, recreating the same entry without formatting may make the filesystem visible again, but guessing sector values can make the damage worse.

Conclusion

Safe fdisk work keeps inspection, planning, and writes separate: identify the whole disk with lsblk, inspect with fdisk -l, rehearse risky edits on an image, and commit only when the printed table matches the plan. Use GPT for modern systems unless a legacy compatibility requirement points to MBR.

Share this guide

Help another Linux user troubleshoot faster

Share this guide with someone troubleshooting Linux systems or saving it for later.

Follow LinuxCapable

Want more LinuxCapable guides in Google?

Add LinuxCapable as a preferred source so Google can show our tutorials more often in Top Stories and mark them as preferred in AI Mode and AI Overviews when relevant.

Add LinuxCapable as a preferred source on Google
Search LinuxCapable

Need another guide?

Search LinuxCapable for package installs, commands, troubleshooting, and follow-up guides related to what you just read.

Found this guide useful?

Support LinuxCapable to keep tutorials free and up to date.

Buy me a coffeeBuy me a coffee
Before commenting, please review our Comments Policy.
Formatting tips for your comment

You can use basic HTML to format your comment. Useful tags currently allowed in published comments:

You type Result
<code>command</code> command
<strong>bold</strong> bold
<em>italic</em> italic
<a href="https://example.com">link</a> link
<blockquote>quote</blockquote> quote block

Add to the discussion

Questions, fixes, command output, and version notes help keep this guide current.

Verify before posting: