VMware ESXi and vSphere Cluster Management
Using the fdisk Utility to List, Create, Delete, and Change Linux Partitions
Learn how to safely use Linux fdisk to inspect disks, create or delete partitions, change partition types, save or discard changes, and complete post-partition tasks.
fdisk is an interactive, text-based Linux utility for viewing and editing a disk's partition table. It is useful for inspecting storage layouts and defining partition boundaries, but partitioning is a destructive administrative task. Confirm the target device before making changes, maintain current backups, and never write a table you have not reviewed.
This lesson uses /dev/sdb as an example target. Replace it only after verifying that it is the intended disk. For background on this command, see the fdisk utility guide.
What fdisk Changes
A block device is a device file representing storage that Linux reads and writes in blocks, commonly under /dev. A disk device represents the complete storage device, such as /dev/sda, /dev/sdb, or /dev/nvme0n1.
A partition is a defined range of sectors on a disk. Its boundaries and characteristics are recorded in the partition table, which is disk metadata describing where partitions begin and end and what they are intended for.
For example, /dev/sda is a complete disk, while /dev/sda1 is its first partition. On an NVMe disk, /dev/nvme0n1 is the complete disk and /dev/nvme0n1p1 is its first partition. The p separates the disk name from the partition number because the disk name already ends in a digit.
fdisk changes partition-table metadata. Creating a partition does not create a filesystem, place files on it, or mount it. Those are separate operations:
- Partitioning: define a sector range and partition type.
- Formatting: create a filesystem such as ext4 or XFS inside that range.
- Mounting: attach the filesystem to a directory in the Linux directory tree.
- Swap setup: initialize a partition with
mkswapand activate it withswapon.
Current Linux versions of fdisk can work with both traditional MBR/DOS partition tables and GPT partition tables. Some tasks are better handled by specialized tools, such as GNU Parted for certain scripted or resizing workflows, or graphical partition managers when visual confirmation is important.
Safety and Preparation
- Identify the disk by its size, model, serial information when available, existing partitions, filesystems, and mount points.
- Do not assume that
/dev/sdais an external or disposable disk. Device names depend on discovery order and system configuration. - fdisk normally requires root privileges, supplied with
sudo. - Avoid editing mounted partitions, partitions actively used by applications, or partitions serving as swap.
- Prepare a live environment, maintenance window, or other appropriate method when the target contains system data.
- Remember that fdisk holds changes in memory during the interactive session. Nothing is committed until you use
w, butwwrites the new table to disk.
Before opening fdisk, inspect the available devices:
lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS
Typical output distinguishes whole disks from partitions and shows whether a partition is mounted. You can also inspect all detected partition tables:
sudo fdisk -l
To inspect one disk without entering the editor, specify the whole-disk path:
sudo fdisk -l /dev/sdb
Do not normally pass an individual partition such as /dev/sdb1 when you intend to edit the disk's partition table. Open fdisk against the complete disk:
sudo fdisk /dev/sdb
Linux Disk and Partition Names
Understanding the Interactive Interface
After starting fdisk, it displays information about the selected disk and presents a prompt similar to Command (m for help):. Commands are usually entered as single letters. Some commands then ask follow-up questions, such as a partition number or a first and last sector.
Display the available commands at any time with:
m
Before and after planned changes, print the current in-memory partition table:
p
The printed table commonly includes the partition device name, partition number, start sector, end sector, total sectors, size, type, and sometimes a boot-related marker. A sector is an addressable unit used to define partition boundaries. The start and end sectors show exactly which range belongs to each partition.
A boot marker is metadata associated with boot-related partitioning conventions. Its meaning depends on the partition-table style and firmware or bootloader configuration; do not treat it alone as proof that a partition contains a bootable operating system.
Listing and Reading the Partition Table
Use p inside fdisk to view the selected disk's current table:
sudo fdisk /dev/sdb
Command (m for help): p
A simplified table might resemble this:
Device Start End Sectors Size Type
/dev/sdb1 2048 20973567 20971520 10G Linux filesystem
/dev/sdb2 20973568 23070719 2097152 1G Linux swap
- Device: the partition path, such as
/dev/sdb1. - 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 partition type code or named identifier.
- Boot-related marker: an optional flag shown by some tables and fdisk versions.
Review free space before creating anything. Confirm that the proposed start and end sectors fall in unused space, do not overlap an existing partition, and do not unintentionally change existing partition numbers.
Common fdisk Interactive Commands
Creating a Partition
Use n to create a partition in unused space:
sudo fdisk /dev/sdb
Command (m for help): n
Depending on the existing partition table and fdisk version, the prompts can ask for:
- Partition kind: MBR may offer primary or extended partitions. GPT generally does not use that distinction.
- Partition number: choose an unused number, or accept the suggested default after checking existing numbering.
- First sector: accepting the suggested value usually preserves alignment and starts the partition at the beginning of the selected free range.
- Last sector or size: accept the default to use the available range, or enter a size such as
+10G.
Alignment means choosing partition boundaries that suit modern storage geometry and performance expectations. Unless you have a specific layout requirement, accepting fdisk's aligned default first sector is usually preferable. A size such as +10G requests approximately ten gigabytes from the selected start sector.
A new partition must fit inside contiguous free space. It cannot overlap an existing partition. If fdisk reports that a range is invalid or unavailable, print the table again and inspect the actual gaps between partitions.
Partition types may be selected separately with t, depending on the partition-table style and fdisk version. Creating a partition does not format it.
For a disposable example disk, the overall interaction may look like this:
sudo fdisk /dev/sdb
Command (m for help): n
# Follow the prompts; accept an aligned start and enter a suitable size, such as +10G.
Command (m for help): p
# Inspect the new in-memory entry.
Command (m for help): w
The exact prompts and default values depend on the disk's current layout, partition-table style, and fdisk version.
Deleting a Partition
Use d and then select the intended partition number:
sudo fdisk /dev/sdb
Command (m for help): d
# Enter the partition number when prompted.
Command (m for help): p
Deletion removes the partition-table entry. It does not necessarily overwrite every byte in the former partition immediately, but the operating system no longer has the same partition definition. The data can become inaccessible, and creating a new partition or writing new data can make recovery difficult.
Always verify the selected number and print the revised table before writing. To demonstrate safely abandoning a pending deletion:
sudo fdisk /dev/sdb
Command (m for help): d
# Select the unwanted partition number.
Command (m for help): p
# Review the pending table.
Command (m for help): q
Because the example ends with q, the deletion is discarded. Use w only when the selected partition and final layout are correct.
Changing a Partition Type
A partition type is a code or identifier describing the intended purpose of a partition. Use t to select a partition and assign an appropriate type:
sudo fdisk /dev/sdb
Command (m for help): t
# Select the partition number when prompted.
# Enter the requested type code or choose a named type.
Command (m for help): p
When choosing a type, use l where fdisk requests a type to list recognized values:
Command (m for help): t
Command (m for help): l
On MBR terminology, 0x83 is the historical Linux filesystem type used for standard Linux filesystem partitions. Linux swap has a separate type. Other operating systems and special-purpose partitions also have their own identifiers.
GPT does not rely only on legacy hexadecimal MBR codes. It uses named or GUID-based partition type identifiers, such as a Linux filesystem or Linux swap type. The displayed selection and accepted input can therefore differ between MBR and GPT.
Changing a type does not create the intended content. For example, marking a partition as Linux swap does not initialize swap space; run mkswap afterward.
MBR/DOS and GPT
MBR/DOS is a legacy partition-table format. It has primary partitions and can use an extended partition containing logical partitions to work around its primary-partition organization. It also has practical disk-size and partition-count limitations.
GPT, or GUID Partition Table, is the modern format used on most current systems. It supports larger disks, many more partitions, and named GUID-based partition types. Firmware, boot mode, operating-system support, and deployment requirements should be considered before choosing a table style.
Saving or Discarding Changes
fdisk keeps edits in memory during the session. Use p to print the final proposed layout, then compare it with your plan. Confirm the disk name, partition numbers, start and end sectors, sizes, and types.
Write the revised table with:
Command (m for help): w
Quit without writing pending changes with:
Command (m for help): q
After a successful write, Linux may automatically reread the partition table. If new partitions do not appear immediately, ask the kernel to reread it:
sudo partprobe /dev/sdb
If the disk is busy, the kernel may refuse to reread the table while partitions are mounted, used by applications, or active as swap. Unmount or deactivate those uses only when safe. A reboot may be required when the kernel cannot safely reload the table.
Post-Partition Tasks
Format a Filesystem Partition
A newly created data partition generally needs a filesystem before it can store ordinary files. Formatting destroys existing content on the selected partition, so verify the device path again:
sudo mkfs.ext4 /dev/sdb1
The command above creates an ext4 filesystem. Choose a filesystem and creation tool appropriate to the operating system and workload.
Mount the Filesystem
A mount point is a directory where Linux attaches a filesystem to the directory tree:
sudo mkdir -p /mnt/data
sudo mount /dev/sdb1 /mnt/data
findmnt /mnt/data
For a mount that should survive reboots, use a stable UUID in /etc/fstab rather than depending on a potentially changing device name. Obtain the UUID with blkid:
sudo blkid /dev/sdb1
An example /etc/fstab pattern is:
UUID=<filesystem-uuid> /mnt/data ext4 defaults 0 2
Initialize and Activate Swap
Linux swap is disk space initialized for virtual-memory use. After assigning the appropriate swap partition type, initialize and activate the partition:
sudo mkswap /dev/sdb2
sudo swapon /dev/sdb2
swapon --show
A persistent swap entry can use the swap UUID:
UUID=<swap-uuid> none swap sw 0 0
Verify the Result
Check the layout and content after partitioning:
lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS
sudo fdisk -l /dev/sdb
sudo blkid
findmnt /mnt/data
swapon --show
Complete Safe Workflow
- Back up important data and make sure the disk can be taken out of active use.
- Identify the target with
lsblk, comparing device size, partitions, filesystems, and mount points. - Inspect the table with
sudo fdisk -l /dev/sdb. - Open fdisk against the whole disk:
sudo fdisk /dev/sdb. - Use
pto record the starting layout. - Use
n,d, ortas required, carefully answering each prompt. - Use
pagain and verify every pending change. - Use
qif anything is wrong or uncertain; usewonly to commit the verified table. - Run
partprobeor reboot if the kernel does not reread the table. - Format a filesystem partition or initialize swap as appropriate.
- Mount the filesystem or activate swap.
- Verify the final state and add UUID-based persistent configuration to
/etc/fstabwhen required.
Troubleshooting
The Wrong Disk Might Be Selected
Stop before using w if the device contains expected system partitions, mounted filesystems, or a size that does not match the intended target. Compare lsblk output, device sizes, mount points, and filesystem information. Use q to discard any in-memory edits.
The Device Is Busy or the Kernel Cannot Reread the Table
Check whether partitions on the disk are mounted, being used by applications, or active as swap. Unmount or deactivate them only when safe, then run:
sudo partprobe /dev/sdb
If the kernel still cannot reload the table, schedule a reboot after ensuring the new layout is correct.
A New Partition Cannot Be Mounted
If mount reports an unknown filesystem, a bad superblock, or a missing filesystem, verify that the partition exists with lsblk or fdisk -l. A partition created by fdisk may still be unformatted. Create the intended filesystem with the appropriate mkfs command, then mount the partition device, not the entire disk device.
The Requested Size or Start Sector Is Rejected
Print the table and identify actual contiguous free space. Accept the suggested aligned starting sector when appropriate, and choose a last sector or size that fits completely within the free range. A partition cannot overlap an existing partition.
The Partition Has the Wrong Intended Use
Use t and the type list to assign the appropriate type for the selected MBR or GPT table. Then perform the purpose-specific follow-up operation. For example, a swap type still requires mkswap before swapon.
Changes Should Not Be Retained
Use p to inspect the pending table. If it is incorrect and has not been written, use q instead of w. Once a changed table has been written, recovery can be difficult, particularly after new data is written to the affected area.
When to Choose Another Tool
fdisk is a strong interactive tool for inspecting and editing partition tables, but it is not the only choice. GNU Parted may be more suitable for scripted workflows or certain partition-management operations. Graphical tools can be preferable when a visual layout makes it easier to confirm free space, boundaries, and pending operations. LVM and filesystem-specific tools are appropriate for volume management and resizing after the underlying partitions have been prepared.