Useful Linux Fdisk Command With Examples – A Linux Disk Partition Tool

In this section, we are going to learn how to use Linux Fdisk command. Linux fdisk command is also known as Fixed Disk is a very powerful command is used for disk partition. fdisk was first introduced by IBM in the year 1983. Let’s have a look at some very useful and important Linux fdisk command with examples.

Useful Linux Fdisk Command With Examples – A Linux Disk Partition Tool
Useful Linux Fdisk Command With Examples – A Linux Disk Partition Tool

Linux fdisk command with examples:

To check the manual or help page of the Linux fdisk command tool use fdisk command with argument -h.

[root@localhost ~]# fdisk -h  # Checking help page of fdisk

Usage:
 fdisk [options]     change partition table
 fdisk [options] -l  list partition table(s)
 fdisk -s       give partition size(s) in blocks

Options:
 -b                  sector size (512, 1024, 2048 or 4096)
 -c                        switch off DOS-compatible mode
 -h                        print help
 -u                  give sizes in sectors instead of cylinders
 -v                        print version
 -C                specify the number of cylinders
 -H                specify the number of heads
 -S                specify the number of sectors per track

To check the Version of the installed fdisk command package version we can use fdisk command with argument -v.

[root@localhost ~]# fdisk -v   # Checking fdisk package Verison
fdisk (util-linux-ng 2.17.2)

fdisk command with option -s will return you the size of the partition in Blocks.

[root@localhost ~]# fdisk -s /dev/sda1  # Check the Partition Size in Blocks
307200

To list the disk partition table use the fdisk command with argument -l. By running this command you will able to get below Disk Informations :

  • Disk Cylinder Details
  • Sector & Track details
  • Type of Partition (eg: Swap Partition, LVM Partition, RAID Partition…etc.)
  • Block Details
[root@localhost ~]# fdisk -l  # List Partition Table

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00088ea1

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2350    18566144   83  Linux
/dev/sda3            2350        2611     2097152   82  Linux swap / Solaris

To List details of a Particular Partition using fdisk command refer to the below command.

[root@localhost ~]# fdisk -l /dev/sda1  # Checking Details of a Particular Partition

Disk /dev/sda1: 314 MB, 314572800 bytes
255 heads, 63 sectors/track, 38 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

As we know that fdisk command is used for disk partition. We have to look at so many most useful commands that we have during disk partition creation. So here I am creating a partition.

[root@localhost ~]# fdisk /dev/sdb  # To create a Partition
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x720b712d.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): # Here we have so many options to Learn for that enter "m" for Help

So refer to the below command where I listed the Partition creation options (Highlighted in Yellow color).

[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x720b712d.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): m   # Enter "m" for Options
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   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)

Now let’s create a Partition so that we can learn and understand all the above disk partition options.

[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xa83d36ba.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n ---> Enter "n" to create a New Partition

# Here we have two options i.e. Primary and Extended, So enter "p" if you want to create a Primary Partition & enter "e"
if you want to create extended partition.

Command action
   e   extended
   p   primary partition (1-4)
p   ----> Here I am creating Primary Partition
Partition number (1-4): 1   # Enter Number for Primary Partition. We can create only 4 Primary Partition/Disk
First cylinder (1-391, default 1): # Press ENTER to allow the system to take cylinder automatically
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-391, default 391): +2G   # Enter Space for the Partition

Command (m for help): w   # Enter "w" to Save the Partition Table
The partition table has been altered!

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

After creating the Partition format the partition using the below command.

[root@localhost ~]# mkfs.ext3 /dev/sdb1   # Formatting the Partition
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131648 inodes, 526120 blocks
26306 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=541065216
17 block groups
32768 blocks per group, 32768 fragments per group
7744 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912

Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

# Now create a Directory and Mount the Partition

[root@localhost ~]# mkdir /data
[root@localhost ~]# mount /dev/sdb1 /data/   # Mounting the Partition
[root@localhost ~]# df -h /dev/sdb1   # Confirming the Mounted Device
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       2.0G   68M  1.9G   4% /data

Option “p” allows us to Print the Partition Table.

[root@localhost ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p   # Enter the "p" to print the Partition Table

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa83d36ba

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         262     2104483+  83  Linux

Option “t” allows us to change the partition ID. For example, if you want to configure LVM or Software RAID then for that first you have to change the partition ID like Partition ID for LVM is 8e and Partition ID for RAID is fd. To list more Partition IDs for different need’s press “L”.

[root@localhost ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): t    # To change the Partition ID
Selected partition 1
Hex code (type L to list codes): L   # To List the available Partition ID's
0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16

Option “q” is for quit the Partition creation without saving anything.

[root@localhost ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): q  ---> To quit the Partition Creation

Option “d” is to delete the Partition.

[root@localhost ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): d   # Enter "d" to delete the Partition
Selected partition 1

Command (m for help): w   # Enter "w" to Save the Partition Table
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.

Option “a” is for enabling or disable the bootable flag in the partition. You have to enable the boot flag for a partition if you have installed the GRUB in root Partition instead of MBR. Otherwise, the system will not boot. But if you installed the GRUB on MBR then there is no need for a Boot flag.

[root@localhost ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): a   # To enable or disable Bootable Flag on Partition
Partition number (1-4): 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 Enable the Bootflag on Partition you will notice a * on the Partition (Highlighted in Red Color).

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

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa83d36ba

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1         262     2104483+  83  Linux

For more advanced options like changing the Cylinder number, change the number of head, fix partition order..etc…you can use the option “x”. This option is for Experts. All options are listed below.

[root@localhost ~]# fdisk /dev/sdb
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): x   # For advance disk related Operations

Expert command (m for help): m   # Enter "m" to List Advance disk Options
Command action
b move beginning of data in a partition
c change number of cylinders
d print the raw data in the partition table
e list extended partitions
f fix partition order
g create an IRIX (SGI) partition table
h change number of heads
i change the disk identifier
m print this menu
p print the partition table
q quit without saving changes
r return to main menu
s change number of sectors/track
v verify the partition table
w write table to disk and exit

To Print the size of the partition in Sectors instead of Cylinder, we can use fdisk command with option -u.

[root@localhost ~]# fdisk -u -l /dev/sda1   # To print the Partition ID in Sectors instead of Cylinder

Disk /dev/sda1: 314 MB, 314572800 bytes
255 heads, 63 sectors/track, 38 cylinders, total 614400 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 identifier: 0x00000000

For more fdisk command related commands and options, you can use the below command.

[root@localhost ~]# man fdisk  # For more fdisk command related options

We tried to include a possible Linux fdisk command, If you something missed out then please comment in the comment box below, So that we can include that in the article.

Also ReadBest Linux Grep Command Examples

That’s all, In this article, we have explained Useful Linux Fdisk Command With Examples – A Linux Disk Partition Tool. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.

Share this:
WhatsApp Channel Join Now
Telegram Channel Join Now
Instagram Channel Join Now

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.