fdisk
The fdisk
command is a powerful utility in Linux used to manage disk partitions. It provides an interface to create, delete, resize, and manage disk partitions on a hard drive or other storage device. Below is a comprehensive guide to using fdisk
.
Basic Usage
To start fdisk
on a specific disk, you use the following command:
sudo fdisk /dev/sdX
Replace /dev/sdX
with the appropriate disk identifier (e.g., /dev/sda
, /dev/nvme0n1
). This command opens an interactive session with fdisk
where you can manage partitions.
Common Commands Within fdisk
Once you’re inside fdisk
, several commands allow you to perform different operations. Here are some of the most commonly used commands:
m
: Display the help menu.p
: Print the partition table of the disk, showing details like partition numbers, types, start and end sectors, and sizes.n
: Create a new partition.d
: Delete an existing partition.t
: Change the type (system ID) of a partition.l
: List known partition types.w
: Write the changes to the disk and exit.q
: Quit without saving changes.v
: Verify the disk.
Step-by-Step Guide to Using fdisk
1. Starting fdisk
To manage partitions on a disk (e.g., /dev/sda
), start fdisk
with:
sudo fdisk /dev/sda
2. View the Current Partition Table
To see the current partition layout, press p
:
Command (m for help): p
This will display the partition table, including details like partition number, start and end sectors, size, and partition type.
3. Creating a New Partition
To create a new partition:
- Press
n
to create a new partition.
Command (m for help): n
- Select the partition type. You will typically choose between
primary
orextended
. You can create up to four primary partitions, or three primary and one extended partition.
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
- Choose the partition number (1-4 for primary partitions).
- Select the starting sector. Press
Enter
to accept the default (which usually aligns with sector boundaries). - Enter the size of the partition. You can specify it in kilobytes (K), megabytes (M), gigabytes (G), or terabytes (T). For example:
Last sector, +sectors or +size{K,M,G,T,P} (4096-2097151, default 2097151): +20G
- The partition is now defined but not yet written to the disk.
4. Changing a Partition’s Type
To change the type (or system ID) of a partition:
- Press
t
.
Command (m for help): t
- Enter the partition number you want to change.
- Enter the hex code of the desired partition type. Press
l
to list all possible types. For example, to set a Linux LVM partition type, enter8e
.
5. Deleting a Partition
To delete an existing partition:
- Press
d
.
Command (m for help): d
- Enter the number of the partition you want to delete.
- The partition is marked for deletion but is not yet removed from the disk.
6. Writing Changes to Disk
After making changes, you must write them to the disk:
- Press
w
to write the changes to disk and exit.
Command (m for help): w
- The disk’s partition table is updated, and
fdisk
exits.
7. Quitting Without Saving Changes
If you want to quit fdisk
without saving any changes:
- Press
q
.
Command (m for help): q
- This exits
fdisk
without writing any changes.
Example Workflow
Let’s say you want to create a new 20GB primary partition on /dev/sda
.
- Start
fdisk
:
sudo fdisk /dev/sda
- Create a new partition:
- Press
n
to create a new partition. - Choose
p
for a primary partition. - Accept the default partition number (e.g.,
2
if this is your second partition). - Accept the default first sector.
- Type
+20G
to specify the size.
- Optionally, change the partition type by pressing
t
. - Write the changes and exit by pressing
w
.
Verifying and Using the New Partition
After creating the partition:
- Update the Kernel Partition Table: Sometimes, you need to notify the kernel of changes:
sudo partprobe
- Format the Partition: You may want to format the new partition before using it:
sudo mkfs.ext4 /dev/sda2
- Mount the Partition: You can then mount the partition to make it available in the filesystem:
sudo mount /dev/sda2 /mnt/newpartition
Summary
fdisk
is an interactive tool for managing disk partitions in Linux.- View the current partition table with
p
. - Create new partitions with
n
. - Delete existing partitions with
d
. - Change partition types with
t
. - Write changes with
w
or quit without saving withq
.
fdisk
is a powerful utility, but it directly manipulates disk partitions, so always be cautious and double-check commands to avoid data loss.