fdisk Command: Creating and Deleting MBR Partitions and Checking with fdisk -l

Test environmentRHEL familyAlmaLinux 9.8root account
Disk · mount commandsfdiskLocation/usr/sbin/fdisk

The fdisk command is a Linux command that checks a disk's partition table and creates or deletes partitions. On a 1 GiB practice image file, an MBR partition table was created and split into two primary partitions, an extended partition and two logical partitions inside it, then saved with w, and fdisk -l confirmed that five partitions had been written.

In the fdisk -l output of the server disk below, the disk name, size, model and identifier were changed to example values, and the patch number after util-linux 2.37 on the first fdisk screen and the working folder /root/work are also example values. The commands that create, delete and save partitions were all run on a practice image file made with truncate.

1️⃣ Basic Usage of the fdisk Command and Checking Partitions with fdisk -l

fdisk is run in two ways. With -l, it prints only the list of disks and partitions and exits. Given a device name without -l, it opens an interactive screen for editing partitions.

Running fdisk -l without arguments prints all the disks attached to the server.

fdisk -l
Disk /dev/nvme0n1: 40 GiB, 42949672960 bytes, 83886080 sectors
Disk model: Example Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 6F1C2A90-3B7E-4D52-9A1E-2C8B5D0F7E41

Device           Start      End  Sectors  Size Type
/dev/nvme0n1p1    2048     4095     2048    1M BIOS boot
/dev/nvme0n1p2    4096   413695   409600  200M EFI System
/dev/nvme0n1p3  413696  2510847  2097152    1G Linux filesystem
/dev/nvme0n1p4 2510848 83886046 81375199 38.8G Linux filesystem

The first line shows the disk name, the total size and the number of sectors, and the [Disklabel type] line is the partition table type. This disk was [gpt], and an MBR partition table appears as [dos]. GPT disks can also be partitioned with the gdisk command. This article is based on the MBR (dos) partition table.

The meaning of each column in the partition list is shown in the table below. The list for the GPT disk had no [Boot] or [Id] column.

FieldMeaning
DevicePartition device name. The partition number follows the disk name (on nvme disks, the number follows p, as in p1)
BootMarked with * on a partition whose boot flag is on. A column found only in MBR (dos) partition tables
Start · EndSector numbers where the partition starts and ends
SectorsNumber of sectors in the partition. Multiplied by the sector size of 512 bytes, it gives the size in bytes
SizeSize converted to an easy-to-read unit
IdPartition type number (hexadecimal). A column found only in MBR (dos) partition tables
TypePartition type name

The practice image is an empty 1 GiB file made with truncate. fdisk also accepts a file path instead of a device name, so it can be used for practice on the same screen without touching a real disk.

truncate -s 1G /root/work/disk.img
fdisk -l /root/work/disk.img
Disk /root/work/disk.img: 1 GiB, 1073741824 bytes, 2097152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

The file has no partition table yet, so only four lines of disk information appeared.

🔲 fdisk Command Options

OptionCopyDescription
-l, --listPrint only the list of disks and partitions and exit
-o, --outputlistPrint only the desired columns, given as column names separated by commas
OptionCopyDescription
-b, --sector-sizesectorsizeSpecify the sector size of the disk. Values are 512, 1024, 2048, 4096
-B, --protect-bootDo not wipe the beginning of the first sector of the disk when creating a new partition table. Supported for GPT and MBR
-c, --compatibility[=mode]Set the compatibility mode to dos or nondos. The default is nondos
-h, --helpPrint help and exit
-L, --color[=when]Colorize the output. when is auto, never, always
-x, --list-detailsSame as --list, but print more detailed information
--lock[=mode]Use an exclusive BSD lock on the device or file. mode is yes, no, nonblock
-n, --noauto-ptDo not automatically create a default partition table on an empty device
-s, --getszPrint the size of a block device as a number of 512-byte sectors. Deprecated (blockdev recommended)
-t, --typetypeSupport only partition tables of the given type and no other types
-u, --units[=unit]Show sizes in units of sectors or cylinders when printing the partition table. The default is sectors
-C, --cylindersnumberSpecify the number of cylinders of the disk
-H, --headsnumberSpecify the number of heads of the disk. A value written to the partition table, separate from the number of physical heads; 255 or 16 is suitable
-S, --sectorsnumberSpecify the number of sectors per track. A suitable value is 63
-w, --wipewhenWipe file system, RAID and partition table signatures from the device. when is auto, never, always
-W, --wipe-partitionswhenWipe file system, RAID and partition table signatures from newly created partitions. when is auto, never, always
-V, --versionPrint version information and exit

2️⃣ Creating Partitions and Logical Partitions with fdisk

To create partitions, give only the path without -l to open the interactive screen.

fdisk /root/work/disk.img
Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xf638ab13.

The file had no partition table, so fdisk created a new DOS (MBR) partition table in memory. As the message says, changes made here are not written to the disk until they are saved with w.

A task is chosen by typing a single letter at the [Command (m for help):] prompt. Typing m shows the list of commands.

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  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
   u   change display/entry units
   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

The commands used in this article are shown in the table below.

CommandTask
mPrint the list of commands
pPrint the current partition table
nCreate a new partition
dDelete a partition
tChange a partition's type number
aTurn the boot flag on or off
lPrint the list of type numbers
FPrint free space with no partitions
oCreate a new empty DOS (MBR) partition table
wWrite the changes to the disk and exit
qDiscard the changes and exit

The first partition was created with n. A primary partition (p) and number 1 were chosen, the first sector took the default with Enter, and +300M was entered at the last sector prompt to set the size to 300 MiB.

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 (1-4, default 1): 1
First sector (2048-2097151, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-2097151, default 2097151): +300M

Created a new partition 1 of type 'Linux' and of size 300 MiB.

The first sector default of 2048 is the 1 MiB (2048 × 512 bytes) position. The fdisk manual recommends following the defaults, because the defaults and size values such as +300M are aligned to the device's characteristics.

In the same way, a 200 MiB primary partition 2 was created, and all of the remaining space was taken as extended partition 3. Skipping the last sector with Enter uses the remaining space up to its end.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (616448-2097151, default 616448): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (616448-2097151, default 2097151): +200M

Created a new partition 2 of type 'Linux' and of size 200 MiB.
Command (m for help): n
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): e
Partition number (3,4, default 3): 3
First sector (1026048-2097151, default 1026048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1026048-2097151, default 2097151): 

Created a new partition 3 of type 'Extended' and of size 523 MiB.

An MBR partition table can record only up to four primary partitions. To split a disk into more, one slot is made an extended partition and logical partitions are created inside it. Logical partition numbers start at 5.

Because the extended partition took up all the remaining space, entering n made fdisk add logical partition 5 right away without asking for the type. After the 100 MiB partition 5, partition 6 was created from the remaining space, and with no free sectors left, 7 could not be created.

Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (1028096-2097151, default 1028096): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1028096-2097151, default 2097151): +100M

Created a new partition 5 of type 'Linux' and of size 100 MiB.
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 6
First sector (1234944-2097151, default 1234944): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1234944-2097151, default 2097151): 

Created a new partition 6 of type 'Linux' and of size 421 MiB.
Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 7
No free sectors available.

The result of the split was checked with p. Extended partition 3 (Id 5) is the range that holds logical partitions 5 and 6, so its [Start] and [End] range encloses both.

Command (m for help): p
Disk /root/work/disk.img: 1 GiB, 1073741824 bytes, 2097152 sectors
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: 0xf638ab13

Device               Boot   Start     End Sectors  Size Id Type
/root/work/disk.img1         2048  616447  614400  300M 83 Linux
/root/work/disk.img2       616448 1026047  409600  200M 83 Linux
/root/work/disk.img3      1026048 2097151 1071104  523M  5 Extended
/root/work/disk.img5      1028096 1232895  204800  100M 83 Linux
/root/work/disk.img6      1234944 2097151  862208  421M 83 Linux

After checking, w was entered to write the partition table to the image file.

Command (m for help): w
The partition table has been altered.
Syncing disks.

After [Syncing disks.] appeared, fdisk exited and returned to the shell prompt.

3️⃣ Saving with fdisk w and Quitting Without Saving with q

Changes made in fdisk stay only in memory and are written to the disk the moment w is entered. When the image saved with w was viewed again with fdisk -l, all five partitions appeared as they were.

fdisk -l /root/work/disk.img
Disk /root/work/disk.img: 1 GiB, 1073741824 bytes, 2097152 sectors
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: 0xf638ab13

Device               Boot   Start     End Sectors  Size Id Type
/root/work/disk.img1         2048  616447  614400  300M 83 Linux
/root/work/disk.img2       616448 1026047  409600  200M 83 Linux
/root/work/disk.img3      1026048 2097151 1071104  523M  5 Extended
/root/work/disk.img5      1028096 1232895  204800  100M 83 Linux
/root/work/disk.img6      1234944 2097151  862208  421M 83 Linux

q discards the changes and exits. Partition 2 was deleted with d, p showed that it was gone from the list, and then fdisk was exited with q.

fdisk /root/work/disk.img
Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): d
Partition number (1-3,5,6, default 6): 2

Partition 2 has been deleted.

Command (m for help): p
Disk /root/work/disk.img: 1 GiB, 1073741824 bytes, 2097152 sectors
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: 0xf638ab13

Device               Boot   Start     End Sectors  Size Id Type
/root/work/disk.img1         2048  616447  614400  300M 83 Linux
/root/work/disk.img3      1026048 2097151 1071104  523M  5 Extended
/root/work/disk.img5      1028096 1232895  204800  100M 83 Linux
/root/work/disk.img6      1234944 2097151  862208  421M 83 Linux

Command (m for help): q

When the list was viewed again after exiting with q, the deleted partition 2 was still there.

fdisk -l /root/work/disk.img
Disk /root/work/disk.img: 1 GiB, 1073741824 bytes, 2097152 sectors
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: 0xf638ab13

Device               Boot   Start     End Sectors  Size Id Type
/root/work/disk.img1         2048  616447  614400  300M 83 Linux
/root/work/disk.img2       616448 1026047  409600  200M 83 Linux
/root/work/disk.img3      1026048 2097151 1071104  523M  5 Extended
/root/work/disk.img5      1028096 1232895  204800  100M 83 Linux
/root/work/disk.img6      1234944 2097151  862208  421M 83 Linux

On a real disk, entering w changes that disk's partition table right away. So before saving, check the device name and size once more with fdisk -l.

4️⃣ Changing Partition Types and Deleting Partitions with fdisk

New partitions are created with type number 83 (Linux). For partitions to be used as swap or LVM, the type number is changed with t. Partition 2 was changed to 82 and 6 to 8e.

fdisk /root/work/disk.img
Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): t
Partition number (1-3,5,6, default 6): 2
Hex code or alias (type L to list all): 82

Changed type of partition 'Linux' to 'Linux swap / Solaris'.
Command (m for help): t
Partition number (1-3,5,6, default 6): 6
Hex code or alias (type L to list all): 8e

Changed type of partition 'Linux' to 'Linux LVM'.

Entering l shows the full list of type numbers. The numbers and aliases often used on Linux servers are listed below, and an alias can be entered instead of a number at the [Hex code or alias] prompt.

No.Type · Alias
83Linux · alias linux. Default type for new partitions
82Linux swap / Solaris · alias swap
8eLinux LVM · alias lvm
5Extended · alias extended. Extended partition that holds logical partitions
fdLinux raid autodetect · alias raid
efEFI (FAT-12/16/32) · alias uefi

a turns a partition's boot flag on and off. After it was turned on for partition 1, p showed * in the [Boot] column of partition 1, and the [Id] and [Type] of 2 and 6 had also changed. This result was saved with w.

Command (m for help): a
Partition number (1-3,5,6, default 6): 1

The bootable flag on partition 1 is enabled now.
Command (m for help): p
Disk /root/work/disk.img: 1 GiB, 1073741824 bytes, 2097152 sectors
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: 0xf638ab13

Device               Boot   Start     End Sectors  Size Id Type
/root/work/disk.img1 *       2048  616447  614400  300M 83 Linux
/root/work/disk.img2       616448 1026047  409600  200M 82 Linux swap / Solaris
/root/work/disk.img3      1026048 2097151 1071104  523M  5 Extended
/root/work/disk.img5      1028096 1232895  204800  100M 83 Linux
/root/work/disk.img6      1234944 2097151  862208  421M 8e Linux LVM

Command (m for help): w
The partition table has been altered.
Syncing disks.

d deletes a partition. When the saved image was opened again with fdisk and the nonexistent number 9 was entered, the [Value out of range.] message appeared and the number was asked for again. Deleting extended partition 3 also removed logical partitions 5 and 6, which were inside it.

fdisk /root/work/disk.img
Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Partition number (1-3,5,6, default 6): 9
Value out of range.
Partition number (1-3,5,6, default 6): 3

Partition 3 has been deleted.

Command (m for help): p
Disk /root/work/disk.img: 1 GiB, 1073741824 bytes, 2097152 sectors
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: 0xf638ab13

Device               Boot  Start     End Sectors  Size Id Type
/root/work/disk.img1 *      2048  616447  614400  300M 83 Linux
/root/work/disk.img2      616448 1026047  409600  200M 82 Linux swap / Solaris

This deletion was only checked with p, and fdisk was exited with q, so it was not saved.

🔲 fdisk Command Simulator

In the simulator below, fdisk can be entered directly on the practice image disk.img. Open it with fdisk disk.img, split it into partitions with n, save with w, and then check the result with fdisk -l disk.img.

fdisk command simulatorfdisk · root 계정
[root@server work]#
Use the ↑↓ keys to recall commands you typed earlier.

5️⃣ Practical Examples of the fdisk Command

  • ↪️ Partition work and mistakes tried on a practice image

🔲 fdisk Example: Creating and Deleting Partitions

The steps followed the order of partitioning a new disk. A new 3 GiB image, disk.img, was made with truncate and used. fdisk was run, an empty MBR partition table was created with o, two primary partitions were then created with n, and +1G and +500M were entered at the last sector prompts.

truncate -s 3G disk.img
fdisk disk.img
Command (m for help): o
Created a new DOS disklabel with disk identifier 0x2dcf4156.

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 (1-4, default 1): 1
First sector (2048-6291455, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-6291455, default 6291455): +1G

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

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2099200-6291455, default 2099200):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2099200-6291455, default 6291455): +500M

Created a new partition 2 of type 'Linux' and of size 500 MiB.

Before entering w, the list was viewed with p, and the table was saved after confirming that the 1 GiB and 500 MiB partitions appeared with the sizes that were entered.

Command (m for help): p
Disk disk.img: 3 GiB, 3221225472 bytes, 6291456 sectors
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: 0x2dcf4156

Device     Boot   Start     End Sectors  Size Id Type
disk.img1          2048 2099199 2097152    1G 83 Linux
disk.img2       2099200 3123199 1024000  500M 83 Linux

Command (m for help): w
The partition table has been altered.
Syncing disks.

For deletion, fdisk was run again, 2 was entered for d, p confirmed that only partition 1 was left, and the change was saved with w.

fdisk disk.img
Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): p
Disk disk.img: 3 GiB, 3221225472 bytes, 6291456 sectors
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: 0x2dcf4156

Device     Boot Start     End Sectors Size Id Type
disk.img1        2048 2099199 2097152   1G 83 Linux

Command (m for help): w
The partition table has been altered.
Syncing disks.

🔲 Common Mistakes When Using fdisk

A mistake that is easy to make the first time is exiting with q instead of w. A 500 MiB partition was added again with n, partition 2 was confirmed in the p list, and then fdisk was exited with q; fdisk -l showed only partition 1.

fdisk -l disk.img
Disk disk.img: 3 GiB, 3221225472 bytes, 6291456 sectors
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: 0x2dcf4156

Device     Boot Start     End Sectors Size Id Type
disk.img1        2048 2099199 2097152   1G 83 Linux

The mistake of specifying the wrong disk can be prevented by checking the size and identifier with fdisk -l before starting. When printed together with backup.img, a 5 GiB image with one 2 GiB partition, the [Disk backup.img: 5 GiB] line and the [Disk identifier: 0xd201d03b] line told it apart from disk.img.

fdisk -l disk.img backup.img
Disk disk.img: 3 GiB, 3221225472 bytes, 6291456 sectors
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: 0x2dcf4156

Device     Boot Start     End Sectors Size Id Type
disk.img1        2048 2099199 2097152   1G 83 Linux


Disk backup.img: 5 GiB, 5368709120 bytes, 10485760 sectors
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: 0xd201d03b

Device      Boot Start     End Sectors Size Id Type
backup.img1       2048 4196351 4194304   2G 83 Linux

🔲 Checking the Change in fdisk -l Output

After a partition table was created with o and saved with w, the [Disklabel type: dos] line and the [Disk identifier: 0x2dcf4156] line were added to the fdisk -l output. The identifier was the same as the value shown in the [Created a new DOS disklabel with disk identifier 0x2dcf4156.] line when o was entered.

In this list, partition 2 started at 2099200, the sector right after the [End] 2099199 of partition 1. After partition 2 was deleted, the list shrank to a single line, disk.img1.

Commands such as n, d, w and q can also be entered directly in the fdisk command simulator.

🔢 FAQ & Recommended Content

List the column names, separated by commas, with the -o option. Running [fdisk -l -o Device,Start,Size,Type /root/work/disk.img] showed only four columns: device name, start sector, size and type.

sfdisk, from the same util-linux package, prints the partition table as text when -d is added. Running [sfdisk -d /root/work/disk.img] showed the [label: dos] line along with one line per partition giving its start sector, size and type. Adding [> file name] at the end saves it to a file, and according to the manual, passing that file to sfdisk as input recreates the same partition table.

Yes, it can. Entering g in the interactive screen creates a new GPT partition table. When g was entered on the practice image, the [Created a new GPT disklabel] message appeared along with a notice that the existing dos signature would be wiped when saving with w, and p showed that [Disklabel type] had changed to gpt. fdisk was exited with q, so this result was not saved.

ℹ️ Affiliate Disclosure
This site's content contains affiliate links. When a visitor buys a product or service through one of them, the site receives a commission from the seller. The amount the buyer pays(it goes down during event discounts ↓)does not go up. Posted prices, discounts, and stock reflect the time of writing and may differ, so confirm with the seller before buying. Products are chosen and reviewed by our own standards, and commissions do not affect the order or content of reviews.

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove your humanity: 4   +   3   =