The fdisk command on Linux Explained

The fdisk command is a standard disk management tool on Linux. This tutorial explains how to use it to create new partitions and manage or delete existing partitions on Linux.

Select a blank hard disk or the hard disk on which you want to create or manage partitions and specify its name as an argument to the fdisk command. You need to provide the full path with the disk name. For example, if you want to create or manage partitions on the disk sda, use the following command.

#fdisk /dev/sda

You can use the lsblk command to list the attached hard disks and their partition information.

lsblk command

The fdisk utility reads the partition information from the specified disk and provides a command prompt to manage the disk.

fdisk start

It uses letters as commands. For example, it keeps all changes in memory until we instruct it to save them. We use the letter w to save the changes. It interrupts the letter w as the write command.

We use the p (print) command to print the current partition information.

print command

As we can see in the above output, this disk currently has no partitions.

MBR firmware

This command creates partitions for MBR firmware. MBR firmware supports a maximum of 14 partitions. It uses a concept of primary, extended, and logical partitions. It supports a maximum of four primary partitions. You must convert the last primary partition into the extended partition if you need more partitions. Inside the extended partition, you can create eleven logical partitions. The extended partition works as a container. It stores only logical partitions. We cannot use it to save data.

MBR firmware

Creating new partitions

The n (new) command creates a new partition. It starts a new command prompt. On this prompt, we need to specify the following information in the sequence: partition type, partition number, first sector, and last sector or partition size.

Partition type Type p to create a primary partition. Type e to create an extended partition. If you need more than four partitions, create three primary and one extended partition. The prompt allows us to create a logical partition if the extended partition is available.
Partition number Type the partition number. The prompt automatically selects the next partition. To accept the default selection, hit the Enter key.
First sector The prompt automatically selects the first available sector for the partition. To start the partition from any other sector, specify the sector number or hit the Entry key to use the default.
Last sector The prompt automatically selects the last available sector for the partition. If you hit the Enter key, the prompt will use all available space for this partition. If you want to create more partitions, you need to specify the size of this partition. You can use standard units to select the size. For example, to create a 500 MB partition, type +500M.

Let us create two primary partitions of 500 MB.

Creating two partitions

Let us create one more primary partition of 1 GB.

1 GB partition

We have created three primary partitions. Now, we can create one more primary partition or an extended partition.

Let us create an extended partition.

Creating new extended partition

Inside the extended partition, we can create logical partitions. We can create a maximum of 11 logical partitions.

Let us create two logical partitions. Use 1GB of disk space for the first partition.

logical partition

Use the remaining disk space for the second partition.

logical partition 2

To verify partitions, use the p command.

Verifying partition

Changing partition type

By default, the fdisk command creates standard Linux partitions. It supports many other partition types as well. To view a complete list of all supported partition types, you can use the l command. The l command lists all supported partition types with their hexacode.

Listing partition type

To change the partition type, we use the t command. The t command needs two arguments: partition number and hexacode. By default, it changes the type of the last partition. To change the type of any other partition, specify its number as an argument. After the partition number, specify the hexacode of the type you want to use.

Let us change the type of the last partition to LVM.

Changing partition type

The fdisk command keeps all changes we make in the RAM until we use the w (write) command. Use the w command to save and apply the changes.

Write change

The kernel reads the partition information at the boot time. It does not actively monitor this information on the running system. If we change to this information, the kernel will learn about the changes at the next boot. If we want to update the kernel about this change without rebooting the system, we must use the partprobe command. This command forces the kernel to reread the partition information.

To verify the new partitions, we can use the lsblk command again.

partprobe command

After creating a partition, you must create a file system on it and mount it to the Linux file system before you can use it to save data. If you mount a partition, you must un-mount it before deleting it.

Deleting partitions

To delete a partition, run the fdisk command and specify the disk's name that contains the partition as an argument.

fdisk command

On the fdisk command prompt, we use the d command to delete a partition. The d command needs the partition number. By default, it selects the last partition.

Let us delete the partitions we created in this exercise.

Deleting partitions

Use the w command to save the updated partition information.

Saving change

Run the partprobe command to update the kernel and use the lsblk command again to verify the delete operation.

Saving change

As we can see in the above output, the partitions we made in this exercise have gone.

This tutorial is part of the tutorial series Linux disk management concepts and configurations. Other parts of this series are the following.

Chapter 01  Linux disk management terminology
Chapter 02  File Management Commands in Linux
Chapter 03  Linux file system types explained
Chapter 04  Adding and removing hard disks in VMware Workstation
Chapter 05  List view and find hard disk names in Linux
Chapter 06  The fdisk command on Linux explained
Chapter 07  Manage Linux disk partition with the gdisk command
Chapter 08  The /etc/fstab file on Linux explained
Chapter 09  Linux disk management with the parted command
Chapter 10  The mkfs command on Linux
Chapter 11  The mount command on Linux temporary mounting
Chapter 12  The swap space on Linux explained
Chapter 13  How to create a swap partition in Linux
Chapter 14  How to configure LVM in Linux step-by-step
Chapter 15  How to configure RAID in Linux step-by-step

Conclusion

The fdisk command is a powerful tool for managing disk partitions on Linux. It enables users to create, modify, and delete partitions effectively through a command-line interface. By following the steps outlined in this tutorial, you can confidently manage both MBR and extended partitions and create logical partitions as needed.

ComputerNetworkingNotes Linux Tutorials The fdisk command on Linux Explained

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com