Fdisk usage on Linux

Introduction

Fdisk is used to manipulate the disk partitions and its types like GPT, MBR, Sun, SGI, and BSD partitions. Fdisk is available as part of the Linux utils commands .We will be exploring some of the many usages of Fdisk in this article.

List all partitions

To list all the partitions available on the system, issue the below command:

sudo fdisk –l

List partition on a specific disk

If we need to look up the partition on only a specific disk, you can use the command:

sudo fdisk -l /dev/sda

This will list all the partitions under the /dev/sda drive. Note that the Linux system always stores the drive in the order of /dev/sdX where X is the ascending order of the alphabet. Hence, /dev/sda will always be assigned to the primary drive in Linux.

If you have multiple drives, you can list out the partitions in a different drive using /dev/sdb, /dev/sdc, etc.

Listing all fdisk commands

The below command can be used to see all the available commands for the partition /dev/sda.

sudo fdisk /dev/sda

When you issue the above command, it will prompt you to enter a command for fdisk. Upon pressing “m”, you will be presented with the help screen as below:

You can also list all the available commands of fdisk:

sudo fdisk -h

Create a partition

When you want to create a partition under a specific drive, you can proceed as follows :

sudo fdisk /dev/sda

Now press “n” to create a new partition and then “p” to make a primary partition or “e” to create an extended partition. After you are satisfied with the partitions, you have to press “w” in order to write the partition table.

Delete a partition

When you want to free up some space on your drive, you can use the command:

sudo fdisk /dev/sda

And then type in “d” to delete a partition. Once you press d, you will be asked to enter the partition number that you would like to delete with all the available partition numbers. Here I have only one partition so it will automatically delete that single partition without asking.

The changes will not be reflected unless you press “w”.

Note: This command is destructive in nature, please use with proper caution. Also, ensure that you have a proper backup of the system in case something goes wrong.

Size of the partition

You can also check the size of the partition on a specific drive using below method:

sudo fdisk -s /dev/sda

This will show the size of the drive in MB (MegaBytes).

Fix partition table order

When you delete a logical partition and create it once again, you may see an error message like “partition entries are misaligned.”

Let’s take an example. Say you had three logical partitions, sda2, sda3, and sda4. Now you delete these partitions at some point in time and create a single new partition. Logically it should be named sda2, but it creates a new partition sda3. The reason being, when you deleted the previous partitions, the system moved the sda5 to the position of sda2 and free space gets shifted to the end of the table.

To fix such issue, use the below method which starts by running the command:

sudo fdisk /dev/sda

When prompted, press “x” and you will be presented with expert mode. When you are again asked to enter a command, press “f” and it will fix the partition tables, but will not reflect on the partition table unless you write it onto a disk with “w”.

Here I have only one partition and that too is in order in which it should be, so it will just print out the error and do nothing.

Disable boot flag

If you need to disable a boot flag from any particular drive, you can do it through fdisk.

sudo fdisk /dev/sda

When asked to enter the command, enter “p” to list all the partitions. Notice here the “*” sign in the Boot column. It denotes that this particular partition is the current boot partition.

When again asked for command, enter “a” to remove the boot flag. Notice the absence of “*” in the Boot column in the below image.

Afterwords

We have learned the basics of fdisk command but there are many more options under expert mode (command “x”) which you can explore for more detailed information (using man fdisk).

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu
Show Buttons
Hide Buttons