Practise fdisk on a Disposable Image First

Type the wrong device name into fdisk and you can wipe a working disk in seconds. This walkthrough builds and verifies a GPT partition table inside a disposable 64 MiB image instead, so you can learn the whole workflow safely, then take it to real storage once every command is familiar.

Allow about fifteen minutes. You need a shell and the installed fdisk command from util-linux. The examples were checked against the local fdisk(8) manual for package version 2.39.3-9ubuntu6.6. The executable on this host reports util-linux 2.41.3, so run fdisk --help on your own machine when a version-specific detail matters.

Warning: Writing a partition table changes the target. The image below is disposable; a real disk command can make filesystems and operating systems inaccessible, and a mistaken device name can destroy the wrong disk. Do not swap the image path for /dev/sda, /dev/nvme0n1 or another live device until you have checked it several times.

1. Confirm the command and its version

Start with ordinary, read-only checks:

$ command -v fdisk
/usr/sbin/fdisk
$ fdisk --version
fdisk from util-linux 2.41.3

Your path and version may differ. The checkpoint that matters is that the command about to run is the expected system utility, not a script sharing its name.

2. Inspect a disk without changing it

Use -l to list partition tables and exit. With a named device, it examines only that device:

$ sudo fdisk -l /dev/DEVICE

Replace DEVICE with a real device name only after checking it. sudo may be required to read some block devices. This does not write a partition table, but its output is no substitute for identifying the disk physically or through your machine's inventory.

Omit the device and fdisk -l lists everything mentioned by /proc/partitions, which can be a lot of output that makes the disk you actually care about easy to miss. Prefer an explicit device for any decision that matters:

$ sudo fdisk -l /dev/DEVICE
Disk /dev/DEVICE: ...
Device          Start       End   Sectors  Size Type
/dev/DEVICE1    ...         ...   ...      ...  ...

Columns and values depend on the device. Treat the displayed name, size and partition count as a checkpoint before any later command.

3. Create a disposable image

Make a regular file in /tmp. fdisk can operate on a file, so no elevated privilege is needed:

$ IMAGE=$(mktemp /tmp/fdisk-guide.XXXXXX)
$ truncate -s 64M "$IMAGE"
$ ls -lh "$IMAGE"
-rw-r--r-- 1 ... 64M ... /tmp/fdisk-guide.img

If you adapt this example later, stop and inspect the value of IMAGE before running anything: the rm command at the end only removes the explicitly named temporary image. The file is not a block device and is not attached to the system as a disk.

Checkpoint: Confirm the path is the image, not a device:

$ file "$IMAGE"
/tmp/fdisk-guide.ABC123: data

An empty image may be described differently by file. Path and size are the checks that matter here.

4. Create a GPT table and one partition

Run fdisk interactively against the image:

$ fdisk "$IMAGE"

At the Command (m for help): prompt, enter these one at a time:

  1. g creates a new, empty GPT partition table.
  2. n adds a partition.
  3. Enter accepts the default partition number.
  4. Enter again accepts the default first sector.
  5. +32M answers the last-sector prompt.
  6. p prints the in-memory table so you can inspect it.
  7. w writes the table to the image and exits.

For a size, the manual recommends the relative + or - notation. The M suffix uses the binary MiB meaning fdisk accepts, and the resulting sectors get aligned to the device's I/O limits. Accepting the suggested first sector is normally the right call.

Destructive boundary: g only changes the in-memory plan, but w is the point that writes it. On a real disk, stop at p, review every line, and leave with q if anything looks wrong. Do not use w until the target and layout are certain.

5. Verify what was written

After w returns, ask fdisk to read the image afresh:

$ fdisk -l "$IMAGE"
Disk /tmp/fdisk-guide.img: 64 MiB, 67108864 bytes, 131072 sectors
Disklabel type: gpt
Device                  Start   End Sectors Size Type
/tmp/fdisk-guide.img1    2048 67583   65536  32M Linux filesystem

Exact spacing, identifier and human-readable size can vary with the util-linux version. What matters is a GPT label and one partition of about 32 MiB. The partition is only an entry in the image; no filesystem has been created yet.

For more detail, use -x:

$ fdisk -x "$IMAGE"

Do not treat normal fdisk output as a stable machine interface. The manual describes fdisk as mainly designed for people, and says output compatibility is not guaranteed. For scripts, look at partx(8) or sfdisk(8) instead.

Common traps

Recovery: If you made a mistake before writing, enter q to discard the in-memory changes. If you already wrote the disposable example, recovery is just removing the image:

$ rm -f /tmp/fdisk-guide.img

There is no equivalent safe undo for a partition table written to a real disk. Restore from a known backup, or recreate the intended table only after confirming what data must be preserved.

Done means