SharedTutor | Tutorials For Everyone

Find your interesting tutorials in Get your tutorial about Computer, Blogging, Makemoneyonline, Games, Design, and more!

Managing Hard Disk Partitions Using fdisk [Linux]

backup-hard-drive-thumbEven the best, single hard disk installation of Linux where the whole disk can be used for the OS probably has multiple partitions around the disk. If you need to assist the partitions on the disk, Linux provides several unique tools including fdisk.

fdisk can be a menu based, interactive command line tool that lets you view, create, modify and delete partitions with a disk. In Linux, all items are named based on special files perfectly located at the /dev directory. A typical SATA hard disk drive is named /dev/sda. To see a list of hard disks on your system utilize “lshw” command:

sudo lshw -class disk

The output shows the difficult drives and optical drives coupled to the system:

Using lshw to list hard drives

To non-interactively list the partition table around the first hard drive, use:

sudo fdisk -l /dev/sda

The output will appear something like this:


This implies that the first partition /dev/sda1 is the biggest partition and is often a Linux partition. Since it may be the only Linux partition, we also know that it will be the root partition (or perhaps the system partition). sda2 is surely an extended partition (which can be subdivided into multiple logical partitions) and sda5 is the 1st (and just) logical partition within the extended partition. sda5 is utilized as swap space.

Create a fresh partition

The second disk (/dev/sdb) with this test method is empty. To create a new partition run fdisk in the interactive mode:

sudo fdisk /dev/sdb

At the command prompt, type m to see the help menu or p to view the current partition list. To create a whole new primary partition, use the n command.

Enter p to generate a primary partition then pick a partition number, in this case 1. Accept the default starting sector and then enter the height and width of the partition. On the test system, sdb is 100GB so I will create a 50GB partition by entering +50GB. Finally list the partitions with all the p command. To save the partition table to the disk and exit, type w.

Create a new partition with fdisk

Deleting and setting the partition type

To delete a partition, make use of the d command. If the disk has multiple partitions, fdisk will ask which partition to delete, adhere to what they there is only one partition then fdisk will automatically delete it.

If you are making a mistake at any point, utilize the q command to give up without saving. This will leave the harddrive in the same state as if you started fdisk.

Each partition needs to have a partition type. The partition type for Windows is different to the partition type for Linux and the like. There are also partition types for swap space and then for older versions of Windows (before XP) using FAT rather than NTFS. Other Unix-like operating systems such as FreeBSD, OpenBSD or Mac OS X all get their own partition ids.

To see a listing of partition types, use the l command. All the numbers listed come in hexadecimal, by way of example FreeBSD uses a5. Linux uses id 83 and Windows (from XP onwards) uses 7. If the partition is made for use within your Linux installation, leave the partition type as the default 83, but if you want a partition that may be read by multiple operating systems including Windows then you definitely should use either 7 or b.

To alter the id on the partition, use the t command. You will be prompted to the partition number then the partition code. If you have forgotten the code you wish to use, then you certainly can type L, as an alternative to entering a partition type, to see the list again. Once you have entered the partition code, use p to list the partitions and look that the partition type may be set as excepted.

Format and mount

Once a new partition has been created, it must be formatted. For partition types besides 83, it is advisable to format the partition while using the relevant native main system (i.e. Windows for id 7 etc). For Linux use the mkfs.ext3 or mkfs.ext4 commands for any typical partition:

sudo mkfs.ext4 /dev/sdb1

The filesytem then must be mounted by using a command like this:

sudo mount /dev/sdb1 /home/gary/mediastore/

Where /home/gary/mediastore/ may be the directory where you want the disk mounted. Finally the /etc/fstab file needs editing, to find out more please read Getting to know your fstab.


fdisk is really a versatile tool however be sure you backup your data before manipulating the partition table as mistakes will not be free. It is also worth noting that fdisk has some limitations, namely no understand GUID partition tables (GPTs) and it is not created for large partitions. In these cases, utilize the parted tool.

No comments

Post a Comment