Linux

How to Use fdisk for Disk Partitioning

How to Use fdisk for Disk Partitioning

Disk partitioning is a critical process in managing storage on a computer. It allows users to divide a single hard drive into multiple sections, or partitions, each of which can be formatted and used for different purposes. The fdisk command is a powerful tool for disk partitioning on Linux systems, and in this article, we will guide you through its usage.

What is fdisk?

fdisk stands for “fixed disk” and is a command-line utility for managing disk partitions. It is available on most Linux distributions and provides a range of features for creating, deleting, and modifying disk partitions. While fdisk is a straightforward tool, it can have a steep learning curve for those who are new to Linux. However, with some practice and guidance, you will find it to be an invaluable tool for disk management.

Understanding Disk Partitions
Before diving into fdisk, it’s essential to understand what disk partitions are. A disk partition is a defined storage space on a hard drive. When a hard drive is partitioned, each partition acts like a separate drive. This is useful for several reasons:

Organization: Keeping operating systems, applications, and personal files separate.
Multiple Operating Systems: Installing multiple OSes on one hard drive.
Backup and Recovery: Easier to back up data on specific partitions.
There are two main types of partition tables: MBR (Master Boot Record) and GPT (GUID Partition Table). MBR supports up to four primary partitions, while GPT allows for a much larger number of partitions and is suitable for modern systems.

Accessing fdisk
To use fdisk, you need to have root privileges. Open your terminal and type the following command to access fdisk for a specific disk. For example, to access the first hard drive (/dev/sda):

sudo fdisk /dev/sda

Basic fdisk Commands
Once you enter fdisk, you can type various commands to manage your partitions. Here are some essential commands:

m: Displays the help menu with a list of available commands.
p: Prints the current partition table.
n: Creates a new partition.
d: Deletes a partition.
w: Writes the changes to disk and exits.
q: Quits without saving changes.
Viewing Current Partitions
To view your current partitions, enter fdisk and type p. This will display a list of all existing partitions, their sizes, and other details. It’s crucial to note the partition numbers and sizes before making any changes.

Creating a New Partition
Start by typing n to create a new partition.
You will be prompted to choose whether you want to create a primary or extended partition. Select p for primary.
Next, you will need to specify the partition number (1-4 for primary) and the size of the partition. You can specify the size in MB (e.g., +500M for 500 MB) or GB (e.g., +20G for 20 GB).
After entering the size, fdisk will allocate the space and add the new partition.
Deleting a Partition
If you need to delete a partition:

Type d and hit Enter.
Enter the partition number you wish to delete.
Confirm the deletion by typing w to write the changes to disk.
Writing Changes
After creating or deleting partitions, you need to save your changes. Type w and hit Enter. This command writes the partition table to the disk. If you forget to write the changes, none of your modifications will be saved.

Exiting fdisk
To exit fdisk, simply type q if you haven’t made any changes or after writing your changes with w.

Formatting Partitions
After partitioning, each partition must be formatted with a filesystem before it can be used. Common filesystems include ext4, NTFS, and FAT32. Use the following command to format a partition:

sudo mkfs.ext4 /dev/sda1

Replace /dev/sda1 with the appropriate partition identifier.

Mounting Partitions
Once formatted, partitions need to be mounted to be accessible. You can mount a partition by creating a mount point and using the mount command:

sudo mkdir /mnt/my_partition
sudo mount /dev/sda1 /mnt/my_partition

Replace /dev/sda1 with your partition and /mnt/my_partition with your desired mount point.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button