Fdisk Command in Linux (Create Disk Partitions)

Introduction

After you've installed a new SSD or hard drive, the first thing you should do is partition it. Before you can format and store files on a drive, it must have at least one partition.

To create partitions in Linux, there are numerous programs available, the most popular of which being fdisk.

fdisk is a command-line application that allows you to build and manipulate partition tables on a hard disc using a menu-driven interface.

Keep in mind that fdisk is a potentially harmful tool that should only be used with great caution. The partition tables can only be manipulated by root or users with sudo capabilities.

In this tutorial, you will learn about fdisk command in Linux. We will also address a few FAQs on fdisk command in Linux.

List Partitions

To view a device's partition table, use the fdisk command with the -l option and the device name. To list the partition table and partitions on /dev/sda, for example, type:

fdisk -l /dev/sda

fdisk will output partition tables for all devices listed in the /proc/partitions file if no device is specified as an argument:

fdisk -l
Output

Disk /dev/nvme0n1: 232.91 GiB, 250059350016 bytes, 488397168 sectors
Disk model: Samsung SSD 960 EVO 250GB               
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: 6907D1B3-B3AB-7E43-AD20-0707A656A1B5

Device            Start       End   Sectors   Size Type
/dev/nvme0n1p1     2048   1050623   1048576   512M EFI System
/dev/nvme0n1p2  1050624  34605055  33554432    16G Linux swap
/dev/nvme0n1p3 34605056 488397134 453792079 216.4G Linux filesystem


Disk /dev/sda: 465.78 GiB, 500107862016 bytes, 976773168 sectors
Disk model: WDC WD5000AAKS-0
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: 0x0001cca3

Device     Boot Start       End   Sectors   Size Id Type
/dev/sda1        2048 976771071 976769024 465.8G 83 Linux

The given output displays the current partition tables of all attached devices to your system. SATA device names are usually in the format /dev/sd[a-z], while NVMe device names are in the format /dev/nvme[1-9]n[1-9].

Creating Partition Table

Run fdisk using the device name to begin partitioning the drive. We'll use /dev/sdb in this example:

fdisk /dev/sdb

The command prompt will change, and the fdisk dialogue box will appear, allowing you to type commands:

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):
👉
Changes to the partition table won't take effect until you use the w command to write them down. Using the q command, you can quit the fdisk dialogue without saving your modifications.

Enter m, to get a list of all possible commands:

m

If you're partitioning a fresh drive, you must first construct a partition table before proceeding to create partitions. If the device already has a partition table and you wish to keep it, skip this step.

fdisk has a number of partitioning options. The two most common partition scheme standards are MBR and GPT, which store partitioning information on a drive in distinct ways. GPT is a newer standard that offers numerous benefits over MBR. The following are the most important factors to consider when deciding which partitioning standard to use:

  • To boot the disc in classic BIOS mode, use MBR.
  • To boot the disc in UEFI mode, use GPT.
  • The MBR standard allows you to create disc partitions up to 2 terabytes in size. Use GPT if your disc is 2 TiB or greater.
  • The maximum number of primary partitions in MBR is four. If you require more partitions, you can make one of the primary partitions an extended partition that can store logical partitions. You can have up to 128 partitions with GPT. Extended or logical partitions are not supported by GPT.

We'll use a GPT partition table in this example.

To create a new empty GPT partition table, type g:

g

This is what the output will look like:

Output

Created a new GPT disklabel (GUID: 4649EE36-3013-214E-961C-51A9187A7503).

The following step is to make new partitions.

We'll divide the room into two sections. The first will take up 100 GiB of disc space, while the second will take up the remaining space.

To make a new partition, use the n command:

n

You will be asked for the partition number. To use the default value (1), press "Enter":

Partition number (1-128, default 1):

The command will then ask you to choose the first sector. Using the default values for the first variable is often recommended. To use the default value (2048), press "Enter":

First sector (2048-500118158, default 2048):

You must input the last sector at the next prompt. Using the + sign followed by the partition size, you can use an absolute number for the last sector or a relative value for the start sector. Kibibytes (K), mebibytes (M), gibibytes (G), tebibytes (T), or pebibytes (P) can be used to specify the size (P).

To set the partition size to 100 GiB, type +100G:

Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-500118158, default 500118158): +100G
Output

Created a new partition 1 of type 'Linux filesystem' and of size 100 GiB.

The new partition's type is set to "Linux file system" by default, which should suffice in most circumstances. To change the type, press l to bring up a list of partition kinds, then t to change it.

Let's make the second partition, which will occupy the remaining disc space:

n

For the partition number, first and final sectors, use the default values. This will build a partition that takes up the entire disc space.

Partition number (2-128, default 2): 
First sector (209717248-625142414, default 209717248): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (209717248-625142414, default 625142414): 

After you've finished generating partitions, type p to see the new partition table:

p
Output

Disk /dev/sdb: 298.9 GiB, 320072933376 bytes, 625142448 sectors
Disk model: nal USB 3.0     
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: F8365250-AF58-F74E-B592-D56E3A5DEED1

Device         Start       End   Sectors   Size Type
/dev/sdb1       2048 209717247 209715200   100G Linux filesystem
/dev/sdb2  209717248 625142414 415425167 198.1G Linux filesystem
👉
The d command can be used to delete a partition.

Run the w command to save your changes:

w

The command will exit the fdisk menu and write the table to the disc.

Output

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

The device partition table will be read by the kernel without the need to reboot the system.

Activating the Partitions

Following the creation of the partitions, the next step is to format them and mount them in the system's directory tree.

Both partitions will be formatted to ext4:

sudo mkfs.ext4 -F /dev/sdb1
sudo mkfs.ext4 -F /dev/sdb2
Output

mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 51928145 4k blocks and 12984320 inodes
Filesystem UUID: 63a3457e-c3a1-43f4-a0e6-01a7dbe7dfed
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done   

The partitions will be mounted to the /mnt/audio and /mnt/video directories in this example.

mkdir can be used to create mount points:

sudo mkdir -p /mnt/audio /mnt/video 

Now, mount the new partition:

sudo mount /dev/sdb1 /mnt/audio
sudo mount /dev/sdb2 /mnt/video 

Partitions will remain mounted until they are unmounted or the machine is shut down. Define a mount in the /etc/fstab file to automatically mount a partition when your Linux system boots up.

You can now store your files on the new partitions.

FAQs on fdisk command in Linux

How can I list all the partitions on my system using fdisk? 

To list all partitions on your system, run the command fdisk -l. This will display information about all the disks and their partitions in your system.

How can I create a new partition using fdisk? 

You can create a new partition by running the fdisk command with the disk drive as an argument, followed by the partition creation steps. For example, fdisk /dev/sda opens fdisk for the '/dev/sda' drive. Then you can use the available options to create a new partition.

How can I delete a partition using fdisk? 

To delete a partition, you need to run fdisk with the disk drive as an argument. Then type d to delete a partition, specify the partition number, and follow the prompts to delete it.

How can I change the partition type using fdisk? 

You can change the partition type by selecting the partition using fdisk and then typing t to change the partition system id. Specify the partition number and the new partition type according to the available options.

Is it possible to resize a partition using fdisk? 

No, fdisk does not support resizing existing partitions. You would need to use tools like resize2fs for resizing ext2/3/4 partitions, or parted for resizing other types of partitions.

How can I save the changes I made to the partition table using fdisk? 

The changes made to the partition table are saved automatically when you exit fdisk. To exit and save the changes, press w.

Can I use fdisk on any storage device in Linux? 

Yes, fdisk can be used on most storage devices, including hard drives, solid-state drives (SSDs), USB drives, and memory cards. Just make sure to correctly specify the device as an argument when running the command.

Conclusion

The command-line utility fdisk is used to create partition schemes. Type man fdisk in your terminal to learn more about the fdisk command.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.