Categories: Disks and Partitions

Linux fdisk command (changes partitions) [Basic Guide]

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 
fdisk -l Disk /dev/xvda: 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
Disk label type: gpt
Identifier of disk: 33E98A7E-CCDF-4AF7-8A35-DA18E704CDD4
Device Home End Sectors Size Type
/dev/xvda1 4096 16777182 16773087 8G Linux file system
/dev/xvda128 2048 4095 2048 1M BIOS boot
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 
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 0x5cfd153d.
Command (m for help): n
Type of partition
p primary (0 primary, 0 extended, 4 free)
and extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-16777215, standard 2048): ;
Last sector, +sectors or +size {K, M, G, T, P} (2048-16777215, standard 16777215):
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 
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 disk identifier 0x7395e584.
Command (m for help): g 
Created a new GPT disk label (GUID: DAA9FD3A-6F1B-4324-9BF3-4AABE4134037).
Command (m for help): n
Partition number (1-128, default 1): 1
First sector (2048-16777182, standard 2048):
Last sector, +sectors or +size {K, M, G, T, P} (2048-16777182, standard 16777182):
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:

Since you have defined the partitions, it is necessary to write the data to the partition table with the “w” option.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Recent Posts

Sudo command on Linux (privilege scale) [Basic Guide]

The sudo command on Linux executes a given command as if it were another user.…

2 years ago

SS command on Linux (investigate the network) [Basic Guide]

The ss command on Linux is extremely useful for investigating sockets, providing various information about…

2 years ago

Free Linux command (memory usage) [Basic Guide]

Free Linux command shows the amount of total memory in use and available, as well…

2 years ago

Linux while command (loop – while) [Basic Guide]

The shell has structures for testing conditions and executing certain program sequences several times (loop),…

2 years ago

Linux fstab file (disk mount setup) [Basic Guide]

The /etc/fstab file stores the configuration of which devices should be mounted and what is…

2 years ago

Netcat command on Linux (Swiss network knife) [Basic Guide]

The Netcat Command in Linux or nc is a utility used to do “almost anything”…

2 years ago

This website uses cookies.