fbpx fdisk Command on Linux: Create and Manage Disk Partitions | Certificação Linux

fdisk Linux: Create, List and Manage MBR and GPT partitions

📅 01/01/2019 ⏱ 6 min ✍️ Uira Ribeiro
Linuxcommands
fdisk Linux: Create, List and Manage MBR and GPT partitions
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

The fdisk command in Linux is a utility to create, list, change and delete 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 a user-friendly dialog form.

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

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

$ sudo fdisk -lDisco /dev/xvda: 8 GiB, 8589934592 bytes, 16777216 setoresUnidades: setor de 1 * 512 = 512 bytesTamanho de setor (lógico/físico): 512 bytes / 512 bytesTamanho E/S (mínimo/ótimo): 512 bytes / 512 bytesTipo de rótulo do disco: gptIdentificador do disco: 33E98A7E-CCDF-4AF7-8A35-DA18E704CDD4
Dispositivo  Início      Fim  Setores Tamanho Tipo/dev/xvda1     4096 16777182 16773087      8G Linux sistema de arquivos/dev/xvda128   2048     4095     2048      1M BIOS inicialização
Disco /dev/xvdb: 8 GiB, 8589934592 bytes, 16777216 setoresUnidades: setor de 1 * 512 = 512 bytesTamanho de setor (lógico/físico): 512 bytes / 512 bytesTamanho E/S (mínimo/ótimo): 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 boot partition of 1MB and uses the GPT system. The second 8GB disk has no partition created.

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

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

# fdisk /dev/xvdb

Bem-vindo ao fdisk (util-linux 2.30.2).
As alterações permanecerão apenas na memória, até que você decida gravá-las.
Tenha cuidado antes de usar o comando de gravação.

A unidade não contém uma tabela de partição conhecida.
Criado um novo rótulo de disco DOS com o identificador de disco 0xa8431f5e.

Comando (m para ajuda): 

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

The most frequent menu options are:

DOS (MBR)
   a   toggles the boot flag
   b   edits nested BSD disklabel
   c   toggles the 'compatibility' flag
 
Generic
   d   deletes a partition
   F   lists free unpartitioned partitions
   l   lists the known partition types
   n   adds a new partition
   p   prints the partition table
   t   changes the partition type
   v   verifies the partition table
   i   prints information about a partition
 
Misc

   m   displays this menu
   u   changes the display/entry units
   x   extra functionality (experts only)
 
Script
   I   loads disk layout from an sfdisk script file
   O   dumps disk layout to an sfdisk script file
 


Save & exit
   w   writes table to disk and exits
   q   quits without saving changes
 
Create a new label
   g   creates a new empty GPT partition table
   G   creates a new empty SGI (IRIX) partition table
   o   creates a new empty DOS partition table
   s   creates a new empty Sun partition table

To create a new MBR type partition you must choose the "n" option in fdisk. Then you will have to choose if the new partition will be primary, extended or logical. If the partition is primary or extended, you will have to enter the partition number from 1 to 4. If the partition is logical, fdisk takes care of numbering it for you.

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

# fdisk /dev/xvdbBem-vindo ao fdisk (util-linux 2.30.2).As alterações permanecerão apenas na memória, até que você decida gravá-las.Tenha cuidado antes de usar o comando de gravação.A unidade não contém uma tabela de partição conhecida.Criado um novo rótulo de disco DOS com o identificador de disco 0x5cfd153d.Comando (m para ajuda): nTipo da partição   p   primária (0 primárias, 0 estendidas, 4 livre)   e   estendida (recipiente para partições lógicas)Selecione (padrão p): pNúmero da partição (1-4, padrão 1): 1Primeiro setor (2048-16777215, padrão 2048): Último setor, +setores ou +tamanho{K,M,G,T,P} (2048-16777215, padrão 16777215): Criada uma nova partição 1 do tipo "Linux" e de tamanho 8 GiB.

The partition size should be chosen by entering the desired first and last sector or by size in megabytes or gigabytes. For example, to create a 1 gig 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/xvdbBem-vindo ao fdisk (util-linux 2.30.2).As alterações permanecerão apenas na memória, até que você decida gravá-las.Tenha cuidado antes de usar o comando de gravação.A unidade não contém uma tabela de partição conhecida.Criado um novo rótulo de disco DOS com o identificador de disco 0x7395e584.
Comando (m para ajuda): gCriado um novo rótulo de disco GPT (GUID: DAA9FD3A-6F1B-4324-9BF3-4AABE4134037).Comando (m para ajuda): nNúmero da partição (1-128, padrão 1): 1Primeiro setor (2048-16777182, padrão 2048): Último setor, +setores ou +tamanho{K,M,G,T,P} (2048-16777182, padrão 16777182): Criada uma nova partição 1 do tipo "Linux filesystem" e de tamanho 8 GiB.

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

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

See the partition types that Linux supports:

Partition Types

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

Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.

Liked it? Share

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp