Useful Linux Fdisk command with examples – A Linux Disk Partition Tool

This section tells you how to use the Linux Fdisk command. The Linux fdisk command is also known as a fixed disk. This is a very powerful command used for the disk partition. fdisk was first introduced by IBM in 1983. Let’s look at some very useful and important Linux commands for fdisk with examples.

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, use the fdisk command with an argument -H,

[[email protected] ~]# 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, we can use the fdisk command with an argument -v,

[[email protected] ~]# fdisk -v   # Checking fdisk package Verison
fdisk (util-linux-ng 2.17.2)

Fdisk command with option -s Returns the size of the partition in blocks.

[[email protected] ~]# fdisk -s /dev/sda1  # Check the Partition Size in Blocks
307200

To list the disk partition table, use the fdisk command with an argument -l, You can get the following disk information by running this command:

  • Details about the cylinder
  • Sector & track details
  • Partition type (e.g. swap partition, LVM partition, RAID partition, etc.)
  • Block details
[[email protected] ~]# 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 the fdisk command, run the following command.

[[email protected] ~]# 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, the fdisk command is used for the disk partition. We have to look at so many useful commands that we have while creating disk partitions. So I’m going to create a partition here.

[[email protected] ~]# 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

Therefore, follow the following command, which lists the options for creating partitions (highlighted in yellow).

[[email protected] ~]# 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 of the above options for the hard disk partition.

[[email protected] ~]# 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 following command.

[[email protected] ~]# 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

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

possibility “P” allows us to print the partition table.

[[email protected] ~]# 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

possibility “T” allows us to change the partition ID. For example, if you want to configure LVM or software RAID, you must first change the partition ID like the partition ID for LVM 8e and partition ID for RAID fd, To list additional partition IDs for different needs, press “L”,

[[email protected] ~]# 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

possibility “Q” Ends partition creation without saving anything.

[[email protected] ~]# 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

possibility “D” is deleting the partition.

[[email protected] ~]# 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.

possibility “on” is used to activate or deactivate the boot flag in the partition. You must enable the boot flag for a partition if you installed GRUB in the root partition instead of MBR. Otherwise the system will not start. However, if you installed GRUB on the MBR, the boot flag is not required.

[[email protected] ~]# 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).

[[email protected] ~]# 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

You can use the option for advanced options such as changing the cylinder number, changing the head number, setting the partition order, etc. “X”, This option is for experts. All options are listed below.

[[email protected] ~]# 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 cylinders, we can use the fdisk command with the option -u,

[[email protected] ~]# 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 other fdisk command-related commands and options, you can use the following command.

[[email protected] ~]# man fdisk  # For more fdisk command related options

We tried to include the Linux fdisk command as much as possible. If you missed anything, please comment in the comment box below so we can include it in the article.

Read Also – Best Examples of Linux Grep Commands

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

Sidebar