Operations 10 min read

Master Linux Disk Partitioning with fdisk: A Step‑by‑Step Guide

This article explains how to use the Linux fdisk command to create, modify, and delete disk partitions, covering syntax, options, example workflows for primary, extended, and logical partitions, formatting with XFS, mounting, and configuring automatic mounts via /etc/fstab.

Open Source Linux
Open Source Linux
Open Source Linux
Master Linux Disk Partitioning with fdisk: A Step‑by‑Step Guide

fdisk: Disk Partitioning

Function Description

The fdisk command can partition disks using an interactive questionnaire interface and also display detailed partition information. Its full name is “Partition table manipulator for Linux”.

Command Syntax

fdisk [options] [parameters]

Option Meanings

The options are described below:

-b <sector size>: specify disk sector size (valid values: 512, 1024, 2048, 4096).

-c[=<mode>]: compatibility mode, "dos" or "nondos" (default).

-h: display this help text.

-u[=<unit>]: with "-l" list, use partition count instead of cylinder count for start addresses.

-v: display version information.

-C <number>: specify number of cylinders.

-H <number>: specify number of heads.

-S <number>: specify sectors per track.

Parameters

Parameters refer to device files, indicating the hard‑disk device to partition or display.

Reference Example

(1) Select the disk to operate on: # fdisk /dev/sda (2) Enter "m" to list available sub‑commands:

m
Commands:
  a   toggle a bootable flag
  b   edit bsd disklabel
  c   toggle the dos compatibility flag
  d   delete a partition
  g   create a new empty GPT partition table
  G   create an IRIX (SGI) partition table
  l   list known partition types
  m   print this menu
  n   add a new partition
  o   create a new empty DOS partition table
  p   print the partition table
  q   quit without saving changes
  s   create a new empty Sun disklabel
  t   change a partition's system id
  u   change display/entry units
  v   verify the partition table
  w   write table to disk and exit
  x   extra functionality (experts only)

(3) Enter "p" to display the current partition table of /dev/sda:

p

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c5066

  Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 209715199 103808000 8e Linux LVM

(4) Create and delete a primary partition:

# fdisk /dev/sdb
n   # create partition
  p   # primary
  1   # partition number
  2048   # start sector
  +10G   # size
p   # verify creation

# fdisk /dev/sdb
d   # delete partition
  1   # partition number

(5) Create an extended partition and logical partitions:

# fdisk /dev/sdb
n   # create partition
  e   # extended
  3   # partition number
  (default start) 
  +30G   # size
p   # verify

# fdisk /dev/sdb
n   # create logical partition
  l   # logical
  (default start)
  +5G   # size

# fdisk /dev/sdb
n   # create another logical partition
  l   # logical
  (default start)
  +10G   # size
p   # verify all partitions

(6) Write changes to disk:

w
The partition table has been altered!
Calling ioctl() to re-read partition table.

After partitioning, the partitions must be formatted before use.

(7) Format the new partitions with XFS:

# mkfs.xfs /dev/sdb5
# mkfs.xfs /dev/sdb6

(8) Create mount points and mount the partitions:

# mkdir /test1
# mkdir /test2
# mount /dev/sdb5 /test1
# mount /dev/sdb6 /test2

(9) Verify the mounts:

# df -Th
Filesystem     Type  Size Used Avail Use% Mounted on
/dev/sdb5      xfs   5.0G 33M  5.0G   1% /test1
/dev/sdb6      xfs  10G  33M  10G    1% /test2

(10) Configure automatic mounting at boot by editing /etc/fstab:

# vim /etc/fstab
/dev/sdb5  /test1  xfs  defaults  0 0
/dev/sdb6  /test2  xfs  defaults  0 0
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxSystem Administrationdisk partitioningfdisk
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.