How to manipulate gpt partition tables with gdisk and sgdisk on Linux

GPT is the acronym for GUID Partition Table: it is the new standard for storage devices: it is part of the UEFI firmware specifications and the successor of MBR, of which it overcomes several limitations. MBR for example, allows a maximum of 4 primary partitions (GPT supports up to 128 of them), and works only with drives up to 2TB in size. On Linux there are several tools we can use to create and manipulate GPT partition tables. In this tutorial we focus on the usage of the gdisk and sgdisk utilities.

In this tutorial you will learn:

  • How to list the partition table of a device with gdisk
  • How to create a GPT partition table
  • How to create a new partition
  • How to remove a partition
  • How to resize a partition
  • How to work non-interactively with sgdisk
How to manipulate GPT partition tables with gdisk and sgdisk
How to manipulate GPT partition tables with gdisk and sgdisk

Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Distribution independent
Software gdisk/sgdisk
Other Root privileges
Conventions # – requires given linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux-commands to be executed as a regular non-privileged user

Gdisk vs sgdisk

In this tutorial we will use two different tools: gdisk and sgdisk. They let us perform the same operations, with a big difference: the former works interactively, provides confirmation prompts and a question-based interface. Changes we perform with it are written to the disk only when we use the dedicated command, in order to minimize mistakes.

The latter, instead, works in a non-interactive way: it is meant to be used inside shell scripts, or more generally directly from the command line. Sgdisk commands are applied immediately, therefore it should be used very carefully. In the first part of this tutorial we focus on how to perform the basic operations with gdisk, in the second we see how to perform the same operations with sgdisk.

Listing the partition table of a device

To start using gdisk, all we have to do is to invoke the program passing the path of the device we want to manipulate as argument. To list the partition table of a device, we call the program with the -l option:

$ sudo gdisk -l /dev/sdc

The following is what the above command returns on a device which is completely uninitialized:

GPT fdisk (gdisk) version 1.0.8

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries in memory.
Disk /dev/sdc: 15122432 sectors, 7.2 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 32A31D98-1500-4FE5-836A-69F289D048A7
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 15122398
Partitions will be aligned on 2048-sector boundaries
Total free space is 15122365 sectors (7.2 GiB)

Creating a new GPT partition table

The device we are working with, as we saw, is completely empty; it doesn’t contain any partition table. To create a new GPT partition table on it, we pass it as argument when invoking gdisk, without providing any options:

$ sudo gdisk /dev/sdc

As soon as we launch the command above, we will be prompted to enter a command command:

Command (? for help):

To create a new GPT partition table, we must use the o command (we type o, and press enter). We will be warned that the action will destroy all existing partitions on the disk, and we will be prompted to confirm our choice:

Command (? for help): o
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): Y

As we already said, when using gdisk, the changes we make to the disk are not immediately written: this is a security measure. To make them effective we must use the w command:

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdc.
The operation has completed successfully.

We will be prompted to provide a last confirmation. After we provide it, changes will become effective, and (hopefully) we will be notified of the operation success.

Creating a new partition

We initialized our device, now we can start creating partitions; let’s see how to do it. To create a new partition we must use the n command. We will be prompted to answer a series of questions; as an answer for each of them we can provide a value or just press enter to accept the default one, displayed
between parenthesis.

The first thing we should decide is the number which should be used for the partition, in the range from 1 to 128. Here we usually want to use the default value, which is progressive.

As a second thing, we should decide at what sector of the disk the partition should start. For the first partition, the default starting sector is 2048. The default value is what we want to use for this parameter in the vast majority of cases.

The next thing we should decide, is at what sector the partition should end: this, as you can imagine, determines the partition size. Instead of specifying a sector, we can provide the partition size directly, with an integer followed by one of the available suffixes: K,M,G,T,P. To create a partition of 500 MiB in size, for example, we would enter +500M as a value.

As a last thing, we will be prompted to decide what HEX code to use for the partition. Here the default value is 8300 (Linux filesystem). To obtain the list of all the available codes, all we have to do is to enter the L command, and then press <enter> again:

0700 Microsoft basic data                0701 Microsoft Storage Replica
0702 ArcaOS Type 1                       0c01 Microsoft reserved
2700 Windows RE                          3000 ONIE boot
3001 ONIE config                         3900 Plan 9
4100 PowerPC PReP boot                   4200 Windows LDM data
4201 Windows LDM metadata                4202 Windows Storage Spaces
7501 IBM GPFS                            7f00 ChromeOS kernel
7f01 ChromeOS root                       7f02 ChromeOS reserved
8200 Linux swap                          8300 Linux filesystem
8301 Linux reserved                      8302 Linux /home
8303 Linux x86 root (/)                  8304 Linux x86-64 root (/)
8305 Linux ARM64 root (/)                8306 Linux /srv
8307 Linux ARM32 root (/)                8308 Linux dm-crypt
8309 Linux LUKS                          830a Linux IA-64 root (/)
830b Linux x86 root verity               830c Linux x86-64 root verity
830d Linux ARM32 root verity             830e Linux ARM64 root verity
830f Linux IA-64 root verity             8310 Linux /var
8311 Linux /var/tmp                      8312 Linux user's home
8313 Linux x86 /usr                      8314 Linux x86-64 /usr
8315 Linux ARM32 /usr                    8316 Linux ARM64 /usr
8317 Linux IA-64 /usr                    8318 Linux x86 /usr verity
Press the <Enter> key to see more codes, q to quit:

The codes are not displayed all at once. To scroll the list we should keep pressing <enter>. In our example we just confirmed the default HEX code for the partition:

Command (? for help): n
Partition number (1-128, default 1):
First sector (34-15122398, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-15122398, default = 15122398) or {+-}size{KMGTP}:+500M
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

At this point the changes exist only in memory; we enter w to write them to disk.

Removing a partition

To remove an existing partition using gdisk we have to use the ‘d’ command. Once we enter it, we will be asked to provide the number of the partition we want to delete. For the sake of this example we remove the second partition on our device:

Command (? for help): d
Partition number (1-2): 2

To write the changes to the disk, as we already know, we enter the w command.

Resizing a partition

Resizing a partition is always a dangerous operation which should be performed carefully, and possibly after creating a backup. Performing such operation with gdisk is somewhat counter-intuitive since the process involves the deletion of the current partition (in memory only), and its re-creation with the same starting sector and the new size/end sector.

Enlarging a partition/filesystem

Suppose we want to extend the second partition we created in this tutorial, so that it uses all the available space on the disk. Here is how we would proceed. We first request information about the partition we are about to manipulate using the i command:

Command (? for help): i
Partition number (1-2): 2
Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem)
Partition unique GUID: D6FF2779-6F14-4E3E-876C-83E38F3A3FD7
First sector: 1026048 (at 501.0 MiB)
Last sector: 3123199 (at 1.5 GiB)
Partition size: 2097152 sectors (1024.0 MiB)
Attribute flags: 0000000000000000
Partition name: 'Linux filesystem'

Since we have to delete the partition as part of the resizing operation, we want to keep its current information at hand. One thing we may want to do is to apply the current partition unique GUID to the newly created one (by default a random one is generated). As we already know, to delete the partition we have to use the d command:

Command (? for help): d
Partition number (1-2): 2

At this point we recreate the partition with the n command, using the same partition number and starting point (in this case the default one), and provide the new size. Since in this case we want to use all the available space, we will also use the default value suggested by the application:

Command (? for help): n
Partition number (2-128, default 2): 2
First sector (34-15122398, default = 1026048) or {+-}size{KMGTP}:
Last sector (1026048-15122398, default = 15122398) or {+-}size{KMGTP}:
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

To change the unique GUID of the new partition and use the previous one, we should enter the “advanced operations” menu of gdisk, using the x command (notice how the prompt changes)

Command (? for help): x

Expert command (? for help):

The expert command we want to use is c. Once we enter it, We will be asked to provide the partition number and the new GUID for it. In this case we enter the one that was used for the “old” partition:

Expert command (? for help): c
Partition number (1-2): 2
Enter the partition's new unique GUID ('R' to randomize): D6FF2779-6F14-4E3E-876C-83E38F3A3FD7
New GUID is D6FF2779-6F14-4E3E-876C-83E38F3A3FD7

At this point we can save the changes to disk with the w command (if for some reasons we want to leave the “expert” menu and return to the main one, we can use m instead):

Expert command (? for help): w

We now have a much larger partition than before, but if there is a filesystem on it, it should still be enlarged to cover all the available space. The command used to perform the operation varies depending on the filesystem type.

Supposing we have an ext4 filesystem on the partition, we would first run a check using the e2fsck command with the -f option (in order to force the check even if the filesystem looks clean), and then proceed with the actual resizing using resize2fs, which, when it is invoked without any options, extends the filesystem to all the available partition space:

$ sudo e2fsck -f /dev/sdc2 && sudo resize2fs /dev/sdc2

Shrinking a partition

To shrink a partition we should perform the above steps in reverse order: first we shrink the filesystem (if any) to a size which is slightly less than the new size we will use for the partition, or to the minimum possible size (that’s what we do in the example below, with the -M option), than we shrink the
partition itself, and finally we enlarge the filesystem again, so that it uses all the available space.

Supposing we want to shrink the second partition on the disk to 2 GiB, we would run:

$ sudo e2fsck -f /dev/sdc2 && sudo resize2fs -M /dev/sdc2

At this point we can launch gdisk, and perform the same steps we did in the previous example, this time providing an ending sector or a partition size smaller than the current one. Once the changes are saved and written to the disk, we enlarge the filesystem so that it covers all the available space:

$ sudo e2fsck -f /dev/sdc2 && sudo resize2fs /dev/sdc2

Performing operations non-interactively with sgdisk

As we already said, While gdisk is an interactive program, sgdisk is a command line utility which can be used to perform operations non-interactively. Let’s see how to perform the same operations we performed in the previous examples, directly with sgdisk.

Creating a GPT partition table

To create a GPT partition table with sgdisk, all we need to do is to run the following command:

$ sudo sgdisk -o /dev/sdc

When invoked with the -o option (or --clear), sgdisk clears any existing partition table on the given device, and creates a new GPT partition table. Again, since the program is meant to be used from scripts, no warnings will be issued, and no confirmations will be requested, so it should be used carefully.

Creating a partition

To create a new partition, we invoke sgdisk with the -n option (short for --new) and provide the partition number, partition start and partition end with the following syntax:


To create a partition with number 2 which uses the first available start sector, with a size of 1GiB (1024 MiB), therefore we would write:


If we want the partition to cover all the available space on the disk, instead, we would use 0 as partition end value:


If we want to use one specific HEX code for the partition, we should use the -t option (short for --typecode) with the following syntax:


The complete command to create the partition and assign the 8300 hex code to it explicitly would therefore be:

$ sudo sgdisk --new 2::+1G --typecode 2:8300 /dev/sdc

Removing a partition

To remove a partition from the disk directly from the command line, without any interaction, we have to use sgdisk with the -d option (--delete), and provide the partition number as the option argument. To delete the partition number 2, we would write:

$ sudo sgdisk --delete 2 /dev/sdc

Resizing a partition

The process of resizing a partition with sgdisk is the same we used interactively with gdisk, but its performed in one line, for example, to enlarge the second partition of the disk so that it covers all the available space, we would run:

$ sudo sgdisk \
  -d 2 \
  -n 2:0:0 \
  -u 2:D6FF2779-6F14-4E3E-876C-83E38F3A3FD7 \

In the example above the -d option, as we know is used to delete the partition, -n is used to create a new one with the same number and the default value for start and ending sectors (0, means “use the default”).

Finally, the -u option is the short for --partition-guid: we used it to assign the new partition the same GUID of the deleted one.

To request information about a specific partition, we can use the -i option and provide the partition number. For example:

$ sudo sgdisk -i 2 /dev/sdc


In this tutorial we learned to used the gdisk and sgdisk utilities in order to manipulate GPT partition tables. We saw that the former is a program which is meant to be used interactively, and provides confirmation prompts and other security measures, while the latter is meant to be used directly from the command line or inside shell scripts, and modifications we made with it are immediately effective. We saw how to create a new partition tables and how to create, remove and resize partitions with both tools. Partitions manipulations is a dangerous activity and should be performed with care. For further information about the mentioned tools, please check their respective manuals.

Comments and Discussions
Linux Forum