Linux fdisk Tutorial With Examples
Hi, creating and deleting partitions in linux generally done in cli which makes some user confused. Here I will look it more detail but if you prefer gui you can use gparted too
In order to get more detailed help man page of fdisk can be used.
$ man fdisk
Simple and fast way to get fdisk options with the parameters following command can be used.
$ fdisk -h
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
$ sudo fdisk -l
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
We assume we are in the interactive fdisk shell with the related disk. In the fdisk shell p which is shortcut of print will list partition information
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
Interactive shell provides a lot of operations and commands. These commands can be get with the following command.
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 t command.
Now we will select our partition number where for this example we haveonly one partition and selected by default.
To set required partition type we can get list of supported partition types with L command like below.
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.
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 w like below.
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.
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
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.
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.
First sector of this partition. If we have no specific requirements we can use default one which is 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
Created partitions can be deleted easily with command d
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.
Detailed information about the partitions can be get in the interactive shell with i command.
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.
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 a will toggle boot able flag which means if current flag is not bootable it will change to boot able vice versa.
Verify Partition Table
Partition tables can be simply verified like below.
After completing operations to quit or exit from fdisk interactive shell q command can be used. Do not forget to save changes if you need them.