You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Complete guide to fdisk - Partition Table Manipulator
category
Storage
tags
storage
disk management
lvm
partitioning
systemd
difficulty
beginner
last_updated
2026-06-06
related_commands
sfdisk
cfdisk
gdisk/sgdisk
parted
partprobe
fdisk - Partition Table Manipulator
Overview
fdisk is a dialog-driven program for creation and manipulation of partition tables. It understands GPT, MBR, Sun, SGI, and BSD partition tables. fdisk is a powerful tool for managing disk partitions on Linux systems and supports both interactive and scripted operations.
Installation
Package Managers
# Debian/Ubuntu (part of util-linux)
apt-get install util-linux
# RHEL/CentOS/Fedora
dnf install util-linux
# Arch Linux
pacman -S util-linux
# Alpine Linux
apk add util-linux
Basic Syntax
fdisk [OPTIONS] DEVICE
fdisk -l [DEVICE...]
Command Structure
Interactive Mode
# Enter interactive mode for a device
fdisk /dev/sda
# Common interactive commands:# m - print help menu# p - print partition table# n - add new partition# d - delete partition# t - change partition type# w - write table to disk and exit# q - quit without saving changes# l - list known partition types# a - toggle bootable flag# v - verify partition table# x - extra functionality (experts only)
Core Options
Display Options
# List partition tables for all devices
fdisk -l
# List partition tables for specific device
fdisk -l /dev/sda
# List partition tables for multiple devices
fdisk -l /dev/sda /dev/sdb /dev/sdc
# Use colored output
fdisk --color=always -l
# Disable colored output
fdisk --color=never -l
# List partition types
fdisk -l -t gpt /dev/sda
fdisk -l -t dos /dev/sda
Size Display Options
# Show sizes in sectors (default)
fdisk -l -u=sectors /dev/sda
# Show sizes in cylinders
fdisk -l -u=cylinders /dev/sda
# Display sizes in human-readable format
fdisk -l -H /dev/sda
# Display sizes in specific units
fdisk -l -S 4096 /dev/sda # 4KB sector size
Compatibility Options
# DOS compatibility mode (deprecated)
fdisk -c=dos /dev/sda
# CHS mode disabled (default)
fdisk -c=nondos /dev/sda
# Force DOS compatibility
fdisk -C 1024 /dev/sda
Partition Management
Creating Partitions
# Interactive partition creation
fdisk /dev/sda
# Then: n (new), p (primary), 1 (partition number), [Enter], [Enter], w (write)# Create primary partitionecho -e "n\np\n1\n\n\nw"| fdisk /dev/sda
# Create extended partitionecho -e "n\ne\n2\n\n\nw"| fdisk /dev/sda
# Create logical partitionecho -e "n\nl\n\n\nw"| fdisk /dev/sda
# Create partition with specific sizeecho -e "n\np\n1\n\n+10G\nw"| fdisk /dev/sda
# Create partition from sector X to Yecho -e "n\np\n1\n2048\n+20971520\nw"| fdisk /dev/sda
# Change partition type interactively
fdisk /dev/sda
# Then: t (type), 1 (partition), 83 (Linux), w (write)# Common partition type codes (MBR/DOS):# 0C - Windows 95 FAT32 (LBA)# 07 - NTFS/exFAT# 82 - Linux swap# 83 - Linux filesystem# 8e - Linux LVM# fd - Linux raid auto# ef - EFI System Partition# Set Linux filesystem typeecho -e "t\n1\n83\nw"| fdisk /dev/sda
# Set swap partition typeecho -e "t\n2\n82\nw"| fdisk /dev/sda
# Set LVM partition typeecho -e "t\n3\n8e\nw"| fdisk /dev/sda
# Set EFI partition typeecho -e "t\n1\nef\nw"| fdisk /dev/sda
GPT Partition Management
GPT Operations
# Create new GPT partition tableecho -e "g\nw"| fdisk /dev/sda
# Convert MBR to GPT
fdisk /dev/sda
# Then: g (GPT), w (write)# List GPT partition types
fdisk /dev/sda
# Then: l (list types)# Common GPT partition type GUIDs:# 1 - EFI System# 20 - Linux filesystem# 19 - Linux swap# 43 - Linux LVM# 29 - Linux RAID# Create EFI system partitionecho -e "n\n1\n\n+512M\nt\n1\nw"| fdisk /dev/sda
# Create Linux filesystem partitionecho -e "n\n2\n\n\nt\n2\n20\nw"| fdisk /dev/sda
GPT Attributes
# Set GPT attributes
fdisk /dev/sda
# Then: x (expert), A (attributes), partition number, attribute bit# Common GPT attributes:# Bit 0 - Platform required# Bit 2 - Legacy BIOS bootable# Bit 60 - Read-only# Bit 62 - Hidden# Bit 63 - No automount
Advanced Partition Operations
Partition Alignment
# Check partition alignment
fdisk -l /dev/sda | grep "^/dev"# Create aligned partitions (1MiB alignment)
fdisk /dev/sda
# Start partition at sector 2048 (1MiB)# Check if partition is aligned
blockdev --getalignoff /dev/sda1
# Get optimal I/O size
blockdev --getioopt /dev/sda
# Get physical block size
blockdev --getpbsz /dev/sda
Boot Flag Management
# Toggle bootable flag
fdisk /dev/sda
# Then: a (toggle boot), 1 (partition), w (write)# Set boot flag via scriptecho -e "a\n1\nw"| fdisk /dev/sda
# Check boot flag status
fdisk -l /dev/sda | grep "*"
Partition Label Management
# Set disk label (MBR)echo -e "o\nw"| fdisk /dev/sda
# Set disk label (GPT)echo -e "g\nw"| fdisk /dev/sda
# Set disk label (SGI)echo -e "s\nw"| fdisk /dev/sda
# Set disk label (SUN)echo -e "S\nw"| fdisk /dev/sda
# Set disk label (BSD)echo -e "b\nw"| fdisk /dev/sda
Expert Mode Features
Expert Commands
# Enter expert mode
fdisk /dev/sda
# Then: x (expert mode)# Expert mode commands:# b - move beginning of data in partition# c - change number of cylinders# d - print raw partition table# e - list extended partitions# f - fix partition order# g - create IRIX partition table# h - change number of heads# i - change disk identifier# l - show partition chain# m - print expert menu# p - print partition table# q - quit without saving# r - return to main menu# s - change number of sectors# v - verify partition table# w - write table to disk and exit
Disk Geometry Modification
# Change number of cylinders
fdisk /dev/sda
# x, c, <number>, r, w# Change number of heads
fdisk /dev/sda
# x, h, <number>, r, w# Change number of sectors per track
fdisk /dev/sda
# x, s, <number>, r, w# Display current geometry
fdisk -l /dev/sda | grep "Geometry"
Disk Identifier Management
# Change disk identifier
fdisk /dev/sda
# x, i, 0x12345678, r, w# Generate random disk identifier
fdisk /dev/sda
# x, i, r, w# Display disk identifier
fdisk -l /dev/sda | grep "Disk identifier"# Set specific identifierecho -e "x\ni\n0x$(openssl rand -hex 4)\nr\nw"| fdisk /dev/sda
# Modern disk alignment (1MiB)# Start partitions at sector 2048# Size in multiples of 2048 sectors# Check optimal alignment
blockdev --getalignoff /dev/sda1 # Should be 0
blockdev --getioopt /dev/sda # Optimal I/O size# SSD alignment (ensure TRIM support)
cat /sys/block/sda/queue/discard_max_bytes
Safety Practices
# Always backup partition table before changes
sfdisk -d /dev/sda > /root/sda_partition_backup.txt
dd if=/dev/sda of=/root/sda_mbr.bin bs=512 count=1
# Verify changes before writing
fdisk /dev/sda
# Make changes, then 'p' to print, verify, then 'w'# Use wipefs before repartitioning
wipefs -a /dev/sda
# Check for mounted filesystems
mount | grep /dev/sda
lsof /dev/sda
Performance Considerations
Optimization Tips
# Use GPT for disks > 2TBecho -e "g\nw"| fdisk /dev/sda
# Align partitions to 1MiB boundaries# Start at sector 2048 (1MiB)# For SSDs, ensure TRIM is enabled
hdparm -I /dev/sda | grep TRIM
fstrim -v /
# Check I/O scheduler
cat /sys/block/sda/queue/scheduler
# Set I/O scheduler for SSDecho"noop"> /sys/block/sda/queue/scheduler
# Set default fdisk behaviorexport FDISK_NO_COLOR=1 # Disable colorsexport FDISK_USE_CYLINDERS=0 # Don't use cylindersexport LIBFDISK_DEBUG=all # Enable debug output# Set specific terminal for fdiskexport TERM=xterm-256color
# Create single partition using entire disk
(
echo o # Create DOS partition tableecho n # New partitionecho p # Primary partitionecho 1 # Partition numberecho# First sector (default)echo# Last sector (default)echo w # Write changes
) | fdisk /dev/sdb
Example 2: Multi-Partition Layout
# Create boot, swap, and root partitions
(
echo o
echo n
echo p
echo 1
echoecho +512M
echo n
echo p
echo 2
echoecho +8G
echo n
echo p
echo 3
echoechoecho t
echo 2
echo 82
echo a
echo 1
echo w
) | fdisk /dev/sdb
Example 3: GPT UEFI System
# Create GPT layout for UEFI boot
(
echo g
echo n
echo 1
echoecho +512M
echo n
echo 2
echoecho +8G
echo n
echo 3
echoechoecho t
echo 1
echo 1
echo t
echo 2
echo 19
echo t
echo 3
echo 20
echo w
) | fdisk /dev/sdb
# Format partitions
mkfs.vfat -F32 /dev/sdb1 # EFI
mkswap /dev/sdb2 # Swap
mkfs.ext4 /dev/sdb3 # Root
Example 4: LVM Setup
# Prepare disk for LVM
(
echo o
echo n
echo p
echo 1
echoechoecho t
echo 8e
echo w
) | fdisk /dev/sdb
# Create LVM structures
pvcreate /dev/sdb1
vgcreate vg_main /dev/sdb1
lvcreate -L 20G -n lv_root vg_main
lvcreate -L 10G -n lv_home vg_main
lvcreate -l 100%FREE -n lv_data vg_main
Example 5: RAID Preparation
# Prepare multiple disks for RAIDfordiskin sdb sdc sdd sde;do
(
echo o
echo n
echo p
echo 1
echoechoecho t
echo fd
echo w
) | fdisk /dev/$diskdone# Create RAID5 array
mdadm --create /dev/md0 --level=5 --raid-devices=4 \
/dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
See Also
fdisk(8) - Linux man page
sfdisk(8) - Script-friendly fdisk
cfdisk(8) - Curses fdisk
gdisk(8) - GPT fdisk
parted(8) - Partition editor
partx(8) - Tell kernel about partition changes
Version Information
# Check fdisk version
fdisk --version
# Check for features
fdisk --help | grep -i gpt
Summary
fdisk is the traditional and most widely used partition table manipulator for Linux systems. While it supports both MBR and GPT partition tables, it's primarily known for its interactive mode and MBR handling. For GPT operations, gdisk/sgdisk might be more appropriate, and for scripting, sfdisk provides better automation capabilities. Always backup partition tables before making changes and verify operations before writing to disk.