gdisk Command in Linux with Examples

Last updated: November 11, 2023 | Bobbin Zachariah

gdisk is an interactive command-line tool used for managing disk partitions, particularly designed for disks using the GUID Partition Table (GPT) format. It is part of the GPT fdisk suite which includes four distinct programs: gdisk, cgdisk, sgdisk, and fixparts. gdisk uses a similar user interface to the traditional fdisk tool.

Some of the key features

  • Creating and Managing GPT Partitions: Allows the creation, deletion, and modification of GPT partitions.
  • MBR to GPT Conversion: Efficiently converts Master Boot Record (MBR) partition tables to GPT format.
  • Repairing GPT Data Structures: Provides tools for repairing and recovering GPT headers and partition tables.
  • Hybrid MBR Creation: Can create hybrid MBR/GPT disks for compatibility with systems that support only MBR.
  • Advanced Partition Configuration: Supports advanced features like setting partition GUIDs and attributes, and adjusting partition table size.

Syntax

gdisk [options] /dev/device

/dev/device: This is the device identifier for the disk you want to partition. For example, it might be /dev/sda or /dev/nvme0n1 on a Linux system.

In most typical uses, options are not necessary.

Remember gdisk requires root or superuser privileges to perform operations on disk partitions.

How to use gdisk

To use gdisk you need to get into its interactive command mode with the selected physical device.

To launch gdisk type:

$ sudo gdisk /path/to/devicename

Prior to running gdisk you can get information about available block devices using lsblk command.

Let us look further into gdisk usage with examples.

Creating Partition

Enter Interactive Mode: Type gdisk [device] (e.g., gdisk /dev/sdb).

View Current Partitions: Press p + Enter.

Create New Partition:

  • Type n + Enter.
  • Choose partition number or press Enter for default.
  • Accept the default first sector, specify the last sector (e.g., +8G for 8GB).
  • Press Enter to accept the default Linux file system type.

Save Changes: Press w + Enter, confirm with Y.

create a new partition using gdisk

Deleting Partition

Deleting partitions is straightforward. First, view the existing partitions by pressing p and Enter. Then, type d, press Enter, and enter the partition number you wish to delete. Save your changes by pressing w, then Enter, and confirm with Y.

deleting a partition

Viewing Partition Table

To view the partition table at any time, simply press p and Enter.

view partition table in gdisk

This will print the partition tables in the disk.

Change Partition Types

If you need to change the type of a partition, type t, press Enter, and then input the partition number. Find the hex code for the desired partition type by pressing L and Enter, then enter your chosen hex code. Save these changes by pressing w, followed by Enter, and confirm with Y

change partition type in gdisk

Converting MBR to GPT

Command: r (to open recovery & transformation menu), then g (to convert MBR to GPT)

Example:

$ gdisk /dev/sda
r
g

Creating a Hybrid MBR

Command: r (recovery menu), h (create hybrid MBR)

Example:

$ gdisk /dev/sda
r
h

Verifying Disk Integrity

Command: v (verify disk)

Example:

$ gdisk /dev/sda
v

Get Partition Details

Inspecting the details of a specific partition involves listing all partitions first by pressing p and Enter, and then typing i followed by the partition number to get detailed information.

It contains details like Partition GUID, Partition unique GUID, First sector, Last sector, Partition size, Attribute flags and Partition name.

get partition information in details

Writing Changes to Disk

To ensure changes made in the gdisk console are applied, perform a write operation after using commands such as n or d. Type w and press Enter, then confirm with Y.

write changes to disk

Exiting Without Saving

If you decide not to apply any changes, you can exit without saving by typing q and pressing Enter. This exit without making any changes.

gdisk exit without saving

Backup and Recovery

Before making any changes, it's essential to back up your device. Initiate a backup in gdisk by typing b and Enter, then specify a file name for the backup.

gdisk backup

In case of issues, you can recover from this backup by typing r to enter the recovery/transformation command mode, then l, and provide the path to your backup file. Write these changes by pressing w and Enter, and confirm with Y.

gdisk recovery

Setting Partition GUIDs

Each partition can have a unique GUID (Globally Unique Identifier).

To change a partition's GUID:

$ gdisk /dev/sda
x   # Enter expert menu
c   # Change partition GUID

Adjusting Partition Attributes

Partition attributes in GPT can be used to control various behaviors like whether a partition is bootable.

$ gdisk /dev/sda
a   # Set attributes

You’ll be prompted to enter the partition number and then choose which attributes to set or clear.

Non-Interactive Usage

The gdisk is primarily built for text mode interactive mode, we can only use it to list the disk partitions non-interactively.

$ sudo gdisk -l /dev/sdb
list disk partitions non interactively using gdisk

sgdisk is the non-interactive counterpart to gdisk. It's designed for automated scripts and advanced users who need to perform partitioning tasks without user intervention.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment