Skip to content

Linux fdisk command (changes partitions)

The fdisk command in Linux is a utility for creating, listing, altering, and deleting disk partitions. It is very robust and allows us to create a large list of partition types for Linux and different operating systems. fdisk works in text mode in the form of a friendly dialogue.

fdisk is capable of manipulating GPT, MBR, Sun, SGI, and BSD partition tables.

The fdisk -l option lists all disks and partitions found on the Linux system:

$ sudo <br></br>fdisk -l Disk /dev/xvda: 8 GiB, 8589934592 bytes, 16777216 sectors <br></br>Units: sector of 1 * 512 = 512 bytes <br></br>Sector size (logical/physical): 512 bytes/512 bytes <br></br>I/O size (minimum/optimal): 512 bytes/512 bytes <br></br>Disk label type: gpt <br></br>Identifier of disk: 33E98A7E-CCDF-4AF7-8A35-DA18E704CDD4
<br></br>Device Home End Sectors Size Type <br></br>/dev/xvda1 4096 16777182 16773087 8G Linux file system <br></br>/dev/xvda128 2048 4095 2048 1M BIOS boot <br></br>
Disk /dev/xvdb: 8 GiB, 8589934592 bytes, 16777216 sectors

Units: sector of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes/512 bytes
I/O size (minimum/optimal): 512 bytes/512 bytes

In this example above, the system has two disks xvda and xvdb. The first disk has an 8GB partition and a 1MB boot partition and uses the GPT system. The second 8GB disk has no partitions created.

In Cloud Computing systems, it is common for disks to use the nomenclature “xvd” to designate disks instead of “sd”.

To partition the disk, you must pass the block device you want to work with as a parameter to fdisk. You must have root permissions to change partitions.

# fdisk /dev/xvdb
Welcome to fdisk (util-linux 2.30.2).
The changes will only remain in memory until you decide to save them.
Be careful before using the record command.
The drive does not contain a known partition table.
Created a new DOS disk label with the disk identifier 0xa8431f5e.
Command (m for help):

In this example, fdisk will be able to partition the virtual disk xvdb.

The most common menu options are:

DOS (MBR)
a toggles the boot option
b edit the nested BSD disk label
c toggle the “compatibility” option

Generic
d Delete a partition
F lists free unpartitioned partitions
l list known partition types
n add a new partition
p show the partition table
t changes the Partition type

v check the partition table
i shows information about a partition

Miscellaneous

m shows this menu
u changes the units of the entries shown
; x additional functionality (for advanced users only)

Script
I loads disk layout from an sfdisk script file
O dumps disk layout into an sfdisk script file

Save & ; exit
and save the table to disk and exit
without saving the changes

Create a new label
g create a new empty GPT partition table G creates a new partition table
G creates a new partition table SGI (IRIX) empty or creates a new empty DOS partition

To create a new MBR partition you must choose the “n” option on fdisk. Then you’ll have to choose whether the new partition will be primary, extended, or logical. If the partition is primary or extended, you must enter the partition number from 1 to 4. If the partition is logical, fdisk takes care of numbering for you.

In this example below, an 8GB primary partition was created using the MBR table:

# fdisk /dev/xvdb <br></br>Welcome to fdisk (util-linux 2.30.2). <br></br>The changes will only remain in memory until you decide to save them. <br></br>Be careful before using the record command. <br></br>The drive does not contain a known partition table. <br></br>Created a new DOS disk label with the disk identifier 0x5cfd153d. <br></br>Command (m for help): n <br></br>Type of partition <br></br>p primary (0 primary, 0 extended, 4 free) <br></br>and extended (container for logical partitions) <br></br>Select (default p): p <br></br>Partition number (1-4, default 1): 1 <br></br>First sector (2048-16777215, standard 2048): ; <br></br>Last sector, +sectors or +size {K, M, G, T, P} (2048-16777215, standard 16777215): <br></br>Created a new partition 1 of type “Linux” and size 8 GiB.

The size of the partition must be chosen by typing the starting cylinder and the desired final cylinder or by the size in megabytes or gigabytes. For example, to create a 1-giga partition, you can type +1024M or +1G.

If you want to use GPT to create a partition, use the “g” option and then the “n” option.

# fdisk /dev/xvdb <br></br>Welcome to fdisk (util-linux 2.30.2). <br></br>The changes will only remain in memory until you decide to save them. <br></br>Be careful before using the record command. <br></br>The drive does not contain a known partition table. <br></br>Created a new DOS disk label with disk identifier 0x7395e584.
Command (m for help): g <br></br>Created a new GPT disk label (GUID: DAA9FD3A-6F1B-4324-9BF3-4AABE4134037). <br></br>Command (m for help): n <br></br>Partition number (1-128, default 1): 1 <br></br>First sector (2048-16777182, standard 2048): <br></br>Last sector, +sectors or +size {K, M, G, T, P} (2048-16777182, standard 16777182): <br></br>Created a new partition 1 of the type “Linux filesystem” and size 8 GiB.

By default, all partitions are created with the Linux type 83.

If you want another type of partition, such as swap, you must create the partition and then change its type with the “t” option.

See the types of partitions that Linux supports:

![](https://learnlinux.com.br/editor/files/tiposparticao_pt.jpg)
Since you have defined the partitions, it is necessary to write the data to the partition table with the “w” option.