How to partition with fdisk, gdisk & parted
This tutorial shows how to partition storage drives from the command line using fdisk, gdisk, and parted. The walkthrough creates a UEFI/GPT layout on an NVMe drive with a 1 GB EFI System Partition, a 16 GB swap partition, and an Ext4 root partition. For flexible volume management on top of these partitions, LVM can layer logical volumes that resize without repartitioning.
Sector alignment: the 1MiB (2048-sector) rule
Standard partitioning tools automatically align partition start sectors to 1 Megabyte (2048 sectors on 512-byte emulation drives). This aligns partition boundaries with underlying 4096-byte (4Kn) physical disk sectors and internal NAND Flash erase blocks, eliminating read-modify-write performance penalties. For the background on logical block addressing and 4Kn Advanced Format, see the partition mapping and geometry guide.
Walkthrough: creating a UEFI/GPT layout with fdisk
In this example, we configure an NVMe drive (/dev/nvme0n1) with a 1GB EFI System Partition (ESP), a 16GB Swap partition, and an Ext4 Root partition utilizing the remaining storage.
The numeric type codes used below (e.g., 1, 19, 23) are positional indices in fdisk's type list and shift between util-linux releases as new partition types are added. For stable, version-independent input, prefer fdisk's string aliases: uefi, swap, linux, home, xbootldr, or the full GUID directly.
1. Open the Device in fdisk
sudo fdisk /dev/nvme0n12. Create a New GPT Partition Table
Enter g to initialize a new GPT disklabel:
Command (m for help): g
Created a new GPT disklabel (GUID: 8C6B1B8A-70E2-4C77-96A1-94E51F729221).3. Create the EFI System Partition (ESP)
Command (m for help): n
Partition number (1-128, default 1): 1
First sector (2048-2000409262, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-2000409262, default 2000409262): +1G
Created a new partition 1 of type 'Linux filesystem' and of size 1 GiB.
Command (m for help): t
Selected partition 1
Partition type or alias (type L to list all, or hex code): 1
Changed type of partition to 'EFI System'.4. Create the Swap Partition
Command (m for help): n
Partition number (2-128, default 2): 2
First sector (2099200-2000409262, default 2099200):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2099200-2000409262, default 2000409262): +16G
Created a new partition 2 of type 'Linux filesystem' and of size 16 GiB.
Command (m for help): t
Partition number (1,2, default 2): 2
Partition type or alias (type L to list all, or hex code): 19
Changed type of partition to 'Linux swap'.5. Create the Root Filesystem Partition
Command (m for help): n
Partition number (3-128, default 3): 3
First sector (35653632-2000409262, default 35653632):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (35653632-2000409262, default 2000409262):
Created a new partition 3 of type 'Linux filesystem' and of size 937.5 GiB.
Command (m for help): t
Partition number (1-3, default 3): 3
Partition type or alias (type L to list all, or hex code): 23
Changed type of partition to 'Linux root (x86-64)'.6. Verify and Commit Changes
Enter p to inspect the layout, then w to write the partition table to disk:
Command (m for help): p
Disk /dev/nvme0n1: 953.87 GiB, 1024209543168 bytes, 2000409264 sectors
Disk model: Samsung SSD 990 PRO 1TB
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Device Start End Sectors Size Type
/dev/nvme0n1p1 2048 2099199 2097152 1G EFI System
/dev/nvme0n1p2 2099200 35653631 33554432 16G Linux swap
/dev/nvme0n1p3 35653632 2000409230 1964755599 937.5G Linux root (x86-64)
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.Formatting the new partitions
# Format EFI System Partition (FAT32)
sudo mkfs.vfat -F 32 -n "EFI-SYSTEM" /dev/nvme0n1p1
# Initialize and activate swap
sudo mkswap -L "SWAP_SPACE" /dev/nvme0n1p2
sudo swapon /dev/nvme0n1p2
# Format root partition with Ext4
sudo mkfs.ext4 -L "ROOT_FS" /dev/nvme0n1p3After formatting, add the new filesystems to /etc/fstab using their UUIDs or volume labels so mounts survive device reordering.
Sources & references
- fdisk(8) — Linux manual page — documents fdisk for GPT/MBR partition table manipulation, disklabel creation (g command), and automatic 4K-sector alignment
- gdisk(8) — GPT fdisk Manual — documents gdisk as a GPT-specific partition manipulator with fdisk-like interface
- parted(8) — Linux manual page — documents parted's script mode (--script) and mkpart command for non-interactive partition creation
- mkswap(8) — Linux manual page — documents mkswap command for setting up Linux swap areas on partitions or files
- swapon(8) — Linux manual page — documents swapon command for enabling swap devices and files
Frequently asked questions
How do I create a GPT partition table with fdisk?
Open the device with sudo fdisk /dev/nvme0n1, then press g to initialize a new GPT disklabel. Use n to create partitions, t to set partition types, p to verify, and w to write changes to disk.
What is the 1 MiB sector alignment rule?
Partitioning tools automatically align partition start sectors to 1 MiB (2048 sectors on 512-byte drives). This aligns boundaries with 4Kn physical sectors and NAND Flash erase blocks, eliminating read-modify-write penalties.
How do I set the EFI System Partition type in fdisk?
After creating the partition, press t to change its type. Use the alias uefi or the numeric code 1 to set it to EFI System. For stable input across util-linux versions, prefer the string alias.
How do I format partitions after creating them?
Use mkfs.vfat -F 32 for the ESP, mkswap for swap partitions, and mkfs.ext4 for root. For example: sudo mkfs.ext4 -L "ROOT_FS" /dev/nvme0n1p3.
Can I partition non-interactively with parted?
Yes. Use parted --script -a optimal with mklabel and mkpart commands in a single invocation. Note that mkpart sets the partition type GUID but does not create the filesystem; run mkfs.* separately.
What fdisk type code creates a Linux swap partition?
Use the alias swap or the numeric code 19 in fdisk to set a partition type to Linux swap. In gdisk, the shortcut is 8200. The numeric codes shift between util-linux releases, so prefer string aliases.