gdisk Command: Tutorial & Examples

Guided Partition Table Manipulation on Linux Servers

gdisk is a command-line utility for creating and managing partitions using the GUID Partition Table (GPT) scheme. It is especially useful for servers and virtual machines requiring advanced partitioning capabilities beyond the limitations of the older Master Boot Record (MBR) system.

This article explains how gdisk works, why it is important for modern Linux systems, and provides detailed examples of its usage. You will learn common commands, parameters, best practices, and troubleshooting tips to safely and effectively manage your server’s disk partitions.

What gdisk Does and Why It Matters

gdisk is designed to manipulate GPT partitions on block devices such as hard drives and SSDs. GPT is the modern partitioning standard that supports:

  • Disks larger than 2 TiB
  • Up to 128 primary partitions by default
  • Redundant partition tables for improved data integrity
  • Unique partition GUIDs and flexible partition types

Compared to legacy tools like fdisk that only support MBR, gdisk provides a safer and more flexible interface for managing partitions on large or complex storage setups typical in servers and virtual machines.

Using gdisk allows administrators to:

  • Create, delete, and modify partitions
  • Change partition types and attributes
  • Convert MBR disks to GPT safely
  • Repair corrupted GPT tables
  • Back up and restore partition tables

Proper disk partitioning is critical for organizing storage, installing multiple operating systems, separating data volumes, and optimizing performance on Linux servers.

Technical Background of GPT and gdisk

GPT disks contain several key components:

  • Protective MBR: A legacy MBR partition that protects GPT disks from being overwritten by MBR-only tools
  • Primary GPT Header: Stores disk layout information at the beginning of the disk
  • Primary GPT Partition Table: Contains partition entries with start/end sectors, types, and GUIDs
  • Secondary GPT Header and Table: Located at the end of the disk as a backup for recovery

gdisk directly reads and modifies these GPT structures, ensuring consistency and validity. It provides an interactive text-based interface for users to perform partitioning tasks safely.

Common Command Line Parameters of gdisk

While gdisk primarily operates interactively, it accepts several useful parameters:

sudo gdisk [options] device
  • -l : List partitions on the specified device without entering interactive mode
  • -b filename : Save the GPT partition table to a backup file
  • -r device : Recover and convert an MBR disk to GPT (read-only mode)
  • -w : Write partition table and exit (used within interactive mode)
  • -h : Display help information

For scripting and automation, the related tool sgdisk is recommended, as it supports non-interactive partitioning.

Practical Examples Using gdisk

Example 1: Listing Existing Partitions

To view the current partitions on a disk /dev/sda:

    sudo gdisk -l /dev/sda

Sample output:

    GPT fdisk (gdisk) version 1.0.5

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

    Found valid GPT with protective MBR; using GPT.

    Number  Start (sector)    End (sector)  Size       Code  Name
       1            2048          409599   200.0 MiB   8300  Linux filesystem
       2          409600         1050623   313.0 MiB   8200  Linux swap
       3         1050624       976773167   464.8 GiB   8300  Linux filesystem

Example 2: Creating a New Partition

Partition creation steps:

  1. Identify the target disk with lsblk:

    lsblk
    
  2. Start gdisk on the disk (e.g., /dev/sda):

    sudo gdisk /dev/sda
    
  3. Inside the interactive prompt, type n to create a new partition.

  4. Specify the partition number or press Enter to accept the default.

  5. Specify the first sector (or accept default).

  6. Specify the last sector or size (e.g., +10G for 10 GiB).

  7. Set the partition type code (e.g., 8300 for Linux filesystem).

  8. Type p to print the partition table and verify.

  9. Type w to write changes and exit.

Example interaction excerpt:

    Command (? for help): n
    Partition number (4-128, default 4): 
    First sector (2076672-976773167, default = 2076672): 
    Last sector (2076672-976773167, default = 976773167): +20G
    Current type is 'Linux filesystem'
    Changed type of partition to 'Linux filesystem'

    Command (? for help): p
    ...[partition table output]...

    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 to /dev/sda.

Example 3: Changing a Partition Type

To change partition 2 to Linux swap type 8200:

    sudo gdisk /dev/sda

Inside interactive mode:

    Command (? for help): t
    Partition number (1-4): 2
    Hex code or GUID (L to show codes): 8200
    Command (? for help): w
    Do you want to proceed? (Y/N): y

Example 4: Deleting a Partition

Delete partition 4:

    sudo gdisk /dev/sda

Inside interactive mode:

    Command (? for help): d
    Partition number (1-4): 4
    Command (? for help): w
    Do you want to proceed? (Y/N): y

Example 5: Converting MBR to GPT

To convert an MBR disk to GPT (with caution):

    sudo gdisk /dev/sda

If the disk uses MBR, gdisk will prompt to convert it. Follow instructions to review and write the new GPT.

Note: Always back up data before conversion to avoid data loss.

Example 6: Backing Up and Restoring GPT Tables

Back up GPT data to a file:

    sudo gdisk /dev/sda

Inside interactive mode:

    Command (? for help): b
    Enter backup filename: /root/sda_gpt_backup.bin
    Command (? for help): q

To restore the backup later, use the l command inside gdisk.

Common Errors and Troubleshooting

  • Warning: Invalid partition table detected
    Use gdisk’s recovery options or tools like testdisk to repair.

  • Data Loss Risk
    Always verify partition boundaries before writing changes.

  • Cannot Write to Disk
    Check if the disk is mounted or in use; unmount partitions or reboot if necessary.

  • MBR vs GPT Conflicts
    Mixing tools like fdisk (MBR) and gdisk (GPT) can cause confusion.

Security Considerations

  • Running gdisk requires superuser privileges (sudo), so ensure you trust the environment.

  • Incorrect partition changes can lead to system unbootability or data loss.

  • Always maintain backups of critical data and partition tables.

Tips and Best Practices

  • Use lsblk and parted to inspect disk layout before modifying.

  • Avoid resizing partitions with gdisk; use filesystem-specific tools like resize2fs after adjusting partitions carefully.

  • Use gdisk’s backup and restore commands (b and l) regularly.

  • For scripting, prefer sgdisk which supports non-interactive commands.

Related Commands and Alternatives

  • fdisk — Legacy MBR partitioning tool
  • parted — Interactive partitioning tool supporting GPT and MBR
  • sgdisk — Scriptable GPT partitioning utility related to gdisk
  • lsblk — List block devices and partitions

Cheatsheet: Common gdisk Interactive Commands

  • n : Create new partition
  • d : Delete partition
  • t : Change partition type code
  • p : Print partition table
  • w : Write changes and exit
  • q : Quit without saving
  • b : Backup partition table to file
  • l : Load partition table from backup
  • v : Verify partition table integrity
  • m : Display help menu

See Also

Further Reading

As an Amazon Associate, I earn from qualifying purchases.

The text above is licensed under CC BY-SA 4.0 CC BY SA