Partitioning Disks in Linux Using the fdisk Utility

Managing disk partitions is a crucial task for Linux system administrators. The fdisk command is a powerful command-line tool that enables you to create, modify, and delete partitions on your Linux system. In this guide, we’ll walk through the process of creating partitions using fdisk step by step. We’ll attach a 10 GB raw disk to our Linux system and create two partitions on it.

Prerequisites

Before you begin, make sure you have the following:

  • A running Linux system.

  • Root privileges or a sudo user with administrative rights.

  • Basic understanding of disks and partitions: Familiarize yourself with device naming conventions in Linux, such as /dev/sda, /dev/sdb, and /dev/nvme0n1.

Steps to Create Partitions in Linux Using fdisk

Method 1: Using fdisk to Create Partitions

Step 1: Identify the disk.

First, identify the newly attached raw disk on which you want to create partitions.

Run the fdisk -l command and look for unpartitioned/unallocated drives.

$ sudo fdisk -l

Alternatively, you can verify the disk (e.g., /dev/sdb) by running the command:

$ lsblk

Step 2: Run fdisk for the target disk.

Run the fdisk command with the target disk as an argument:

$ sudo fdisk /dev/sdb

Once inside the fdisk interactive menu, you will see a prompt like this:

Command (m for help):

Note: Type m and press Enter to display a list of available commands.

Step 3: Create a new partition.

To create a new partition, type n and press Enter. You will be prompted to select the partition type and number.

Select Partition Type:

  • p for primary partition

  • e for extended partition

Specify the Partition Number: If it’s the first partition, type 1 and press Enter.

Set the Start and End Sectors:

  • Press Enter to accept the default starting sector.

  • Specify the size by entering a value (e.g., +4G for a 4 GB partition).

Create two partitions, each of size 4 GB.

Step 4: Write the changes.

After defining the partitions, type w and press Enter to write the changes to the disk. This step finalizes the partitioning.

Step 5: Verify the new partitions.

Run the following command to verify the new partitions on disk /dev/sdb:

$ sudo fdisk -l /dev/sdb

You should see two new partitions listed, such as /dev/sdb1 and /dev/sdb2 with their respective IDs and types.

Step 6: Format partitions with mkfs.

Format the newly created partitions with a file system. For example, to format /dev/sdb1 as ext4 and /dev/sdb2 as xfs, run the following commands:

$ sudo mkfs.ext4 /dev/sdb1
$ sudo mkfs.xfs /dev/sdb2

The output will confirm that both partitions have been formatted with the ext4 and xfs file systems, respectively.

Step 7: Mount the partitions.

To mount these partitions, first create mount points:

$ sudo mkdir /opt/data1
$ sudo mkdir /opt/data2

Now, run the mount command:

$ sudo mount /dev/sdb1 /opt/data1
$ sudo mount /dev/sdb2 /opt/data2

Next, run the df command to verify whether the new partitions are mounted:

$ df -Th

To mount these disk partitions permanently, add the following entries to the /etc/fstab file:

/dev/sdb1         /opt/data1         ext4    defaults             0 0
/dev/sdb2         /opt/data2         xfs     defaults             0 0

Save and close the file, and then run:

$ sudo mount -a

Troubleshooting Common Issues

Partition Not Showing Up

If the new partition doesn’t appear, run the following command to reload the partition table:

$ sudo partprobe

Unable to Format the Partition

Ensure the partition is not mounted before formatting. Use:

$ sudo umount /dev/sdb1

The fdisk command is a versatile tool for managing disk partitions in Linux. By following this guide, you can confidently create partitions and prepare them for use.