The fdisk
command in Linux is a powerful disk partitioning tool used to create, delete, and modify disk partitions. It is commonly used to manage disk drives and partitions on a system. While fdisk
works primarily with MBR (Master Boot Record) partition schemes, it also supports GPT (GUID Partition Table) on modern systems.
Contents
List All Disks and PartitionsStart fdisk for a Specific DiskCreate a New PartitionDelete a PartitionChange Partition TypeDisplay Partition TableResize a PartitionVerify Partition ChangesSave Changes and ExitDiscard Changes and ExitCreate a GPT Partition TableCreate an MBR Partition TableDisplay Help Menu
List All Disks and Partitions
sudo fdisk -l
Displays information about all disks and their partitions.
Start fdisk for a Specific Disk
sudo fdisk /dev/sda
Opens the fdisk
interface for /dev/sda
.
Create a New Partition
- Start
fdisk
for the disk:
sudo fdisk /dev/sda
- Press
n
to create a new partition. - Follow the prompts to specify the partition type (primary/extended), size, and location.
- Press
w
to write changes and exit.
Delete a Partition
- Start
fdisk
for the disk:
sudo fdisk /dev/sda
- Press
d
to delete a partition. - Select the partition number to delete.
- Press
w
to save changes.
Change Partition Type
- Start
fdisk
for the disk:
sudo fdisk /dev/sda
- Press
t
to change the partition type. - Enter the partition number and the type code (e.g.,
83
for Linux,8e
for LVM). - Press
w
to save changes.
Display Partition Table
- Start
fdisk
for the disk:
sudo fdisk /dev/sda
- Press
p
to print the partition table.
Resize a Partition
- Start
fdisk
for the disk:
sudo fdisk /dev/sda
- Delete the partition (
d
) and recreate it (n
) with the new size. - Note: Resizing may require additional tools like
resize2fs
for ext4 partitions.
Verify Partition Changes
- Start
fdisk
for the disk:
sudo fdisk /dev/sda
- Press
v
to verify the partition table for errors.
Save Changes and Exit
- Start
fdisk
for the disk:
sudo fdisk /dev/sda
- Press
w
to write changes and exit.
Discard Changes and Exit
- Start
fdisk
for the disk:
sudo fdisk /dev/sda
- Press
q
to quit without saving changes.
Create a GPT Partition Table
- Start
fdisk
for the disk:
sudo fdisk /dev/sda
- Press
g
to create a new GPT partition table. - Press
w
to save changes.
Create an MBR Partition Table
- Start
fdisk
for the disk:
sudo fdisk /dev/sda
- Press
o
to create a new MBR (DOS) partition table. - Press
w
to save changes.
Display Help Menu
- Start
fdisk
for the disk:
sudo fdisk /dev/sda
- Press
m
to display the help menu with all available commands.
Key Notes:
- Backup Data: Always back up your data before modifying partitions.
- Partition Types: Use
l
infdisk
to list all supported partition types. - GPT vs. MBR: GPT supports larger disks (>2TB) and more partitions.