fdisk Command in Linux: Create Disk Partitions

By 

•

Updated on

•

14 min read

Terminal showing fdisk partition table output

The first thing you need to do after installing a new SSD or hard disk is to partition it. A drive needs to have at least one partition before you can format it and store files on it.

In Linux, there are several tools that you can use to create partitions, with fdisk being the most commonly used one. fdisk is a menu-driven command-line utility that allows you to create and manipulate partition tables on a storage device.

To create a partition in Linux with fdisk, open the target disk with sudo fdisk /dev/sdX, create a partition table if needed, add a new partition with n, review the layout with p, and write the changes with w.

This guide covers the fdisk command in Linux. If you prefer a more visual interface or need advanced features, consider tools such as cfdisk, parted , or gdisk.

Be aware that fdisk is a dangerous tool and should be used with extreme caution. Only root or users with sudo privileges can manipulate the partition tables.

Syntax

The general syntax for the fdisk command is:

txt
fdisk [OPTIONS] DEVICE

For example, to open /dev/sdb:

Terminal
sudo fdisk /dev/sdb

The options you will use most often are:

  • -l, --list - Print the partition tables of the given devices and exit.
  • -x, --list-details - Like -l, but with more details, such as partition UUIDs and names.
  • -o, --output - Choose the columns to print, for example -o Device,Size,Type.
  • -t, --type - Recognize only the given partition table type, such as gpt or dos.
  • -w, --wipe - Control when old filesystem and partition table signatures are wiped from the device: auto, always, or never.
  • --bytes - Print sizes in bytes instead of human-readable units.

Run fdisk --help to see the complete list of options.

List Partitions

To list the partition table of a device, invoke the fdisk command with the -l option, followed by the device name. For example, to list the /dev/sda partition table and partitions, you would run:

Terminal
sudo fdisk -l /dev/sda

When no device is given as an argument, fdisk will print partition tables of all devices listed in the /proc/partitions file:

Terminal
sudo 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 output above shows the current partition tables of all devices that are attached to your system. Generally, SATA device names follow the pattern /dev/sd[a-z], while NVMe device names follow /dev/nvme0n1 and their partitions are named /dev/nvme0n1p1, /dev/nvme0n1p2, and so on.

Create a Partition Table and Partitions

Before opening fdisk, identify the disk you want to partition. The safest way to start is to list only whole disks with lsblk :

Terminal
lsblk -d -o NAME,SIZE,MODEL
output
NAME          SIZE MODEL
sda         465.8G WDC WD5000AAKS-0
sdb         298.1G External USB 3.0
nvme0n1     232.9G Samsung SSD 960 EVO 250GB

In this example, /dev/sdb is the external disk we want to partition. Use the whole disk path, such as /dev/sdb, not an existing partition path such as /dev/sdb1.

To start partitioning the drive, run fdisk with the device name. In this example we will work on /dev/sdb:

Terminal
sudo fdisk /dev/sdb

The command prompt will change, and the fdisk dialogue where you can type in commands will open:

output
Welcome to fdisk (util-linux 2.41.3).
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 (MBR) disklabel with disk identifier 0x3dceae47.

Command (m for help):

On a blank disk, fdisk creates a DOS (MBR) partition table in memory. Nothing has been written to the disk yet, and we will replace it with a GPT table in a moment.

Tip
Changes you make to the partition table will not take effect until you write them with the w command. You can exit the fdisk dialogue without saving the changes using the q command.

To get a list of all available commands enter m:

output
Command (m for help): m
output
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
   e   resize a partition
   T   discard (trim) sectors

  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 MBR (DOS) partition table
   s   create a new empty Sun partition table

The first group changes with the partition table type. On a GPT disk, it lists GPT commands instead of the DOS (MBR) ones.

The T command discards sectors immediately after you confirm the operation. It does not wait for w, and quitting with q cannot undo it.

If you are partitioning a new drive, create the partition table before creating partitions. Skip this step if the device already has a partition table and you want to keep it.

fdisk supports several partitioning schemes. MBR and GPT are the two most popular partition table standards, and they store partition information on a drive in different ways. GPT is the better choice for most modern systems. The main points to consider when choosing what partitioning standard to use are:

  • Use MBR to boot the disk in legacy BIOS mode.
  • Use GPT to boot the disk in UEFI mode.
  • The MBR standard supports creating a disk partition up to 2 TiB. If you have a disk of 2 TiB or larger, use GPT.
  • MBR has a limit of 4 primary partitions. If you need more partitions, one of the primary partitions can be set as an extended partition and hold additional logical partitions. With GPT, you can have up to 128 partitions. GPT does not support extended or logical partitions.

In this example, we will use a GPT partition table.

Enter g to create a new empty GPT partition table:

output
Command (m for help): g
output
Created a new GPT disklabel (GUID: 4649EE36-3013-214E-961C-51A9187A7503).

The next step is to create the new partitions.

We will create two partitions. The first one with a size of 100 GiB and the second one will take the rest of the disk space.

Run the n command to create a new partition:

output
Command (m for help): n

You will be prompted to enter the partition number. Hit “Enter” to use the default value (1):

output
Partition number (1-128, default 1):

Next, the command will ask you to specify the first sector. In most cases, use the default value so fdisk can align the partition correctly. Hit “Enter” to use the default value (2048):

output
First sector (2048-625142414, default 2048):

On the next prompt, you will need to enter the last sector. You can use an absolute value for the last sector or a relative value to the start sector, using the + symbol followed by the partition size. The size can be specified in kibibytes (K), mebibytes (M), gibibytes (G), tebibytes (T), or pebibytes (P).

Enter +100G to set the partition size to 100 GiB:

output
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-625142414, default 625141759): +100G
output
Created a new partition 1 of type 'Linux filesystem' and of size 100 GiB.

By default, the type of the new partition is set to “Linux filesystem”, which is fine for most Linux data partitions.

Change Partition Types

Some partitions need a different type. For example, an EFI System partition, swap partition, LVM physical volume, or RAID member should use the matching partition type so tools and installers can recognize it correctly.

To list the available fdisk partition types, enter l:

output
Command (m for help): l

For GPT disks, common partition types include Linux filesystem, Linux swap, EFI System, Linux LVM, and Linux RAID. For MBR disks, fdisk shows numeric IDs such as 83 Linux, 82 Linux swap / Solaris, and 8e Linux LVM.

For a regular data partition, skip this step and leave the type as Linux filesystem. If you are creating a swap partition instead, enter t, choose the partition number, and then enter the type name, alias, or code shown by l:

output
Command (m for help): t
Selected partition 1
Partition type or alias (type L to list all): swap
Changed type of partition 'Linux filesystem' to 'Linux swap'.

The disk has only one partition, so fdisk selects it without asking. When there are several partitions, it prompts for the partition number first.

Let us create the second partition that will take the rest of the disk space:

output
Command (m for help): n

Use the default values for the partition number, first and last sectors. This will create a partition that will use all available space on the disk.

output
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 625141759):
output
Created a new partition 2 of type 'Linux filesystem' and of size 198.1 GiB.

The default last sector stops a few sectors short of the end of the disk. fdisk ends the partition on a 1 MiB boundary, and the last sectors of a GPT disk hold the backup partition table.

Once done creating partitions, use the p command to display the new partition table:

output
Command (m for help): p
output
Disk /dev/sdb: 298.09 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: 4649EE36-3013-214E-961C-51A9187A7503

Device         Start       End   Sectors   Size Type
/dev/sdb1       2048 209717247 209715200   100G Linux filesystem
/dev/sdb2  209717248 625141759 415424512 198.1G Linux filesystem

Save the changes by running the w command:

output
Command (m for help): w

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

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

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

Deleting a Partition

To delete a partition, open the disk with fdisk and enter d. When the disk has more than one partition, fdisk asks which one to delete:

output
Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 has been deleted.

Deleting a partition removes its entry from the partition table and makes the data inaccessible, so make sure you have a backup. As with every other change, nothing is written to the disk until you enter w.

Activating the Partitions

Now that the partitions have been created, the next step is to format the partitions and mount them to the system’s directory tree.

We will format both partitions to ext4. Formatting erases data on the target partition, so double-check the device names before running these commands:

Terminal
sudo mkfs.ext4 /dev/sdb1
sudo mkfs.ext4 /dev/sdb2

The output for the first partition looks like this:

output
mke2fs 1.47.2 (1-Jan-2025)
Discarding device blocks: done
Creating filesystem with 26214400 4k blocks and 6553600 inodes
Filesystem UUID: d25ed86f-1e82-4290-8ddb-0f48f48f7dfe
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 (131072 blocks): done
Writing superblocks and filesystem accounting information: done

If a partition still holds an old filesystem, mkfs.ext4 asks for confirmation before overwriting it. Check the device name again before you answer.

In this example, we will mount the partitions to /mnt/audio and /mnt/video directories.

Create the mount points with mkdir :

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

Mount the new partitions:

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

Partitions will stay mounted until you unmount them or shutdown the machine. To automatically mount a partition when your Linux system starts up, add an entry for it to the /etc/fstab file. Refer to the partition by its UUID. Device names such as /dev/sdb1 can change between boots, but the UUID stays the same. To print the UUID, use blkid:

Terminal
sudo blkid /dev/sdb1
output
/dev/sdb1: UUID="d25ed86f-1e82-4290-8ddb-0f48f48f7dfe" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="b80b85ca-39ae-4320-ae22-dd8f69b41e43"

The UUID value is the one to use in /etc/fstab.

You can now use the new partitions to store files.

Resizing Partitions

In util-linux 2.40 and later, fdisk can grow or shrink a partition with the e command. Check your version with fdisk --version; earlier versions do not have this command. It changes only the partition entry, not the filesystem inside it, and it can grow a partition only into free space that directly follows it.

In the example below, the second partition is 1 GiB and the rest of the disk is free. Enter e, choose the partition, and type the new size. Press “Enter” without a value to use all free space after the partition:

output
Command (m for help): e
Partition number (1,2, default 2): 2
New <size>{K,M,G,T,P} in bytes or <size>S in sectors (default 3G):
Partition 2 has been resized.

After you write the table with w, grow the filesystem to fill the new space. For ext4, use resize2fs:

Terminal
sudo resize2fs /dev/sdb2

Shrinking works in the opposite order. For ext4, unmount the filesystem first, shrink the filesystem, and then shrink the partition. You can grow a mounted ext4 filesystem, but you cannot shrink it while it is mounted. If the partition ends up smaller than the filesystem, you will lose data. When the free space sits before the partition, use gparted. It can move partitions as well as resize them.

Quick Reference

For a printable quick reference, see the fdisk cheatsheet .

CommandDescription
sudo fdisk -lList partition tables for all detected disks
sudo fdisk -l /dev/sdXList the partition table for one disk
sudo fdisk /dev/sdXOpen a disk in interactive fdisk mode
mShow help and available interactive commands
pPrint the current in-memory partition table
nCreate a new partition
dDelete a partition
eResize a partition (util-linux 2.40 and later)
lList available partition types
tChange a partition type
gCreate a new GPT partition table
oCreate a new MBR (DOS) partition table
wWrite changes to disk and exit
qQuit without saving changes

Troubleshooting

Re-reading the partition table failed.: Device or resource busy
fdisk wrote the new table, but the kernel is still using the old one. This happens when a partition on that disk is mounted. Unmount the partitions on that disk with sudo umount /dev/sdXN, and then run sudo partprobe /dev/sdX. If the disk holds the root filesystem, reboot the system.

Partition #1 contains a ext4 signature
The new partition starts where an old filesystem used to be. Answer Y to remove the old signature when the table is written. Answer N only when you are recreating a partition at the same start sector and want to keep its data.

fdisk: cannot open /dev/sdX: Permission denied
Reading and writing partition tables requires root access. Run fdisk with sudo, including when you only list partitions with -l.

FAQ

What is the difference between MBR and GPT?
MBR is the legacy partitioning scheme with a 2 TiB disk size limit and up to four primary partitions. GPT is the modern standard that supports larger disks and many more partitions, and it is the standard layout on UEFI systems.

How do I convert MBR to GPT?
Back up the disk, then recreate the partition table as GPT and restore the data. For in-place conversion in some cases, use tools such as gdisk or sgdisk, but always verify compatibility and keep a backup.

Can I partition a disk without losing data?
It depends on the existing layout and free space. Partitioning can be destructive, so use reliable backups and test on non-critical disks before making changes.

What is the difference between fdisk and parted?
fdisk is a menu-driven tool that keeps partition-table edits in memory until you write them with w. parted applies most changes immediately and can run a full command from the shell prompt, so it is easier to use in scripts.

Conclusion

fdisk covers most everyday partition work: listing disks, switching between MBR and GPT, adding, removing, and resizing partitions, and changing types. For scripted partitioning, see our guide on the parted command .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page