Cyberithub

16 Fdisk Command Examples to Manage Disk Partitions in Linux

fdisk command is a free and open source utility widely available on Linux Based Platforms to Manage Disk Partitions Table. It understand different partitions table like BSD, GPT, MBR, SUN and SGI. fdisk command can be used to create a new partition, delete a partition, change the partition ID, resize the partition, move a partition and so on.

One can create a maximum of four primary partition using fdisk command and n number of logical partitions based on available disk size. Partition number from 1 to 4 are reserved for Primary partitions and then logical partition starts from number 5. fdisk command creates partition of minimum 40 MB size and maximum of 2 TB size.

What is Block Device

A Block device in Linux is defined as a file which represents devices like Hard Disk, CD-ROMs from where data can be read in fixed size blocks.

What is Partition

A Block device are usually divided into one or more logical disks which are known as partitions. The information of these partitions are stored in a table Known as Partition Table.

Synopsis

fdisk [-uc] [-b sectorsize] [-C cyls] [-H heads] [-S sects] device

fdisk -l [-u] [device…]

fdisk -s partition…

fdisk -v

fdisk -h

fdisk command examples to manage disk partitions in linux

Fdisk Command Examples to Manage Disk Partitions in Linux

Also Read: 8 dumpefs Command Examples to Manage EXT2, EXT3 and EXT4 Filesystem in Linux

Example 1: How to Check fdisk Command Version

To check fdisk command version you need to use -v option as shown below. Here current fdisk version is 2.23.2.

[root@localhost ~]# fdisk -v
fdisk from util-linux 2.23.2

NOTE:

Please note that here I am using root user to run all the below commands. You can use any user with sudo access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo access to the User.

Example 2: How to List All Disk Partitions in Linux

If you want to List all the disk partitions then you need to use -l option with fdisk command as shown below. Here we have two disks /dev/sda and /dev/sdb but partitions are created only on /dev/sda disk. We have two partitions /dev/sda1 and /dev/sda2 available on /dev/sda disk.

[root@localhost ~]# fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000f1fe2

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 83886079 40893440 8e Linux LVM

Disk /dev/sdb: 8589 MB, 8589934592 bytes, 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x5d09da13

-l : List the partition tables for the specified devices and then exit. More on fdisk Man Page.

Example 3: How to Check total number of partitions in a Disk

If you want to check all the partitions of a Specific disk then you can also use -l option along with the disk name. In this example we are checking all the partition information of /dev/sdc disk using fdisk -l /dev/sdc command as shown below.

[root@localhost ~]# fdisk -l /dev/sdc

Disk /dev/sdc: 8589 MB, 8589934592 bytes, 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x5bb7ecd8

Device Boot Start End Blocks Id System
/dev/sdc1 2048 16777215 8387584 83 Linux

Example 4: How to Delete a Disk Partition in Linux

To delete a disk partition you need to use command d as shown below. In this example we are deleting partition /dev/sdc1 using d command and then writing the partition table changes using w command.

[root@localhost ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Selected partition 1
Partition 1 is deleted

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

NOTE:

Please check before deleting a partition as deletion of wrong partition could result in data loss.

Example 5: How to Create a Disk Partition in Linux

To create a disk partition you need to use n command as shown below. In this example we are creating partition on /dev/sdc disk using fdisk /dev/sdc command. Then selecting n command to create a new partition and selecting p to create it as a primary partition type. Here we are using default sector value i.e first sector value as 2048 and last sector value as 16777215 which will create partition on entire 8 GB disk and then writing the changes to partition table using w command.

[root@localhost ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-16777215, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-16777215, default 16777215):
Using default value 16777215
Partition 1 of type Linux and of size 8 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Example 6: How to Check the Size of Disk Partition in Linux

To check the size of disk partition you can use -s option with fdisk command as shown below. In this example we are checking the size of /dev/sdc1 partition using fdisk -s /dev/sdc1 command. Here you will see size in number of blocks. So the total size of /dev/sdc1 partition is 8387584 blocks.

[root@localhost ~]# fdisk -s /dev/sdc1
8387584

-s : Print the size (in blocks) of each given partition. More on fdisk Man Page.

Example 7: How to Verify Disk Partition table in Linux

Another useful command that you can use is v command to verify the disk partition table in Linux. In this example we are verifying /dev/sdc disk partition using v command and then writing the changes to partition table using w command.

[root@localhost ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): v
Remaining 2047 unallocated 512-byte sectors

Command (m for help): w

Example 8: How to Quit fdisk Command without saving any changes

To quit fdisk command utility without saving any changes you need to use q command as shown below. In this example we have exited the /dev/sdc disk changes without saving it by using q command.

[root@localhost ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): q

Example 9: How to Save and Exit fdisk Utility

If you want to exit after saving partition table information then you need to use w command as shown below. This command will save all the partition table changes and then exit out from fdisk command.

[root@localhost ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Example 10: How to Change Disk Partition ID to “Linux LVM” type

If you want to change disk partition ID to Linux LVM type then you need to use hex code 8e after selecting the partition through t command as shown below. In this example we are changing the partition Id of /dev/sdc1 partition from 83 to 8e to convert the partition from normal disk to LVM type.

[root@localhost ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Example 11: How to Change Disk Partition ID to “Linux Swap” Type

For creating a swap partition you need to change the partition type to Linux swap type by using hex code 82 as shown below. In this example we are changing partition ID of /dev/sdc1 from normal partition to Linux swap type.

[root@localhost ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 82
Changed type of partition 'Linux LVM' to 'Linux swap / Solaris'

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Example 12: How to Disable/Enable Boot Flag(*) of a Partition

If you want you can also disable/enable boot flag of a partition using fdisk command in Linux. As you can see from below output, partition /dev/sda1 is currently marked as boot partition.

[root@localhost ~]# fdisk -l /dev/sda

Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000f1fe2

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 83886079 40893440 8e Linux LVM

To disable the boot flag from /dev/sda1 partition you need to use command a under fdisk utility and then select the partition to disable the boot flag as shown below.

[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): a
Partition number (1,2, default 2): 1

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

After writing the disk partition table changes if you check the /dev/sda1 partition status again then you can notice that star(*) marked is removed now. This confirms boot flag is now disabled. You can follow the same procedure to enable it again.

[root@localhost ~]# fdisk -l /dev/sda

Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000f1fe2

Device Boot Start End Blocks Id System
/dev/sda1 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 83886079 40893440 8e Linux LVM

Example 13: How to Correct the Partition Table Order

If you want to correct the partition table order then you need to use the extra functionality available by using x command and then use f command to correct the partition table order. Since in this example our disk partition table is already in order so it won’t do any changes here.

[root@localhost ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): x

Expert command (m for help): f
Nothing to do. Ordering is correct already.


Expert command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Example 14: How to Disable/Enable DOS Compatibility Mode 

If you want to disable/enable DOS compatibility mode from a disk partition then you need to use c command as shown below. In this example we are changing the DOS Compatibility mode of partition /dev/sdc1 using c command.

[root@localhost ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): c
DOS Compatibility flag is not set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Example 15: How to Check all the available options of fdisk command

To check all the available options of fdisk command you need to use m command as shown below. This command will list all the available actions that can be done on disk partition using fdisk command.

[root@localhost ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help):

Example 16: How to check Man Page of fdisk command

Man Page of fdisk command can be checked using man fdisk command as shown below.

[root@localhost ~]# man fdisk
FDISK(8) System Administration FDISK(8)

NAME
fdisk - manipulate disk partition table

SYNOPSIS
fdisk [-uc] [-b sectorsize] [-C cyls] [-H heads] [-S sects] device

fdisk -l [-u] [device...]

fdisk -s partition...

fdisk -v

fdisk -h

DESCRIPTION
fdisk (in the first form of invocation) is a menu-driven program for creation and manipulation of partition tables. It understands GPT (experimental for
now), MBR, Sun, SGI and BSD partition tables.

fdisk does not use DOS-compatible mode and cylinders as display units by default. The old deprecated DOS behavior can be enabled with the '-c=dos -u=cylin‐
ders' command-line options.

 

 

 

 

Popular Recommendations:-

7 Simple Steps to Install MTR(My Traceroute) on Linux(RHEL/CentOS 7/8)

How to Install Arpwatch tool on RHEL/CentOS 7/8(Simple and Effective Steps)

How to Install and Use i3 Window Manager on Ubuntu 20.04

Troubleshooting Guide to Fix All Postfix mail Server issues in Linux[Practical Solutions]

How to Suppress all the output of a Linux Bash Shell Script

How to Convert/Change time to epoch time using date utility on Linux/Unix Server

25 Practical and Useful rpm command examples in Linux{cheatsheet}

How to Install jq(JSON Processor) on RHEL/CentOS 7/8 

Leave a Comment