In this guide, we will explain how to create partitions in linux using fdisk command step by step. We will attach a raw disk on our linux system of size 10 GB and will create two partitions on it.
Creating and managing disk partitions is one of the important tasks for linux system administrators. The fdisk command is a powerful command line tool that allows you to create, modify, and delete partitions on your Linux system.
Prerequisites
Before we start, ensure you have the following:
- A Running Linux system.
- Root privileges or sudo user with admin rights
- A basic understanding of disks and partitions: Know the device naming conventions in Linux, such as /dev/sda,/dev/sdb and /dev/nvme0n1, etc.
Steps to Create Partitions in Linux Using Fdisk
1) Identify the Disk
First, find the newly attached raw disk on which we will create partitions.
Run fdisk -l command and look for unpartitioned / unallocated drives.
$ sudo fdisk -l
Alternate way to verify that disk /dev/sdb is the newly attached disk by running the command.
$ lsblk
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 below:
Command (m for help):
Note : Type m and press Enter to display a list of available commands.
3) Create 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).
Using above steps create two partitions of each size 4 GB as shown below:
After defining the partitions, type w and press Enter to write the changes to the disk. This step finalizes the partitioning.
4) 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 Id 83 and Type as Linux.
5) Format Partitions with mkfs
Format the newly created partitions with a file system. For example, to format /dev/sdb1 as ext4 & /dev/sdb2 as xfs. Run the following fdisk commands.
$ sudo mkfs.ext4 /dev/sdb1 $ sudo mkfs.xfs /dev/sdb2
Output above confirms that both the partitions have been formatted with ext4 and xfs file system respectively.
6) Mount these Partitions
In order to mount these partitions, let’s first create a 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 df command to verify whether new partitions are mounted or not.
$ 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 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
Conclusion
The fdisk command is a powerful tool for managing disk partitions in Linux. By following this guide, you can confidently create partitions and prepare them for use. Always exercise caution and ensure your data is backed up before performing disk operations.
That’s all from this guide, feel free to post your queries and feedback in below comments section.
Also Read: How to Create Partitions with Parted Command in Linux