Linux Disk Partitioning Made Easy: A Simple Tutorial
Managing disk space efficiently can be essential for system stability, especially when running multiple operating systems or reserving disk space for specific purposes. In this guide, you will learn how to create a partition in Linux using the command-line utility fdisk
. The process is straightforward, and partitions can help protect your data in case of disk failure.
Step-by-Step Guide
To begin with, we will use the fdisk
command, a powerful text-based utility that allows you to view and manage partitions on your system.
Viewing Existing Partitions
Before creating a new partition, it is necessary to identify the current layout of your disks. The following command lists all the partitions on your system:
sudo fdisk -l
This command will request administrative privileges. Once entered, you will see a list of partitions similar to the output below:
Disk /dev/loop0: 14.8 MiB, 15466496 bytes, 30208 sectors
...
Selecting a Disk for Partitioning
After reviewing the list of partitions, choose a disk where you wish to create the new partition. To begin partitioning, we need to enter the disk’s path in the following command:
sudo fdisk [disk path]
This brings us to the command mode of fdisk
.
Navigating the Command Mode
Once in the command mode, you can view the list of available actions by pressing m
. The fdisk
command mode interface can be confusing at first, but it’s manageable once you know the basic commands.
Creating the Partition
To create a new partition, use the n
command. This will prompt you to choose the type of partition:
- Press
p
to create a primary partition. - Press
l
if you want to create a logical partition.
For this tutorial, we will create a primary partition by selecting p
. You will now be asked to specify the starting and ending sectors of the partition.
To use the default starting sector, press ENTER. You can either use all available space or specify the size of the partition. In this example, we are creating a partition of 10 MB:
+10M
Changing the Partition Type
By default, Linux assigns the “Linux” partition type. If you need to change it, you can use the t
command. For example, if you want to set the partition to “Linux LVM”, you will be prompted to enter the HEX code for that partition type.
If you don’t remember the HEX code, press L
to list all available codes. For “Linux LVM,” the code is 8e
. Enter this code to confirm the partition type.
Verifying and Saving the Changes
Once all your changes are set, it’s a good idea to verify them. You can list the partitions again with the p
command to confirm everything is as expected.
To make the changes permanent, press w
to write the partition to the disk. If you change your mind and don’t want to save the new partition, press q
to exit without saving.
Formatting the Partition
After creating the partition, it is highly recommended to format it. This ensures the partition is ready for use and avoids potential issues. Use the following command to format the partition to the ext4 file system:
sudo mkfs.ext4 [partition path]
Conclusion
Partitioning your disk in Linux is a great way to allocate space for specific tasks or install multiple operating systems. Using the fdisk
tool, you can easily create, modify, and manage partitions to suit your needs. Each partition acts as a separate disk, which helps minimize damage if one partition becomes corrupted.