How To's

Format a Disk via fdisk (Linux)

70,140 views · 13 found this helpful · 4 did not

Was this helpful?

Problem

Format a disk using the Linux fdisk utility.

tl;dr

To list disks:

fdisk -l

Assuming you want to format disk sdb:

sudo fdisk /dev/sdb

Enter write if it prompts you to with error message. You then need to erase any existing partitions. Type in d, which will prompt you to select a partition. Enter the number of any defined partitions on the disk. If you don't know which partitions are defined, just use d on all of them.

d
1

Repeat this for all partitions on the disk. Then, create a primary partition:

write
fdisk /dev/sdb
n
p

Follow the prompts. Enter write to write all of your changes to disk.

write

Solution

Fdisk allows you to format disks with an MBR partition table less. Therefore, this will only work on disks 2 TB in size or less if you wish to utilize their full capacity. Your primary disk is usually sda. If you have more than one disk, you may wish to find out which letter the disk you are about to format has. You can do this via fdisk -l command.

# fdisk -l

Disk /dev/sda: 180.0 GB, 180045766656 bytes
255 heads, 63 sectors/track, 21889 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b6aa7

Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 1070 8388608 82 Linux swap / Solaris
/dev/sda3 1070 21890 167231488 83 Linux

Disk /dev/sdb: 40.0 GB, 40020664320 bytes
255 heads, 63 sectors/track, 4865 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

As you can see from above, your root partition is mounted to /dev/sda, and your secondary disk is mounted to /dev/sdb. sdb does not contain any partitions and filesystems, and so it must be formatted before it can be used. To begin the format process, type in:

fdisk /dev/sdb

You may see this error message:

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

This happens when your disk is completely empty. You will need to create a partition on it:

n
p
1

Hit Enter to use default values, or enter your own custom values for first and last sectors. You can also specify partition size instead by specifying disk size and units, preceded by a + sign. For example, this will create a single 20 GB partition:
Last cylinder, +cylinders or +size{K,M,G} (1-4865, default 4865): +20G

Make sure to write your changes!

write

This will save your changes to disk. To see the newly created partitions, you can use fdisk -l again.

# fdisk -l
Disk /dev/sdc: 40.0 GB, 40020664320 bytes
255 heads, 63 sectors/track, 4865 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x7d9e837c

Device Boot Start End Blocks Id System
/dev/sdc1 1 2612 20980858+ 83 Linux

Example

This will create two partitions, first one 20 GB and second one to maximum available size.

fdisk -l
fdisk /dev/sdb
w
n
1

Hit [Enter] to use default value for first cylinder

+20G
n
2

Hit [Enter] twice to use default value for first cylinder and size

w

  • Updated

← All articles

Still stuck?

Our engineers answer tickets directly, 24/7.