gdisk Command: Linux Partition Setup and Creating and Deleting GPT Partitions
To split a disk with GPT, you create and delete partitions with the gdisk command. Running gdisk followed by a device file name opens an interactive screen, and you set up Linux partitions by typing one-letter options, such as p to list partitions and d to delete one.
gdisk disk.imgThe gdisk output in this post comes from disk.img, a 3 GiB practice image file made with truncate. On a real disk, put a device file name such as /dev/sdb in place of disk.img.
The steps up to mounting are as follows.
Nothing you do to the partitions is written to the disk until you save with w.
1️⃣ Setting up Linux partitions with the gdisk command
gdisk is a partition management utility for GPT partition tables. Because the MBR scheme cannot use all of a disk larger than 2 TiB, large disks of 2 TB or more are split with GPT, and the partition work is done with gdisk or parted.
gdisk -[options] [target device file]You can check the configured partition information with the gdisk or fdisk command by adding the -l option.
gdisk -l disk.img
GPT fdisk (gdisk) version 1.0.7
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Disk disk.img: 6291456 sectors, 3.0 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): ABA1DC8A-EB0E-4342-86D5-EEF52D68CD23
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 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size code Name
1 2048 2099199 1024.0 MiB 8300 Linux filesystem
2 2099200 3147775 512.0 MiB 8200 Linux swap
3 3147776 6291422 1.5 GiB 8300 Linux filesystem1. gdisk start screen
This is the default screen that appears after running gdisk on an empty disk.img with no partition table.
gdisk disk.img
GPT fdisk (gdisk) version 1.0.7
Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present
Creating new GPT entries in memory.
Command (? for help):2. gdisk p option: viewing the partition list
Below is the list shown with p after creating and saving three partitions (1 GiB, a 512 MiB swap, and the remaining 1.5 GiB) on the same disk.img.
Command (? for help): p
Disk disk.img: 6291456 sectors, 3.0 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): ABA1DC8A-EB0E-4342-86D5-EEF52D68CD23
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 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size code Name
1 2048 2099199 1024.0 MiB 8300 Linux filesystem
2 2099200 3147775 512.0 MiB 8200 Linux swap
3 3147776 6291422 1.5 GiB 8300 Linux filesystem| p output fields | Description |
|---|---|
| Disk | Sectors and the total capacity of the disk |
| Sector size | Size of one logical sector (512 bytes in this post) |
| Disk identifier | Shows the GUID. |
| Number | Partition number |
| Start (sector) | First sector of the partition |
| End (sector) | Last sector of the partition |
| Size | Partition size |
| Code | Partition type shown as a code |
| Name | Purpose of the partition. The name can be changed with the c command. |
3. Creating a partition with gdisk
When you type n, gdisk asks for the partition number, the first sector and the last sector in turn. Below is an example on a disk.img that has only partition 1: the number and first sector were left at their defaults, and +512M was entered as the last sector to create partition 2.
Command (? for help): n
Partition number (2-128, default 2):
First sector (34-6291422, default = 2099200) or {+-}size{KMGTP}:
Last sector (2099200-6291422, default = 6291422) or {+-}size{KMGTP}: +512M
Current type is 8300 (Linux filesystem)You can check partition types with the L command.
Hex code or GUID (L to show codes, Enter = 8300): L
Type search string, or <Enter> to show all codes:
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 partition type code is 8200 for a swap partition and 8300 for a Linux file system; you set the type by entering this code.
Hex code or GUID (L to show codes, Enter = 8300): 8200
Changed type of partition to 'Linux swap'The partitions you made are written to the disk with w, and when asked whether to proceed, enter Y.
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to disk.img.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.4. Deleting a partition with gdisk
The [d] option deletes a partition. When asked for a number, enter the number of the partition to delete; if there is only one partition, it is deleted right away without asking for a number.
Command (? for help): d
Partition number (1-3): 3🔲 gdisk command options
| Option | Copy | Description |
|---|---|---|
| -l | Print the partition list and quit immediately | |
| p | Display the partition list of the disk | |
| n | Create a new partition (enter the number, first sector and last sector in that order) | |
| L | Show the list of partition types at the type code prompt | |
| 8200 | Enter a partition type code (at the type prompt of n and t) | |
| w | Save the partition information to the disk and quit | |
| d | Delete a partition |
| Option | Copy | Description |
|---|---|---|
| c | Change a partition's name | |
| l | List partition types | |
| q | Quit without saving partition information | |
| t | Set a partition type (including changing the type of an existing partition) | |
| s | Sort partitions | |
| ? | Print the list of commands |
🔲 gdisk command simulator
In the simulator below, run gdisk disk.img and type p, n, d and w from the option table with the chips or your keyboard to see how the memory and disk panels differ before you save.
2️⃣ gdisk command in practice
First, two practice images were made with truncate. The output in this section comes from a newly made 3 GiB image, disk.img, and a 1 GiB image, data.img, and you can enter the same steps yourself in the gdisk command simulator.
truncate -s 3G disk.img
truncate -s 1G data.img🔲 Example of creating and deleting gdisk partitions
gdisk was run on an empty disk.img, and n was used to create a 1 GiB partition and then a 512 MiB swap partition. The number and first sector were left at their defaults with Enter, +1G and +512M were entered as the last sectors, and 8200 was entered as the type code of the second partition.
gdisk disk.img
GPT fdisk (gdisk) version 1.0.7
Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present
Creating new GPT entries in memory.
Command (? for help): n
Partition number (1-128, default 1):
First sector (34-6291422, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-6291422, default = 6291422) or {+-}size{KMGTP}: +1G
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
Command (? for help): n
Partition number (2-128, default 2):
First sector (34-6291422, default = 2099200) or {+-}size{KMGTP}:
Last sector (2099200-6291422, default = 6291422) or {+-}size{KMGTP}: +512M
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 8200
Changed type of partition to 'Linux swap'Before typing w, the list was checked with p first. The two partitions came out with the sizes and type codes that were entered, and the free space was 1.5 GiB.
Command (? for help): p
Disk disk.img: 6291456 sectors, 3.0 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): 85C84C15-686C-4505-BE51-CF9025D04020
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 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 3145661 sectors (1.5 GiB)
Number Start (sector) End (sector) Size code Name
1 2048 2099199 1024.0 MiB 8300 Linux filesystem
2 2099200 3147775 512.0 MiB 8200 Linux swapAfter checking the list, w and Y were entered to write it to the disk.
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to disk.img.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.Deleting followed the same steps. gdisk was run again, 2 was entered for d, p confirmed that only partition 1 was left, and the change was saved with w.
Command (? for help): d
Partition number (1-2): 2
Command (? for help): p
Disk disk.img: 6291456 sectors, 3.0 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): 85C84C15-686C-4505-BE51-CF9025D04020
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 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 4194237 sectors (2.0 GiB)
Number Start (sector) End (sector) Size code Name
1 2048 2099199 1024.0 MiB 8300 Linux filesystem🔲 Common mistakes when using gdisk
A common mistake for first-time users is quitting with q without w. On a disk.img with only partition 1, a swap partition was added with n and partition 2 was confirmed with p, and then gdisk was quit with q without entering w.
Command (? for help): n
Partition number (2-128, default 2):
First sector (34-6291422, default = 2099200) or {+-}size{KMGTP}:
Last sector (2099200-6291422, default = 6291422) or {+-}size{KMGTP}: +512M
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 8200
Changed type of partition to 'Linux swap'
Command (? for help): p
Disk disk.img: 6291456 sectors, 3.0 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): 85C84C15-686C-4505-BE51-CF9025D04020
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 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 3145661 sectors (1.5 GiB)
Number Start (sector) End (sector) Size code Name
1 2048 2099199 1024.0 MiB 8300 Linux filesystem
2 2099200 3147775 512.0 MiB 8200 Linux swap
Command (? for help): qChecking again with [gdisk -l] showed that partition 2 was gone and only partition 1 remained. Even a partition shown in the p list is written to the disk only after you save with w.
gdisk -l disk.img
GPT fdisk (gdisk) version 1.0.7
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Disk disk.img: 6291456 sectors, 3.0 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): 85C84C15-686C-4505-BE51-CF9025D04020
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 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 4194237 sectors (2.0 GiB)
Number Start (sector) End (sector) Size code Name
1 2048 2099199 1024.0 MiB 8300 Linux filesystemPointing gdisk at the wrong disk can be avoided by checking the size and partition list with [gdisk -l] before you start. Below is the output of the same command on the 1 GiB image data.img; the [Disk data.img: 2097152 sectors, 1024.0 MiB] line and the empty partition list told it apart from disk.img.
gdisk -l data.img
GPT fdisk (gdisk) version 1.0.7
Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present
Creating new GPT entries in memory.
Disk data.img: 2097152 sectors, 1024.0 MiB
Sector size (logical): 512 bytes
Disk identifier (GUID): E8F9E93B-4299-41F3-8985-37973CBAEFFF
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 2097118
Partitions will be aligned on 2048-sector boundaries
Total free space is 2097085 sectors (1024.0 MiB)
Number Start (sector) End (sector) Size code Name🔲 Checking how the gdisk output changes
After you create or delete a partition, the number of lines in the partition list and the [Total free space] line change together in the p output. Right after the two partitions were made it was [Total free space is 3145661 sectors (1.5 GiB)], and after the 512 MiB partition 2 was deleted it grew to [Total free space is 4194237 sectors (2.0 GiB)].
The Code column showed the type codes exactly as entered: 8300 for partition 1, which took the default with Enter, and 8200 for partition 2, where 8200 was entered.
Whether the table was saved could also be told from the lines under [Partition table scan] on the first gdisk screen. On the empty image before saving it was [GPT: not present], and after saving with w the [GPT: present] and [Found valid GPT with protective MBR; using GPT.] lines appeared.
🔢 FAQ & Recommended Content
UUID
Linux blkid Command | UUID vs GUID
A post with blkid examples for checking the file system type and UUID of block devices, and the difference between UUID and GUID.
Directory size
du command: checking file disk usage
A post on the basic use and options of the du command, which checks the disk usage of files and directories.
ℹ️ Affiliate Disclosure
This site's content contains affiliate links. When a visitor buys a product or service through one of them, the site receives a commission from the seller. The amount the buyer pays(it goes down during event discounts ↓)does not go up. Posted prices, discounts, and stock reflect the time of writing and may differ, so confirm with the seller before buying. Products are chosen and reviewed by our own standards, and commissions do not affect the order or content of reviews.