The fdisk command on Linux Explained

The fdisk command is a standard disk management tool on Linux. It allows us to create new partitions and manage or delete existing partitions on Linux.

Disk management involves risks of data loss. If you are learning the fdisk command, use a blank disk or a virtual machine. The following tutorials explain how to create a virtual machine and add additional disks for testing.

RHCE Practice Lab Setup in VMWare and in VirtualBox
Adding and removing hard disks in VMware Workstation

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

lsblk command

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 specify 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

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.

To print the current partition information, we use the p (print) command.

print command

As we can see in the above output, currently this disk 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. If you need more partitions, you need to convert the last primary partition into the extended partition. Inside the extended partition, you can create a maximum of 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

To create a new partition, we use the n (new) command. 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 partitions and one extended partition. If the extended partition is available, the prompt allows us to create a logical partition in it.
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. If you want 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 uses 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 specify the size. For example, to create a 500 MB partition, you need to type +500M.

Let us create two primary partitions of 500MB.

create two partitions

Let us create one more primary partition of 1 GB.

new 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.

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 1 GB 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.

verify 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 partition number, you need to specify the hexacode of the type you want to use.

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

change 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 make any changes to this information, 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 need to 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. The following tutorials explain the process of creating a file system on a partition and mounting and un-mounting partitions.

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.

delete partitions

Use the w command to save the updated partition information.

save change

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

save change

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

ComputerNetworkingNotes Linux Tutorials The fdisk command on Linux Explained