In this tutorial, we are going to learn how to use fdisk
partitioning tool on Linux. fdisk
is a dialog-driven program for creation and manipulation of partition tables. It understands GPT, MBR, Sun, SGI and BSD partition tables.
Syntax;
fdisk [options] device
fdisk -l [device...]
How to Use fdisk Partitioning Tool on Linux
Getting To Know MBR and GPT
There are two main ways of storing partition information on hard disks. The first one is MBR (Master Boot Record), and the second one is GPT (GUID Partition Table).
MBR
This is a remnant from the early days of MS-DOS (more specifically, PC-DOS 2.0 from 1983) and for decades was the standard partitioning scheme on PCs. The partition table is stored on the first sector of a disk, called the Boot Sector, along with a boot loader, which on Linux systems is usually the GRUB bootloader. But MBR has a series of limitations that hinder its use on modern systems, like the inability to address disks of more than 2 TB in size, and the limit of only 4 primary partitions per disk.
GUID
A partitioning system that addresses many of the limitations of MBR. There is no practical limit on disk size, and the maximum number of partitions are limited only by the operating system itself. It is more commonly found on more modern machines that use UEFI instead of the old PC BIOS.
Managing MBR Partitions with fdisk
The most common command-line partitioning tool is the fdisk
utility. The fdisk
program allows you to create, view, delete, and modify partitions on any drive that uses the MBR method of indexing partitions. To use it, type fdisk
followed by the device name corresponding to the disk you want to edit. For example,
fdisk /dev/sda
The above command would edit the partition table of the first SATA-connected device (sda
) on the system. You need to specify the device corresponding to the physical disk, not one of its partitions (like /dev/sda1
).
NOTE; All disk-related operations need to be done as the user root
(the system administrator), or with root privileges using sudo
.
fdisk
when invoked, will show a greeting then a warning and it will wait for your commands. For example;
sudo fdisk /dev/sdb
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help):
The warning is important. You can create, edit or delete partitions at will, but nothing will be written to disk unless you use the write (w
) command. To exit fdisk
without saving changes, use the q
command.
Listing Partition Tables For Specific Devices
Use -l
option to list the partition tables for the specified devices and then exit. If no devices are given, those mentioned in /proc/partitions (if that file exists) are used.
sudo fdisk -l /dev/sdb
Disk /dev/sdb: 15.45 GiB, 16580567040 bytes, 32383920 sectors
Disk model: VBOX HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 9618E03F-8D0D-42B3-A70F-3655453B6929
Device Start End Sectors Size Type
/dev/sdb1 2048 32383886 32381839 15.5G Linux swap
Printing the Current Partition Table
Use p
command to print the current partition table. We obtain the following output;
Command (m for help): p
Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 sectors
Disk model: VBOX HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xfcdd6514
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 52426751 52424704 25G 83 Linux
In the above output, we obtain the following columns;
Device
: The device assigned to the partition.Boot
: Shows whether the partition is “bootable” or not.Start
: The sector where the partition starts.End
: The sector where the partition ends.Sectors
: The total number of sectors in the partition. Multiply it by the sector size to get the partition size in bytes.Size
: The size of the partition in “human readable” format. In the example above, values are in gigabytes.Id
: The numerical value representing the partition type.Type
: The description for the partition type.
Primary vs Extended Partitions
On an MBR disk, you can have 2 main types of partitions, primary and extended. You can have only 4 primary partitions on the disk, and if you want to make the disk “bootable”, the first partition must be a primary one. One way to work around this limitation is to create an extended partition that acts as a container for logical partitions. You could have, for example, a primary partition, an extended partition occupying the remainder of the disk space and five logical partitions inside it. For an operating system like Linux, primary and extended partitions are treated exactly in the same way, so there are no “advantages” of using one over the other.
Creating a Partition with Fdisk command in Linux
Use n
command to create a partition. By default, partitions will be created at the start of unallocated space on the disk. You will be asked for the partition type (primary or extended), first sector and last sector.
For the first sector, you can usually accept the default value suggested by fdisk
, unless you need a partition to start at a specific sector. Instead of specifying the last sector, you can specify a size followed by the letters K
, M
, G
, T
or P
(Kilo, Mega, Giga, Tera or Peta). So, if you want to create a 6 GB partition, you could specify +6G
as the Last sector
, and fdisk
will size the partition accordingly.
example;
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-128, default 2): 2
First sector (10487808-32383886, default 10487808): 10487808
Last sector, +/-sectors or +/-size{K,M,G,T,P} (10487808-32383886, default 32383886): +6G
Created a new partition 2 of type 'Linux filesystem' and of size 6 GiB.
Checking for Unallocated Space with fdisk
Use the F
command to show the unallocated space, if you do not know how much free space there is on the disk.
example;
Command (m for help): F
Unpartitioned space /dev/sdb: 6.45 GiB, 6915825152 bytes, 13507471 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
Start End Sectors Size
18876416 32383886 13507471 6.5G
Deleting Partitions with fdisk
To delete a partition, use the d
command. fdisk
will ask you for the number of the partition you want to delete, unless there is only one partition on the disk. In this case, this partition will be selected and deleted immediately.
example;
Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 has been deleted.
Changing the Partition Type with fdisk
Use t
command to change the partition type. The partition type must be specified by its corresponding hexadecimal code, and you can see a list of all the valid codes by using the command L
.
example;
Command (m for help): t
Selected partition 1
Partition type (type L to list all types): 19
Changed type of partition 'Linux filesystem' to 'Linux swap'.
Print Partition Information with fdisk
Use i
command to print information about a partition.
example;
Command (m for help): i
Partition number (1,2, default 2): 1
Device: /dev/sdb1
Start: 2048
End: 12584959
Sectors: 12582912
Size: 6G
Type: Linux filesystem
Type-UUID: 0FC63DAF-8483-4772-8E79-3D69D8477DE4
UUID: 3D2E073F-761C-CD41-A9EF-74E8E5A8EA55
Verifying Partition Table with fdisk
Use v
command to verify the partition table.
example;
Command (m for help): v
No errors detected.
Header version: 1.0
Using 2 out of 128 partitions.
A total of 13509485 free sectors is available in 2 segments (the largest is 6.5 GiB).
Writing Data with fdisk
Using w
command will write data. Use this command to save your changes.
example;
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
Displaying Help
To dispaly help, use m
command.
example;
Command (m for help): m
Help:
GPT
M enter protective/hybrid MBR
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
Conclusion
This marks the end of our tutorial on how to use fdisk Partitioning Tool on Linux.
For further understanding of fdisk
command, feel free to check:
man fdisk
Related Articles: