Home Linux Disk Management How To Create And Manage Disk Partitions With Fdisk In Linux

How To Create And Manage Disk Partitions With Fdisk In Linux

Linux Fdisk Command Examples To Partition Disks

By Karthick
Published: Updated: 13.4K views

The other day, we discussed how to create and manage Linux disk partitions using Parted utility. Today, we will do the same with an another command line utility called 'Fdisk'. This tutorial explains what is Fdisk utility, and how to create and manage disk partitions with Fdisk in Linux.

What is Fdisk?

Fdisk is a command-line menu-driven utility that is shipped with all Linux distributions to manipulate your disk partition table.

When you get a fresh piece of the disk, you need to create partitions on top of the disk to start using the disk. With Fdisk, you can maintain the complete lifecycle of a partition starting from labeling the partition to either MBR or GPT type among the other supported label types, creating a partition from the drive, resizing a partition, deleting a partition, etc.

To check if the fdisk is already installed on your machine run any one of the following commands.

$ fdisk --version
fdisk from util-linux 2.32.1
$ which fdisk
/usr/sbin/fdisk
$ whereis fdisk
fdisk: /usr/sbin/fdisk /usr/share/man/man8/fdisk.8.gz

Fdisk Commands To Create And Manage Linux Disk Partitions

For the demonstration purpose, I am using Rocky Linux 8 running in Virtualbox. I am using 2 disks, wherein my first disk my operating system is installed, and the second drive is a new drive with no partition on it.

Heads Up: Fdisk requires elevated privilege. Either use the root user or use sudo.

1. Print the Partition Table

Run the following command to display all the disks and their partitions. You can get the output like disklabel type, how many partitions are created for the drive, type of the partitions, partition size, and a few more information.

$ fdisk -l

Sample output:

Print Partition Table
Print Partition Table

If you wish to print the partition table for the particular drive, you have to use the block device name.

$ fdisk -l /dev/sda

Sample output:

Print Partition Table For Particular Drive
Print Partition Table For A Particular Drive

You can also get this information from within the Fdisk interactive console window. When you do not pass the -l flag with fdisk, it will go to the interactive menu where you can type "p" and press enter.

Fdisk Interactive Menu
Fdisk Interactive Menu

2. List Fdisk Options

To get the list of supported options for the interactive menu, type 'm' in the Fdisk console window and press enter.

Fdisk Help Options
Fdisk Help Options

3. DiskLabel Type

To create partitions, first, we should decide which partition scheme is going to be used on the disk. MBR and GPT are the two most used partition schemes. By default when you use fdisk against a new drive, it will create a DOS partition scheme.

If you wish to create GPT or other supported partition schemes, type 'm' and look out for the "create a new label" section where you can get the list of options to create a partition table.

Disk Label Options
Disk Label Options

To create a GPT partition scheme, type "g" and press enter.

Command (m for help): g
Created a new GPT disklabel (GUID: 73749F7E-1B28-874D-94AE-DED4CE70D269).

Whatever changes you are making will be in memory until you write them to the disk. Type "w" and press enter to write the changes.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

If you run the list command again, you can see the disklabel is changed to "gpt".

GPT Partition Scheme
GPT Partition Scheme

4. Create a Linux Partition with Fdisk

To create a new partition type "n" and press enter. It will ask you for a series of inputs.

Firstly, select the partition number.

Partition number (1-128, default 1):

Heads Up: For the MBR partition scheme, you can create only 4 primary partitions. If you wish to create more partitions in MBR, you have to create a logical partition and create additional partitions. For GPT partitions you can create up to 128 primary partitions.

You can leave this to default which will pick the next available number for the partition.

Next, you have to choose the first sector and last sector. Leave the first sector empty and in the last sector you can give the partition size in terms of KB, MB, GB, TB, PB. Here I am creating the Partition of size 10GB, so I have given it as +10G.

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

Now a partition of size 10 GB will be created. To save the changes, type 'w' and write the changes to the disk.

You can view the changes by typing 'p' in the interactive mode.

Command (m for help): p
Disk /dev/sdb: 30 GiB, 32212254720 bytes, 62914560 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
Disklabel type: gpt
Disk identifier: 73749F7E-1B28-874D-94AE-DED4CE70D269

Device        Start      End  Sectors Size Type
/dev/sdb1      2048 20973567 20971520  10G Linux filesystem
/dev/sdb2  20973568 41945087 20971520  10G Linux filesystem

You can also run the lsblk command to check if the partition is created successfully. I have repeated the same process and created one more partition with 10G size.

[root@rockylinux ~]# lsblk -p /dev/sdb
NAME        MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
/dev/sdb      8:16   0  30G  0 disk 
├─/dev/sdb1   8:17   0  10G  0 part 
└─/dev/sdb2   8:18   0  10G  0 part 

5. Set Partition as Boot Partition

If in case you wish to use the newly created partition as boot partition, then you type 'a' in the interactive menu and it will prompt for the partition number which will be tagged as boot partition.

Set A Partition As Boot Partition
Set A Partition As Boot Partition

This is supported only if your disk is labeled to the MBR partition scheme. If you try the option in GPT, you will get an error like below.

Unknown Command Error Message
Unknown Command Error Message

6. Change Partition Type

When you create a new partition, it will be tagged to the "Linux Filesystem" type by default.

Partition Type
Partition Type

You can change the type for the partition. Type 't' and press enter.

Command (m for help): t
Selected partition 1
Partition type (type L to list all types):

It will ask you to select the partition type. You can type "L" and press enter to get the list of all partition types. In total 87 different partition types are supported.

List Of Partition Types
List Of Partition Types

Let’s say if I wish to tag the partition as a swap partition, then I need to type the number 19.

Partition type (type L to list all types): 19
Changed type of partition 'Linux filesystem' to 'Linux swap'

7. Delete Partition

To delete a partition, launch the fdisk utility with the appropriate disk.

$ fdisk /dev/sdb

Here, I want to delete the second partition( /dev/sdb2). Type 'd' in the interactive menu and it will ask you to give the partition number.

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

Partition 2 has been deleted.

Check the changes by typing 'p' in the interactive menu.

Delete Partition
Delete Partition

If your partition contains data and is mounted, make sure first you unmount the file system then delete the partition.

8. Create the File System & Mount the Partition

Once the partition is created, you have to format the partition with the file system and mount it. I am using EXT4 to format my partition.

$ mkfs.ext4 /dev/sb1
Format File System
Format File System

Mount the partition to any directory in the file system. I am mounting the partition under /opt/.

$ mount /dev/sdb1 /opt/

Run the lsblk command to check the mount point for the partition.

$ lsblk -f /dev/sdb1
NAME FSTYPE LABEL UUID                                 MOUNTPOINT
sdb1 ext4         fbac8781-406b-47bc-b1f3-8484de24b6e1 /opt

To automatically mount the partition every time when you boot the machine, you need to add the partition entry in the /etc/fstab file.

Add Partition Entry In Fstab File
Add Partition Entry In Fstab File

If you want to learn more about fstab file and how to use it, refer the following link.

Conclusion

In this article, we have discussed some Fdisk command examples to perform various disk operations. Fdisk is a kind of old tool and a good alternative would be to use parted which even has a gui version called GParted which is used in os installers like POP!_OS to create partitions during the os installation process.

Resource:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

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

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More