CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Disk & Storage / fdisk

fdisk

View and edit a disk's partition table

Intermediatediskpartitionsadmin

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.

Syntaxfdisk [options] DEVICE | fdisk -l [DEVICE...]

Watch it run

You want to see the root disk's layout, partition types and exact sector positions before changing anything.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 2
fdisk -l /dev/nvme0n1

fdisk reads the raw device, which only root (and the disk group) can open. lsblk works without root because it reads sysfs instead.

Anatomy of a command

$
commandsudo

Reading a raw disk device needs root (the device node is owned by root:disk).

How it works

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.

Options you'll actually use

-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 cylinders
g (interactive)Create a new empty GPT partition table
n (interactive)Add a new partition
t (interactive)Change a partition's type (Linux, swap, LVM, EFI...)
d / p (interactive)Delete a partition / print the table in memory
w / q (interactive)Write changes and exit / quit without saving

Common mistakes

sudo fdisk /dev/nvme0n1p1

fdisk 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/nvme0n1
Using an MBR (DOS) table on a 4 TB disk

MBR 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 table
Running 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 lsblk
Piping keystrokes into fdisk in automation

Prompts 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%

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    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.)

  2. 2

    Dump the partition table of disk.img to a text file, as a backup.

  3. 3

    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.)

Related commands

lsblkList disks, partitions and where they are mountedmkfsCreate a filesystem on a disk or partitionblkidShow filesystem type, UUID and label of devicesddCopy and convert raw data block by block
PreviouslsblkNextmount
Linux / Disk & Storage / fdisk

fdisk

View and edit a disk's partition table

Intermediatediskpartitionsadmin

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.

Syntaxfdisk [options] DEVICE | fdisk -l [DEVICE...]

Watch it run

You want to see the root disk's layout, partition types and exact sector positions before changing anything.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 2
fdisk -l /dev/nvme0n1

fdisk reads the raw device, which only root (and the disk group) can open. lsblk works without root because it reads sysfs instead.

Anatomy of a command

$
commandsudo

Reading a raw disk device needs root (the device node is owned by root:disk).

How it works

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.

Options you'll actually use

-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 cylinders
g (interactive)Create a new empty GPT partition table
n (interactive)Add a new partition
t (interactive)Change a partition's type (Linux, swap, LVM, EFI...)
d / p (interactive)Delete a partition / print the table in memory
w / q (interactive)Write changes and exit / quit without saving

Common mistakes

sudo fdisk /dev/nvme0n1p1

fdisk 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/nvme0n1
Using an MBR (DOS) table on a 4 TB disk

MBR 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 table
Running 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 lsblk
Piping keystrokes into fdisk in automation

Prompts 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%

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    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.)

  2. 2

    Dump the partition table of disk.img to a text file, as a backup.

  3. 3

    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.)

Related commands

lsblkList disks, partitions and where they are mountedmkfsCreate a filesystem on a disk or partitionblkidShow filesystem type, UUID and label of devicesddCopy and convert raw data block by block
PreviouslsblkNextmount