LFCS 74 🐧 Partitioning with fdisk
fdisk is the traditional interactive partition editor for Linux, and it remains the tool the LFCS exam expects you to know for MBR disks. Modern versions of fdisk from util-linux also handle GPT, which means a single tool can inspect and modify both table types, but the menu flow and the partition-numbering rules differ between them. The exam objective is not memorizing every key; it is knowing the small set of commands that create, delete, change type, and write the table, and understanding that changes are staged in memory until you explicitly commit them .
Key point: fdisk does not apply changes as you type them. Every operation modifies an in-memory copy of the partition table. Pressing w writes that copy to disk; pressing q discards it. This is the safety mechanism that makes fdisk less dangerous than parted, which commits each command immediately .
Why fdisk remains relevant
The MBR workflow problem. GPT is the better choice for modern hardware, and the manual page says so explicitly . But many exam environments and legacy systems still use MBR, and the primary/extended/logical distinction only exists in MBR. fdisk is the tool that exposes that distinction clearly: the n command asks whether you want a primary or extended partition, and logical partitions are created inside an extended container. A tool that hides this distinction, like parted, makes the MBR workflow harder to reason about.
The inspection problem. fdisk -l is the standard way to list partitions on a device. The output shows the partition number, boot flag, start and end sectors, size, type ID, and type name. This is the information needed to verify a layout before formatting or mounting. The lsblk command provides a tree view, but fdisk -l provides the partition type IDs and the disk label type, which lsblk does not show by default.
The scriptability problem. fdisk is interactive, which makes it unsuitable for automation. But the manual page notes that fdisk can read sfdisk-compatible script files with the I command, apply them to the in-memory table, and then allow inspection before writing . This means the interactive tool can be used to review a scripted layout before committing it. For the exam, the interactive flow is what matters, but the script compatibility explains why fdisk has survived in automated provisioning pipelines.
a. Inspecting and Starting fdisk
The first step is always identifying the correct device. fdisk -l lists all disks and their partitions without modifying anything. The output includes the disk label type, which tells you whether the disk is MBR (dos) or GPT .
# List all disks and partitions without modifying anything
sudo fdisk -l
# Inspect a specific disk
sudo fdisk -l /dev/sdb
The output for an MBR disk shows partitions numbered 1 through 4 for primaries and 5 and above for logicals. The Id column shows the partition type code: 83 for Linux, 82 for swap, 8e for LVM . For a GPT disk, the output shows the partition type GUID and name rather than a hex ID.
Starting fdisk on a device opens the interactive menu. The prompt Command (m for help): is where all operations begin.
# Open the disk for interactive editing
sudo fdisk /dev/sdb
Typing m displays the help menu, which lists every available command. The commands that matter for the exam are n (new), d (delete), t (type), p (print), w (write), and q (quit). The p command is not optional before writing; it shows the in-memory table so you can verify that the operations produced the intended layout.
b. Creating Partitions
The n command creates a new partition. On an MBR disk, the first prompt asks whether the partition is primary or extended. A primary partition consumes one of the four slots in the MBR table. An extended partition is a container that holds logical partitions, and it also consumes one of the four slots .
# Create a primary partition
# n → p → 1 → <Enter> for default start → +2G for size
The partition number prompt defaults to the next available slot. For a primary partition, the valid range is 1 through 4. For a logical partition, the numbering starts at 5, and fdisk automatically selects the next logical number inside the extended partition .
# Create an extended partition
# n → e → 2 → <Enter> → <Enter> (use remaining space)
# Create a logical partition inside the extended partition
# n → l → <Enter> → +1G
The size can be specified in sectors or with a size suffix. The recommended notation is +size{K,M,G,T,P}, which allocates the specified size aligned to the device’s I/O limits . The older suffixes without the i (like MB instead of MiB) are deprecated but still accepted.
On a GPT disk, the n command does not ask for primary or extended. All GPT partitions are primary, and the partition number can be any value from 1 to 128 (the tool’s default limit, though the GPT specification itself allows more) . The type is selected with a GUID or a type alias, and fdisk provides a list with L.
c. Deleting, Changing Type, and Writing
The d command deletes a partition. It prompts for the partition number, and the deletion takes effect in the in-memory table immediately. The partition is not removed from disk until w is pressed.
# Delete partition 2
# d → 2
The t command changes the partition type. On an MBR disk, it prompts for the partition number and then a hex code. Typing L lists all available codes. The common codes are 83 for Linux, 82 for swap, and 8e for LVM . On a GPT disk, the prompt asks for a type alias or GUID instead of a hex code.
# Change partition 2 to Linux swap (MBR)
# t → 2 → 82
The w command writes the in-memory table to disk and exits. The q command exits without writing, discarding every change made in the session. This distinction is the most important safety property of fdisk: an accidental deletion or type change can be abandoned by pressing q instead of w.
# Write the table and exit
# w
# Or discard all changes and exit
# q
After writing, the kernel may not re-read the partition table automatically. The partprobe command forces a re-read, or a reboot achieves the same result . If the disk is in use—for example, if a partition on it is mounted—the kernel may refuse to update the table until the device is released.
Complete Example Session
# ============================================
# PART 1: IDENTIFY THE DISK
# ============================================
# List all disks and their table types
sudo fdisk -l /dev/sdb
# Look for "Disklabel type: dos" (MBR) or "gpt"
# ============================================
# PART 2: OPEN FDISK
# ============================================
sudo fdisk /dev/sdb
# Prompt: Command (m for help):
# ============================================
# PART 3: VIEW THE CURRENT TABLE
# ============================================
# p
# Shows existing partitions, their sizes, and type IDs
# ============================================
# PART 4: CREATE A PRIMARY PARTITION
# ============================================
# n
# p (primary)
# 1 (partition number)
# <Enter> (default first sector)
# +2G (size)
# ============================================
# PART 5: CREATE AN EXTENDED PARTITION
# ============================================
# n
# e (extended)
# 2 (partition number)
# <Enter> (default start)
# <Enter> (use remaining space)
# ============================================
# PART 6: CREATE A LOGICAL PARTITION
# ============================================
# n
# l (logical, inside the extended partition)
# <Enter> (default start)
# +1G (size)
# fdisk assigns the next logical number (5)
# ============================================
# PART 7: CHANGE A PARTITION TYPE
# ============================================
# t
# 5 (partition number)
# 82 (Linux swap)
# L (lists all type codes if needed)
# ============================================
# PART 8: VERIFY BEFORE WRITING
# ============================================
# p
# Confirm the layout: sdb1 (83), sdb2 (extended), sdb5 (82)
# ============================================
# PART 9: WRITE THE TABLE
# ============================================
# w
# The table is written and fdisk exits
# ============================================
# PART 10: REFRESH THE KERNEL
# ============================================
sudo partprobe /dev/sdb
# Or reboot if the device is busy
The ten parts trace the full interactive workflow. Part 8 is the step that prevents mistakes: the p command before w shows exactly what will be written.
Quick Reference
Essential fdisk Commands
| Key | Action |
|---|---|
m | Display help menu |
p | Print current partition table |
n | Create new partition |
d | Delete partition |
t | Change partition type |
w | Write table and exit |
q | Quit without writing |
MBR Partition Types
| Code | Meaning |
|---|---|
83 | Linux native |
82 | Linux swap |
8e | Linux LVM |
5 / f | Extended partition |
Size Suffixes
| Suffix | Meaning |
|---|---|
K | KiB (1024 bytes) |
M | MiB |
G | GiB |
T | TiB |
+size | Aligned to device I/O limits |
MBR vs. GPT in fdisk
| Aspect | MBR (dos) | GPT |
|---|---|---|
| Init command | o | g |
| Partition limit | 4 primary | 128 default |
| Extended/logical | Yes | No |
| Type selection | Hex code | GUID or alias |
Best Practices
✅ Do This:
# Inspect the disk before modifying it
sudo fdisk -l /dev/sdb # ✅
# Use p to verify the in-memory table before writing
p # ✅
# Use q to abandon changes if the layout is wrong
q # ✅
# Use +size{K,M,G} for aligned partition sizes
+2G # ✅
# Refresh the kernel table after writing
sudo partprobe /dev/sdb # ✅
❌ Don’t Do This:
# Write without verifying the layout
w # after n, without p first # ❌
# Use MBR on a disk larger than 2 TB
# The space beyond 2 TB is unusable # ❌
# Use MBR for a UEFI boot disk
# The firmware cannot boot from it # ❌
# Assume changes are applied immediately
# They are not written until w is pressed # ❌
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Fifth primary partition fails | MBR four-slot limit | Create an extended partition, use logical |
| Changes disappear after exit | Pressed q instead of w | Press w to write |
| Kernel does not see new partition | Table not re-read | partprobe /dev/sdX |
| Partition type is wrong | Default type used | t to change the type code |
| Disk larger than 2 TB shows 2 TB | MBR table | Use GPT (g in fdisk, or gdisk) |
Real-World Examples
1. List Partitions
sudo fdisk -l /dev/sdb
2. Open fdisk
sudo fdisk /dev/sdb
3. Create Primary Partition
n → p → 1 → <Enter> → +2G
4. Create Extended Partition
n → e → 2 → <Enter> → <Enter>
5. Create Logical Partition
n → l → <Enter> → +1G
6. Delete Partition
d → 2
7. Change Type to Swap
t → 5 → 82
8. Change Type to LVM
t → 3 → 8e
9. Write Changes
w
10. Discard Changes
q
Visual
The fdisk In-Memory Table
┌──────────────────────────────────────────────────────────────┐
│ DISK /dev/sdb │
│ │
│ ┌─────────────────────────────────────────────────────────┐│
│ │ IN-MEMORY PARTITION TABLE (not yet written) ││
│ │ ││
│ │ Partition 1: primary, +2G, type 83 ││
│ │ Partition 2: extended, rest of disk ││
│ │ Partition 5: logical, +1G, type 82 ││
│ │ ││
│ │ Operations modify this copy only. ││
│ └─────────────────────────────────────────────────────────┘│
│ │ │
│ │ w │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────┐│
│ │ ON-DISK PARTITION TABLE ││
│ │ (the real table, read by the kernel at boot) ││
│ └─────────────────────────────────────────────────────────┘│
│ │
│ q discards the in-memory copy without writing. │
│ │
└──────────────────────────────────────────────────────────────┘
MBR Partition Creation Flow
┌──────────────────────────────────────────────────────────────┐
│ n (new partition) │
│ │ │
│ ▼ │
│ ┌─────────────────┐ │
│ │ Primary or │ │
│ │ extended? │ │
│ └────────┬────────┘ │
│ │ │
│ ┌─────┴─────┐ │
│ ▼ ▼ │
│ ┌──────┐ ┌──────┐ │
│ │ p │ │ e │ │
│ │ 1-4 │ │ 1-4 │ │
│ └──┬───┘ └──┬───┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌──────┐ ┌─────────────────────┐ │
│ │ Data │ │ Extended container │ │
│ │ │ │ (n → l for logical) │ │
│ └──────┘ └─────────────────────┘ │
│ │
│ Primary slots are 1-4. Logical partitions start at 5. │
│ │
└──────────────────────────────────────────────────────────────┘
Type Code Selection
┌──────────────────────────────────────────────────────────────┐
│ t (change type) │
│ │ │
│ ▼ │
│ ┌─────────────────┐ │
│ │ Partition │ │
│ │ number? │ │
│ └────────┬────────┘ │
│ ▼ │
│ ┌─────────────────┐ │
│ │ Hex code or L │ L lists all codes │
│ └────────┬────────┘ │
│ ▼ │
│ ┌─────────────────┐ │
│ │ 83 = Linux │ │
│ │ 82 = Swap │ │
│ │ 8e = LVM │ │
│ └─────────────────┘ │
│ │
│ The type is a hint to tools and the OS, │
│ not an enforced constraint. │
│ │
└──────────────────────────────────────────────────────────────┘
Write vs. Quit
┌──────────────────────────────────────────────────────────────┐
│ w q │
│ │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Write in-memory │ │ Discard in- │ │
│ │ table to disk │ │ memory table │ │
│ └────────┬────────┘ └────────┬────────┘ │
│ ▼ ▼ │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Changes persist │ │ Changes lost │ │
│ │ after exit │ │ after exit │ │
│ └─────────────────┘ └─────────────────┘ │
│ │
│ This is the safety mechanism. │
│ An accidental d or t can be abandoned with q. │
│ │
└──────────────────────────────────────────────────────────────┘
Summary
| Item | Value |
|---|---|
| Tool | fdisk (util-linux) |
| Table types | MBR (dos) and GPT |
| Init MBR | o |
| Init GPT | g |
| Create partition | n |
| Delete partition | d |
| Change type | t |
| Print table | p |
| Write | w |
| Quit without writing | q |
| MBR primary limit | 4 |
| Logical numbering | Starts at 5 |
| Linux type code | 83 |
| Swap type code | 82 |
| LVM type code | 8e |
Key takeaways:
- Changes are staged in memory until
w. Everyn,d, andtmodifies a copy of the partition table. The disk is not touched untilwis pressed. This is the difference betweenfdiskandparted, and it is the reasonfdiskis safer for interactive use . pbeforewis not optional. The print command shows exactly what will be written. An accidental deletion or a wrong size is visible in the in-memory table and can be abandoned withq.- MBR has a four-slot primary limit. The fifth partition must be logical, created inside an extended partition.
fdiskassigns logical numbers starting at 5 automatically once an extended partition exists . - The type code is a hint, not a constraint.
83marks a partition as Linux, but it does not prevent formatting it as swap or LVM. The type helps tools and the OS identify the intended use. - GPT works in
fdisk, but the flow differs. Usegto initialize a GPT table. Thencommand does not ask for primary or extended, and the type is selected with a GUID or alias rather than a hex code . - The kernel may need
partprobe. Writing the table does not always cause the kernel to re-read it.partprobeforces the update, or a reboot achieves the same result .
Remember: fdisk is a menu-driven editor for the partition table, and the menu is the interface. The commands are single letters, but the behavior is transactional: changes accumulate in memory and are committed or discarded as a unit. The exam tests the ability to navigate the menu, create the correct partition type, verify the layout with p, and write with w. The production skill is the same, with the addition of recognizing when MBR’s limits make GPT necessary and when partprobe is required to make the new partitions visible to the running system.
Stop using slow, ad-bloated tool sites! 🤮
🔎 Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)
⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free
🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!