fdiskView and edit a disk's partition table
fdisk lists and edits partition tables, both GPT and the old MBR (DOS) format. It works interactively and changes nothing on disk until you type w, which makes it the classic tool for carving a new disk into partitions.
fdisk [options] DEVICE | fdisk -l [DEVICE...]You want to see the root disk's layout, partition types and exact sector positions before changing anything.
sudoReading a raw disk device needs root (the device node is owned by root:disk).
A partition table is a few sectors at the start of the disk. MBR (DOS) lives in sector 0: four primary entries with 32-bit sector numbers, so it can't address beyond 2 TiB. GPT puts a protective MBR in sector 0, a header in sector 1 and 128 partition entries in sectors 2-33, with a backup copy at the end of the disk and CRC32 checksums on both. fdisk opens the device, reads and validates the table, and keeps all your edits in memory. When you type w it writes the table, calls fsync, and asks the kernel to re-read it (the BLKRRPART ioctl, or BLKPG per partition). If a partition on that disk is mounted, the kernel can't drop it and keeps the old layout until partprobe, partx -u or a reboot. fdisk aligns new partitions to 1 MiB (sector 2048) so they line up with SSD and RAID boundaries. For scripts, use sfdisk or parted instead of feeding keystrokes to fdisk.
-lList partition tables and exit-xList with extra detail: partition UUIDs, attributes, GPT header info-t TYPEOnly recognise this table type (gpt, dos)-w always|never|autoWipe old filesystem/RAID signatures when creating a new table-u / --unitsShow positions in sectors (default) or cylindersg (interactive)Create a new empty GPT partition tablen (interactive)Add a new partitiont (interactive)Change a partition's type (Linux, swap, LVM, EFI...)d / p (interactive)Delete a partition / print the table in memoryw / q (interactive)Write changes and exit / quit without savingsudo fdisk /dev/nvme0n1p1fdisk works on whole disks. Pointing it at a partition shows an empty 'disk' inside it and invites you to write a table into your root filesystem.
sudo fdisk /dev/nvme0n1Using an MBR (DOS) table on a 4 TB diskMBR stores sector numbers in 32 bits, so anything past 2 TiB is unusable.
Type g in fdisk (or parted mklabel gpt) to create a GPT tableRunning mkfs right after fdisk reported 'Re-reading the partition table failed'The kernel still has the old layout, so /dev/nvme2n1p1 may have the old size or not exist yet.
sudo partprobe /dev/nvme2n1 (after unmounting), then check lsblkPiping keystrokes into fdisk in automationPrompts change between versions and a wrong default can destroy data.
echo 'label: gpt' ... | sudo sfdisk /dev/nvme2n1, or parted -s /dev/nvme2n1 mklabel gpt mkpart data 1MiB 100%Try these on a Linux machine of your own. Each task has a hint and a solution.
Create a 200 MB file, give it a GPT table with two partitions using sfdisk, then list it with fdisk. (Disk image files work without root.)
Dump the partition table of disk.img to a text file, as a backup.
List the partition table of disk.img, then write the command that lists every disk's partition table on a server. (The practice sandbox has no real disks.)
fdiskView and edit a disk's partition table
fdisk lists and edits partition tables, both GPT and the old MBR (DOS) format. It works interactively and changes nothing on disk until you type w, which makes it the classic tool for carving a new disk into partitions.
fdisk [options] DEVICE | fdisk -l [DEVICE...]You want to see the root disk's layout, partition types and exact sector positions before changing anything.
sudoReading a raw disk device needs root (the device node is owned by root:disk).
A partition table is a few sectors at the start of the disk. MBR (DOS) lives in sector 0: four primary entries with 32-bit sector numbers, so it can't address beyond 2 TiB. GPT puts a protective MBR in sector 0, a header in sector 1 and 128 partition entries in sectors 2-33, with a backup copy at the end of the disk and CRC32 checksums on both. fdisk opens the device, reads and validates the table, and keeps all your edits in memory. When you type w it writes the table, calls fsync, and asks the kernel to re-read it (the BLKRRPART ioctl, or BLKPG per partition). If a partition on that disk is mounted, the kernel can't drop it and keeps the old layout until partprobe, partx -u or a reboot. fdisk aligns new partitions to 1 MiB (sector 2048) so they line up with SSD and RAID boundaries. For scripts, use sfdisk or parted instead of feeding keystrokes to fdisk.
-lList partition tables and exit-xList with extra detail: partition UUIDs, attributes, GPT header info-t TYPEOnly recognise this table type (gpt, dos)-w always|never|autoWipe old filesystem/RAID signatures when creating a new table-u / --unitsShow positions in sectors (default) or cylindersg (interactive)Create a new empty GPT partition tablen (interactive)Add a new partitiont (interactive)Change a partition's type (Linux, swap, LVM, EFI...)d / p (interactive)Delete a partition / print the table in memoryw / q (interactive)Write changes and exit / quit without savingsudo fdisk /dev/nvme0n1p1fdisk works on whole disks. Pointing it at a partition shows an empty 'disk' inside it and invites you to write a table into your root filesystem.
sudo fdisk /dev/nvme0n1Using an MBR (DOS) table on a 4 TB diskMBR stores sector numbers in 32 bits, so anything past 2 TiB is unusable.
Type g in fdisk (or parted mklabel gpt) to create a GPT tableRunning mkfs right after fdisk reported 'Re-reading the partition table failed'The kernel still has the old layout, so /dev/nvme2n1p1 may have the old size or not exist yet.
sudo partprobe /dev/nvme2n1 (after unmounting), then check lsblkPiping keystrokes into fdisk in automationPrompts change between versions and a wrong default can destroy data.
echo 'label: gpt' ... | sudo sfdisk /dev/nvme2n1, or parted -s /dev/nvme2n1 mklabel gpt mkpart data 1MiB 100%Try these on a Linux machine of your own. Each task has a hint and a solution.
Create a 200 MB file, give it a GPT table with two partitions using sfdisk, then list it with fdisk. (Disk image files work without root.)
Dump the partition table of disk.img to a text file, as a backup.
List the partition table of disk.img, then write the command that lists every disk's partition table on a server. (The practice sandbox has no real disks.)