This article will go through the basic usage of fdisk command that helps to manage partition tables in Linux systems. In order to run this command successfully, you need to have root privileges. Adding a word of caution, do not use this command if you don’t know what you are willing to do or it will create a huge mess!
Fdisk Commands
1. Enlisting all Disk Partitions
The most fundamental use of fdisk will be for viewing all the disk partitions of your system and in this regard, option –l is used. This will enlist all the disk partitions in your system along with their device names like /dev/sda, /dev/sdb/and so on.
2. Viewing Specific Disk Partition
To observe a specific disk partition, you will have to use option –l followed by the device name (/dev/sda, /dev/sdb or any other partition) you wish to observe.
Example: sudo fdisk –l /dev/sda
Example: sudo fdisk –l /dev/sda
3. Command Mode Usage
To observe all the options associated with fdisk command, use it without any option (but do not forget to mention device name in the command).
Pressing m key will display all the possible commands that can be applied on the mentioned disk partition.
Pressing m key will display all the possible commands that can be applied on the mentioned disk partition.
4. Printing the Partition Table
To print the current partition table of the system using command mode, just follow a similar procedure but press p key instead. It will print the list of partitions available on the hard disk.
And again, changes won’t take place unless they are saved. Press w to save all the changes to the partition table and reboot the system.
Syntax: mkfs.ext4 <partition_name>
5. Deleting the Partition
In order to delete any partition, option d in the command mode can be used. When d is pressed, it expects the number of partitions (ID of the partition which can be observed using option p) that you want to delete. When you check the partition table upon deleting the partition, deleted partition doesn’t appear there, but the changes remain unsaved unless they are written using w key.6. Creating the Partition
If you are having free space on any one of your devices and you wish to create a new partition under it, you can do this using option n in the command mode.
At the time of creating the partition, you will be asked whether you need a primary partition or a logical (or extended) partition. Press p for the primary partition and l for the logical one. As said earlier, you may create several logical partitions but a maximum of four (4) primary partitions.
You will now need to specify the sector the new partition would start at and the last sector where the partition will end. While specifying the last sector, you can mention the total size that would be occupied by the partition, say for a partition of 50 Gigabytes, you will have to enter “+50G”. Remember, if you don’t use any unit (G for Gigabytes in this case), ‘sectors’ will be considered as the default unit.
And again, changes won’t take place unless they are saved. Press w to save all the changes to the partition table and reboot the system.
7. Formatting the Partition
When the new partition is formed, it is recommended that the user should format the newly created partition with a file system which can be done with the use of ‘mkfs’ command. So as to format a newly created partition, Enter the following command:Syntax: mkfs.ext4 <partition_name>
8. Checking the Partition Size
To check the size of any specific partition, use the option –s which will display the disk space occupied by the partition in terms of blocks.
Syntax: fdisk –s <partition_name>
When you delete some of the partitions (say sda1, sda2 and sda3) and create a new partition which you suppose to take a name as 'sda1', the system would create it with a name 'sda4'. Because, when you delete the partitions, sda4 will be renamed as sda1, sda5 as sda2, and so on, shifting the free space at the end.
To solve the problems associated with partition table order, option x (extra functionality section) can be used followed by option f (fix the partition table order) in the command mode. Do not forget to save the changes using option w.
When you see the current partition table (using p command), there is a boot flag on /dev/sda1 and to enable/disable that, option a can be used.
That’s it! In this article, I tried to cover the basic usage of fdisk command. But this is not the end, fdisk has several expert commands which can be used using option x.
Syntax: fdisk –s <partition_name>
9. Fixing Partition Table Order
When a logical partition is deleted and created again, you may face an error message saying 'Partition out of order' or 'Partition table entries are not in disk order'.When you delete some of the partitions (say sda1, sda2 and sda3) and create a new partition which you suppose to take a name as 'sda1', the system would create it with a name 'sda4'. Because, when you delete the partitions, sda4 will be renamed as sda1, sda5 as sda2, and so on, shifting the free space at the end.
To solve the problems associated with partition table order, option x (extra functionality section) can be used followed by option f (fix the partition table order) in the command mode. Do not forget to save the changes using option w.
10. Disabling Boot Flag
Generally, when fdisk is run, it will show a boot flag symbol i.e. (*) on every partition. To enable/disable this boot flag, follow the steps mentioned below:When you see the current partition table (using p command), there is a boot flag on /dev/sda1 and to enable/disable that, option a can be used.
That’s it! In this article, I tried to cover the basic usage of fdisk command. But this is not the end, fdisk has several expert commands which can be used using option x.
Conclusion
This fdisk utility is very much advantageous at the time of forming some space for new partitions, organizing the space for newly created drives, re-organizing the older partitions, and copying/moving the data to other partitions. Using fdisk you can create at most four new primary partitions and several logical partitions, depending upon the hard disk space available in the system.