10 Useful fdisk Command Examples on Linux – Manage disk partitions

By | March 31, 2023

fdisk (fixed disk or format disk) is a command-line utility for creating and managing disk partitions in Linux and other Unix-based systems. In other words, fdisk is a command used to create, view, modify, and delete partitions on a hard drive.

fdisk works by making changes to the disk's partition table. The partition table stores information about the partitions present on a disk. fdisk is a convenient tool for creating partition tables, modifying partitions, diagnosing partition errors, and changing bootable partitions. However, root or sudo privileges are required to use it.

In this article, you'll learn 10 handy fdisk commands that you can use to manage your disk's partitions.

1. View Disk Devices

Before using the fdisk command to alter disk partitions, one of the first things we need to do is view all the partitions of the connected disks by running the command below:

sudo fdisk -l

When this command is executed, fdisk reads the partition tables of connected disks and displays a list of the available partitions on the screen.

Sample output:

Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 sectors
Disk model: VBOX HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: F4D78834-A71C-4812-A2CB-AB87D342C6CC
Device       Start      End  Sectors  Size Type
/dev/sda1     2048     4095     2048    1M BIOS boot
/dev/sda2     4096  1054719  1050624  513M EFI System
/dev/sda3  1054720 52426751 51372032 24.5G Linux filesystem
Disk /dev/sdb: 100 MiB, 104857600 bytes, 204800 sectors
Disk model: VBOX HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 4D192D22-99CD-5B43-A62F-04ABCA19439B
Device      Start    End Sectors  Size Type
/dev/sdb1    2048 102000   99953 48.8M Linux filesystem
/dev/sdb2  102400 125000   22601   11M Linux filesystem
/dev/sdb3  126976 188415   61440   30M Linux filesystem

The output shows the following information:

  • The device name: This is the name of the partition. Partitions are ordered according to the partition number. For example, /dev/sda1, /dev/sda2, /dev/sda3, and so on.
  • The start and end sectors: This indicates the positions of the starting and ending sectors of the partition.
  • The size of the partition: This is indicated both as the number of sectors in the partition and one of bytes, kilobytes, megabytes, or gigabytes.
  • The type of partition: This shows the type of file system used in the partition.
  • However, the output also displays a list of loop devices. A loop device is a virtual block device that allows files to be accessed just like an actual block device. That being said, loop devices often clutter the output, which makes reading and analysis difficult since we're usually only interested in actual block devices.

    To address this issue, we can pipe the "fdisk -l" output to the "sed" command to filter out loop devices as shown below:

    sudo fdisk -l | sed -e '/Disk \/dev\/loop/,+5d'

    This command ensures any line with a loop device is removed together with the next 5 lines following it.

    Sample output:

    Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 sectors
    Disk model: VBOX HARDDISK
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: F4D78834-A71C-4812-A2CB-AB87D342C6CC
    Device       Start      End  Sectors  Size Type
    /dev/sda1     2048     4095     2048    1M BIOS boot
    /dev/sda2     4096  1054719  1050624  513M EFI System
    /dev/sda3  1054720 52426751 51372032 24.5G Linux filesystem
    Disk /dev/sdb: 100 MiB, 104857600 bytes, 204800 sectors
    Disk model: VBOX HARDDISK
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: 4D192D22-99CD-5B43-A62F-04ABCA19439B
    Device      Start    End Sectors  Size Type
    /dev/sdb1    2048 102000   99953 48.8M Linux filesystem
    /dev/sdb2  102400 125000   22601   11M Linux filesystem
    /dev/sdb3  126976 188415   61440   30M Linux filesystem
    Disk /dev/sdc: 930 MiB, 975175680 bytes, 1904640 sectors
    Disk model: VBOX HARDDISK
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: 41C56B61-D646-4F4C-A1BA-04FAF8963C9C

    The output can be filtered further with the grep command:

    $ sudo fdisk -l | sed -e '/Disk \/dev\/loop/,+5d' | grep 'dev'
    Disk /dev/sda: 111.79 GiB, 120034123776 bytes, 234441648 sectors
    /dev/sda1  *     2048 199999487 199997440 95.4G 83 Linux
    Disk /dev/sdb: 111.79 GiB, 120034123776 bytes, 234441648 sectors
    /dev/sdb1        2048 200886271 200884224 95.8G 82 Linux swap / Solaris
    Disk /dev/sdc: 447.13 GiB, 480103981056 bytes, 937703088 sectors
    /dev/sdc1   2048 838862847 838860800  400G Linux filesystem
    $

    2. List partitions of a disk

    Additionally, we can view the available partitions on a specific disk by selecting the device's name as shown below:

    sudo fdisk -l /dev/sda

    Sample output:

    Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 sectors
    Disk model: VBOX HARDDISK
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: F4D78834-A71C-4812-A2CB-AB87D342C6CC
    Device       Start      End  Sectors  Size Type
    /dev/sda1     2048     4095     2048    1M BIOS boot
    /dev/sda2     4096  1054719  1050624  513M EFI System
    /dev/sda3  1054720 52426751 51372032 24.5G Linux filesystem

    This shell output shows the partitions present in "/dev/sda".

    3. Create a GPT Partition Table

    As noted earlier, a partition table stores information about partitions. fdisk allows the creation of different types of partition tables, including the GUID Partition Table (GPT), which supports disks larger than 2TB unlike the Master Boot Record (MBR).

    A GPT table can be created in the following steps:

    Run the fdisk command to open the fdisk utility, specifying the name of the disk to be partitioned as shown below:

    sudo fdisk /dev/sdc

    Enter the "g" command in the shell prompt, which indicates that we want to create a new GPT table. It's worth noting that any pre-existing partition table on the disk will be overwritten, and data erased.

    Sample output:

    Created a new GPT disklabel (GUID: EF67C6B1-9E2E-5A49-A454-77FEDB8EF5B0)

    Enter "w" in the prompt to save the changes to the partition table and exit.

    4. Create an MBR Partition Table

    fdisk also supports creating a Master Boot Record (MBR) partition table. This is particularly useful when creating partitions in older systems.

    We can create an MBR partition table in the following steps:

    Run the fdisk command to open the fdisk utility, specifying the name of the disk to be partitioned as shown below:

    sudo fdisk /dev/sdb

    Enter the "o" command in the shell prompt, which indicates that we want an MBR partition table. However, this action will overwrite and erase any existing partition table and data.

    Sample output:

    Created a new DOS disklabel with disk identifier 0x7c05c2ed.

    Enter "w" in the prompt to write the changes to the disk and exit.

    5. Create a Partition

    We can divide a disk into multiple partitions with the help of the fdisk command, depending on the disk's size. Each partition can also have its own file system based on the intended use.

    However, it is important to note that the number of primary partitions that can be created on a disk depends on the partition scheme of that disk. For instance, MBR allows a maximum of 4 primary partitions while GPT supports up to 128. Although, when using MBR, one of the partitions can be dedicated as an extended partition, which can be further divided into more logical partitions.

    In addition, to successfully partition a disk that is currently mounted, we first have to unmount its mounted partitions by executing the "umount" command and specifying the mounted partition's name as shown below:

    sudo umount /dev/sdb1

    To create a partition, we need to specify the disk to be segmented when running the fdisk command as shown below:

    sudo fdisk /dev/sdb

    However, there's a slight difference in the shell prompt when the disk uses the GPT and MBR partition scheme. We'll walk through creating partitions for both schemes.

    In this example, the "/dev/sdb" disk uses the GPT partition scheme.

    After running the command, the shell prompts us for input as shown below:

    Welcome to fdisk (util-linux 2.37.2).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    Command (m for help):

    Enter "n", which indicates that we want to create a new partition.

    Next, we are prompted to enter a partition number. Press enter to use the default number.

    Sample output:

    Command (m for help): n
    Partition number (1-128, default 1):

    Then, we are prompted to input the first sector. Press enter again to use the default.

    Sample output:

    First sector (2048-1904606, default 2048):

    Lastly, we are prompted to enter the size of the last sector, which is the size of the partition. The value of the sector size can be specified in sectors or kilobytes (K), megabytes (M), gigabytes (G), and so on.

    Sample output:

    Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-1904606, default 1904606):

    The size is expressed with the syntax, "+SUnit" or "-SUnit". The "+" sign indicates that the size should be increased relative to the currently used disk size, while the "-" sign indicates that the specified size should be subtracted from the available space and the resulting value should be used.

    S is the numerical value for the size and Unit refers to either one of kilobyte (K), megabyte (M), gigabyte (G), and so on. For instance, a value of +850M, indicates that the partition size should be 850MB. You could also press enter without specifying a value, which defaults to the available space on the disk.

    Sample output:

    Created a new partition 1 of type 'Linux filesystem' and of size 850 MiB.

    Although we have successfully created a new partition, these changes haven't been affected yet. We still need to enter "w" in the shell prompt to write the changes to the partition table.

    We've seen how to create partitions in disks using the GPT scheme, let's briefly go through how to do the same for the MBR scheme.

    Run the fdisk command, specifying the disk you want to partition as shown below:

    sudo fdisk /dev/sdc

    Next, enter "n" in the shell prompt. This indicates that we want to create a partition.

    After executing the command, we get the following shell prompt:

    Partition type
       p   primary (2 primary, 0 extended, 2 free)
       e   extended (container for logical partitions)
    Select (default p):

    As noted previously, we can either create 4 primary partitions in an MBR disk or 1 extended partition with 3 primary partitions. Inputting "p" will create a primary partition, while "e" will create an extended partition. We'll choose "p" for this example.

    The next steps are similar to the previous "GPT" example.

    Once we have successfully created the partition, we enter "w" to write the changes to the partition table and exit.

    After creating a partition, we need to format it to begin using it. We can do this with the mkfs command by specifying the partition name and the file system to be used as shown below:

    mkfs.ext4 /dev/sdc3

    In this example, we format the third partition of the "/dev/sdc" disk with the Linux's ext4 file system.

    Sample output:

    mke2fs 1.46.5 (30-Dec-2021)
    Creating filesystem with 9979 4k blocks and 9984 inodes
    Allocating group tables: done
    Writing inode tables: done
    Creating journal (1024 blocks): done
    Writing superblocks and filesystem accounting information: done

    6. Delete a Partition

    We can delete a partition to free up space on the disk. This can be achieved in the following steps:

    Run the fdisk command, specifying the disk where the partition is located as shown below:

    sudo fdisk /dev/sdc

    Enter d in the shell prompt, which indicates that we want to delete a partition.

    Next, enter the number of the partition to be deleted in the prompt. For instance, we can enter "3" to delete the third partition (/dev/sdc3) of the "/dev/sdc" disk.

    Sample output:

    Command (m for help): d
    Partition number (1-3, default 3):

    Finally, enter "w" in the shell prompt to write the changes to the partition table and exit.

    7. Fix Partition Table Order

    A partition table holds information about the partitions present on a disk, ordered according to the sector the partition resides on the disk from the starting sector to the end. A common partition table problem is the "Partition table entries are not in disk order" error, which can arise in the following situations:

    • When partitions are manually created or deleted without properly updating the partition table.
    • When newly created partitions fall between sectors of pre-existing partitions.
    • When restoring a disk image that has a partition table order different from that of the target disk.

    A typical partition table with this error can be seen below:

    Device     Boot  Start    End Sectors   Size Id Type
    /dev/sdc1         4005 147455  143451    70M 83 Linux
    /dev/sdc2         2500   4004    1505 752.5K 83 Linux
    /dev/sdc3       200000 405503  205504 100.3M 83 Linux
    /dev/sdc4       150000 190463   40464  19.8M 83 Linux
    Partition table entries are not in disk order.

    As you have observed, the partitions are incorrectly ordered. For instance, the current "second partition" falls between sectors 2500 and 4004. But this partition should actually be the first partition since it has the lowest starting and ending sectors.

    This error can potentially lead to data loss and might even prevent the operating system from booting if not quickly resolved. However, a faulty partition table order can easily be fixed with the fdisk command.

    To perform a repair with fdisk, we first need to run fdisk, specifying the problematic disk as shown below:

    sudo fdisk /dev/sdc

    Replace "/dev/sdc" with your actual device name.

    Next, enter "x" in the shell prompt, which indicates that we want expert mode.

    Then, enter "f" in the prompt to perform the partition order fix.

    Next, enter "r" in the prompt to leave expert mode.

    Lastly, enter "w" in the shell prompt to write the changes to the partition table and exit.

    After fixing the error, the updated partition table is shown below:

    Device     Boot  Start    End Sectors   Size Id Type
    /dev/sdc1         2500   4004    1505 752.5K 83 Linux
    /dev/sdc2         4005 147455  143451    70M 83 Linux
    /dev/sdc3       150000 190463   40464  19.8M 83 Linux
    /dev/sdc4       200000 405503  205504 100.3M 83 Linux

    8. Check Disk/Partition Size

    The fdisk -s command can be used to obtain information about the size of a disk or its partitions. However, the size of the device is displayed in blocks. The information can be useful when allocating disk space to new partitions.

    To check the disk's size, run the fdisk command with the "s" flag and specify the name of the device you're interested in as shown below:

    sudo fdisk -s /dev/sdc

    Sample output:

    952320

    Alternatively, you can check the size of a partition by specifying the partition instead as shown below:

    sudo fdisk -s /dev/sdc2

    In this example, we're checking the size of the second partition of the "/dev/sdc" disk.

    Sample output:

    71725

    9. Toggle Partition's Boot Flag

    In an MBR partition table, the boot flag (or active partition flag) is used to indicate the partition which serves as the active boot partition. This partition contains the boot loader, which is used to load the operating system.

    In the partition table, the active boot partition is marked with an asterisk (*) as shown below:

    Device     Boot  Start    End Sectors   Size Id Type
    /dev/sdc1         2500   4004    1505 752.5K 83 Linux
    /dev/sdc2         4005 147455  143451    70M 83 Linux
    /dev/sdc3       150000 190463   40464  19.8M 83 Linux
    /dev/sdc4  *    200000 405503  205504 100.3M 83 Linux

    In the partition table above, the fourth partition is the active boot partition as indicated by the asterisk.

    However, the boot flag can easily be changed to a different partition using the fdisk command. This is particularly useful for booting a different operating system on your machine.

    It is worth noting that there can be only one active boot partition, which should contain a boot loader to avoid unpredictable behaviors while booting.

    To set a boot flag on a partition, we run the fdisk command, selecting the disk with the bootable partition as shown below:

    sudo fdisk /dev/sdc

    Next, enter "a" in the shell prompt, which indicates that we want to set a boot flag.

    After doing this, we're prompted to enter a partition number, which should be the partition number with the bootloader. This activates the boot flag for this partition and will deactivate it if previously set.

    Sample output:

    Command (m for help): a
    Selected partition 1
    The bootable flag on partition 1 is enabled now.

    Lastly, enter "w" in the shell prompt to write the changes to the partition table and exit.

    10. Change Partition Type

    Each partition on a disk has a partition type, which indicates the file system used in that partition and helps the operating system understand how to access data. The partition type is identified using a hexadecimal (hex) code. For instance, Linux, which is the default type when creating a partition with fdisk, uses the "83" hex code. Some common partition types and their hex code are:

    NTFS (07)
    Linux swap (82)
    FAT 32 (0b)

    The example below shows a partition table with all of the partitions having a partition type of "Linux".

    Device     Boot  Start    End Sectors   Size Id Type
    /dev/sdc1         2500   4004    1505 752.5K 83 Linux
    /dev/sdc2         4005 147455  143451    70M 83 Linux
    /dev/sdc3  *    150000 190463   40464  19.8M 83 Linux
    /dev/sdc4       200000 405503  205504 100.3M 83 Linux

    With fdisk, we can easily change to a different partition type. Assuming, we want to change the partition type from Linux (83) to FAT 32 (0b), we first run the fdisk command, selecting the disk with the partition we're interested in as shown in the command below:

    sudo fdisk /dev/sdc

    Next, enter "t" in the shell prompt, which indicates that we want to change the partition type.

    Then, enter the partition number in the shell prompt. We're choosing "2" for this example.

    Next, you're prompted to enter the hex code of the partition type being changed. Here, you can enter "l" to view the full list of the available types.

    Sample output:

    00 Empty            24 NEC DOS          81 Minix / old Lin  bf Solaris
    01 FAT12            27 Hidden NTFS Win  82 Linux swap / So  c1 DRDOS/sec (FAT-
    02 XENIX root       39 Plan 9           83 Linux            c4 DRDOS/sec (FAT-
    03 XENIX usr        3c PartitionMagic   84 OS/2 hidden or   c6 DRDOS/sec (FAT-
    04 FAT16 <32M       40 Venix 80286      85 Linux extended   c7 Syrinx
    05 Extended         41 PPC PReP Boot    86 NTFS volume set  da Non-FS data
    06 FAT16            42 SFS              87 NTFS volume set  db CP/M / CTOS / .

    However, since we already know that FAT 32 uses the "0b" hex code, we'll input that and proceed.

    Lastly, enter "w" to write the changes to the partition table and exit.

    The updated partition table is shown below:

    Device     Boot  Start    End Sectors   Size Id Type
    /dev/sdc1         2500   4004    1505 752.5K 83 Linux
    /dev/sdc2         4005 147455  143451    70M  b W95 FAT32
    /dev/sdc3  *    150000 190463   40464  19.8M 83 Linux
    /dev/sdc4       200000 405503  205504 100.3M 83 Linux

    11. Verify Partition Table

    When modifying partitions on a disk with fdisk, there's a risk of altering the partition table incorrectly, which can lead to errors and inconsistencies. It is important that we check for any errors before writing the change to the partition table.

    To verify the integrity of the partition table, we can execute the "v" command before saving changes when modifying the partition table with fdisk.

    Sample output:

    No errors detected.
    Remaining 1544448 unallocated 512-byte sectors.

    This output shows that there isn't any error in the partition table.

    Conclusion

    fdisk is a robust tool for managing disk partitions in Linux. By using fdisk, system administrators and users can create different types of partition tables on a hard disk, segment disks into different partitions to store data in an organized manner, and fix errors that may arise from partitioning. However, the fdisk command should be used with caution to avoid damage to the partition table and data loss.

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

Leave a Reply

Your email address will not be published. Required fields are marked *