21.2 fdisk: MBR Partition Management
Alright, let’s get our hands dirty with fdisk, the cantankerous old wizard of partition management. It’s been around since the dawn of time (or 1983, which is basically the same thing in computer years) and it’s still the go-to tool for wrangling MBR (Master Boot Record) partition tables. Forget the shiny graphical tools for a moment; this is where the real control is, and it’s all done through a delightfully archaic, text-based interface. Don’t worry, I’ll be your guide.
First, a crucial reality check: fdisk is a sledgehammer. It doesn’t just move data around; it modifies the very low-level data structures that tell your operating system what storage devices even exist. This means one wrong move can turn your precious data into a digital wasteland.
Rule Zero: Back up anything you cannot afford to lose. I’m not joking. If you have a single byte of data on that disk you care about, it needs to be somewhere else before you even think about running fdisk. Got it? Good. Let’s proceed.
The First Command: Picking Your Weapon
You’ll need to run fdisk with superuser privileges. The first step is always identifying the correct disk. Linux disks are typically /dev/sdX for SATA/NVMe/USB or /dev/hdX for older IDE, where X is a letter.
To list your disks, use:
sudo fdisk -l
The output will look something like this:
Disk /dev/sda: 232.9 GiB, 250059350016 bytes, 488397168 sectors
Disk model: Samsung SSD 850
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: dos
Disk identifier: 0x738a4738
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 1050623 1048576 512M 83 Linux
/dev/sda2 1050624 488397167 487346544 232.4G 83 Linux
See the Disklabel type: dos? That’s fdisk-speak for “MBR.” Now, let’s say we want to partition a new disk, /dev/sdb. We open it for surgery:
sudo fdisk /dev/sdb
Welcome to the fdisk prompt. It doesn’t look like much, just a waiting Command (m for help):. Type m and press enter to see the menu of commands. Don’t be intimidated; we only need a handful of these.
The Core Commands: n, p, d, w, q
Let’s break down the essentials:
p: Print the partition table. Do this constantly. It’s your ground truth.n: Add a new partition. This is where you’ll specify primary/extended/logical and the start and end sectors.d: Delete a partition. Simple and terrifyingly effective.w: Write the new partition table to disk and exit. This is the point of no return. Only do this when you are 1000% sure.q: Quit without saving any changes. Your get-out-of-jail-free card. Use it if you get confused or make a mistake.
Creating Your First Partition
Let’s walk through creating a single primary partition that uses the whole disk. After typing n for new, you’ll get a conversation with fdisk:
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-500118158, default 2048): [Enter]
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-500118158, default 500118158): [Enter]
Why the default first sector of 2048? It’s a holdover from the dark ages of spinning rust. This 1MiB offset ensures partitions are aligned to physical sector boundaries for optimal performance, especially on modern Advanced Format drives (which have 4KiB physical sectors). Just hit enter; the designers got this one right.
For the last sector, hitting enter accepts the default, which is the last available sector, making one big partition. You could also specify a size, like +20G to make a 20-gigabyte partition.
The Partition Type Id Quirk
Here’s a classic fdisk quirk. After creating a partition, it will default to type 83 Linux. That’s fine for most things, but if you’re creating a swap partition (82 Linux swap), or a partition that will be used by another OS, you need to change it. Use the t command.
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'.
Why is this still a thing? Because MBR is from an era where we needed to label a partition for “OS/2 Boot Manager” (hex a0) and “Plan 9” (hex 39). It’s a museum of forgotten operating systems. The L command to list them all is a fun little history lesson, but utterly useless for modern work. Just remember 83 for Linux, 82 for swap, and ef for EFI System Partition (if you’re mixing MBR and UEFI, which is its own special kind of madness).
The Final, Fateful Step
You’ve created, deleted, and tweaked. The p command shows a partition table that looks perfect. Now you must choose: w or q.
If you’re ready, you type w and hit enter. The tool will ominously say “The partition table has been altered.” and then “Calling ioctl() to re-read partition table.” If you’re partitioning the disk your operating system is running from, it might tell you to reboot. If it’s an external disk, you can usually force the kernel to re-read the table with partprobe or udevadm settle.
And that’s it. You’ve just used a tool older than most of its users to bend raw storage to your will. It’s clunky, it’s unforgiving, but it’s precise and it works everywhere. That’s power. Now go make a filesystem on your new partition with mkfs. But that’s a story for another section.