Linux fdisk Tutorial With Examples – POFTUT

Linux fdisk Tutorial With Examples


Hi, creating and deleting partitions in Linux generally done in command line interface which makes some user confused. Here I will look it more detail but if you prefer GUI you can use gparted too

Man

In order to get more detailed help man page of fdisk can be used.

$ man fdisk
Man
Man

Help

Simple and fast way to get fdisk options with the parameters following command can be used.

$ fdisk -h
Help
Help

List Disks

Disk on the Linux system can be listed with the following fdisk command. In order to get detailed information about disks fdisk need root privileges. In the recent versions of Ubuntu the special disk named ram disks also listed with -l parameter.

$ sudo fdisk -l
List Disk
List Disk

As we see from the screenshot information like disk name, path, size, sectors count, logical and physical sector sizes are provided with the command.

Fdisk Interactive Shell

Most of the features of fdisk is provided by interactive shell provided by fdisk. In order to start this shell the disk we want to operate should be provided like below.

$ fdisk /dev/vda
Interactive Shell
Interactive Shell

List Partitions

We assume we are in the interactive fdisk shell with the related disk. In the fdisk shell which is shortcut of print will list partition information

p
List Partitions
List Partitions

OR

Disk listing command will provide partition list too

List Partitions Of Specific Disk

There are situations to only list specific disks partitions this can be accomplished by specifying disk path like below.

$ sudo fdisk -l /dev/vda
List Partitions Of Specific Disk
List Partitions Of Specific Disk

Shell Help

Interactive shell provides a lot of operations and commands. These commands can be get with the following command.

m
Shell Help
Shell Help

Change Partition Type

As we now there are a lot of file system or partition type. Windows systems generally uses FAT,VFAT, NTFS on the other hand Linux/Unix systems prefer ext3,ext4,btrfs,xfs like file systems. Partition file system type can be specified with command.

t

Now we will select our partition number where for this example we haveonly one partition and selected by default.

1

To set required partition type we can get list of supported partition types with command like below.

L

Linux type partition is OK for us which number is 83. we enter partition type number. By default all newly created partitions types are created as Linux partition type.

83
Change Partition Type
Change Partition Type

Write Changes To Disk

While using fdisk interactive shell modifications are done in the ram which means they are no effective if we do not write them to the disks. We should write change to the disk with like below.

w
Write Changes To Disk
Write Changes To Disk

Re-Read Disk Changes

After writing disk changes to the disk because of some kernel performance mechanisms they are not directly written. So to make them effective from kernel side following command should be provided in the bash.

$ partprobe

Format Disk Dos Partition Table

Every disks will have their own partition table to identify partitions their sizes, boot options etc. DOS is most used partition table because of the Windows popularity. DOS was default partition table for Windows operating systems. DOS partition table can only use up to 2 TB disks so use with cautiously. Because of the architectural restrictions DOS partition table can not use more than 2 TB

o
Format Disk Dos Partition Table
Format Disk Dos Partition Table

Format Disk GPT Partition Table

GPT partition table is newer than DOS and fixes DOS partition table restrictions like disk size. GPT table supports disks bigger than 2 TB. To create new GPT partition table use following command.

g
Format Disk GPT Partition Table
Format Disk GPT Partition Table

Create Partition

Partition are specified parts of the disks. Partitions provides logical boundaries and makes disk multi part for different usage scenarios. First we will provide new partition command .

n

Then provide partition type with 

p

Now we will provide partition number which is by default 1.

1

First sector of this partition. If we have no specific requirements we can use default one which is 2048

2048

The most important part is last sector number or size. We will set end boundary for this partition. If we enter without any parameter which will use default sector number which is the last unused sector on the disk. Alternative way is specifying disk size in kilobyte,megabyte, gigabyte, terabyte, petabyte. For example to create 10 GB partitions use +10G

+10G
Create Partition
Create Partition

Delete Partition

Created partitions can be deleted easily with command 

d
Delete Partition
Delete Partition

If there is more than one partition the partition number is asked. In example above it is not asked because of there is only one partition.

LEARN MORE  How To Use Chkdsk /F Command?

Partition Information

Detailed information about the partitions can be get in the interactive shell with command.

Partition Information
Partition Information

List Unpartitioned Disk Space

Partitions provides segmentation of disks for different usage scenarios. Some partitions can be created later and for these partitions some free or unpartitioned space can be left. Unpartitioned space can be listed with command F.

F
List Unpartitioned Disk Space
List Unpartitioned Disk Space

Enable Boot Flag

Partitions those contains operating systems like Windows, Linux, Unix should be flagged as bootable. Partitions can be made bootable with command a . Actually command will toggle boot able flag which means if current flag is not bootable it will change to boot able vice versa.

a
Enable Boot Flag
Enable Boot Flag

Verify Partition Table

Partition tables can be simply verified like below.

v
Verify Partition Table
Verify Partition Table

Quit/Exit

After completing operations to quit or exit from fdisk interactive shell command can be used. Do not forget to save changes if you need them.

q
Quit/Exit
Quit/Exit

 

Linux fdisk Tutorial With Examples Infographic

Linux fdisk Tutorial With Examples Infographic
Linux fdisk Tutorial With Examples Infographic

1 thought on “Linux fdisk Tutorial With Examples”

Leave a Comment