How to use fdisk to manage partitions on linux

For computer file systems, fdisk is a command-line utility that provides disk partitioning functions. In versions of the Windows NT operating system line from Windows 2000 onwards, fdisk is replaced by a more advanced tool called diskpart. Similar utilities exist for Unix-like systems.

There are 10 fdisk Commands to Manage Linux Disk Partitions:

1. View all Disk Partitions in Linux

Use this command to check all:
sudo fdisk -l

2. View Specific Disk Partition in Linux

Use this command to check only one:
fdisk -l /dev/sda

3. Check all Available fdisk Commands

This command helps you to use fdisk:
fdisk /dev/sda
Enter “m” for help.

4. Print all Partition Table in Linux

Use this commands and after enter “p” in shell:
fdisk /dev/sda

5. How to Delete a Partition in Linux

Use this command:
fdisk /dev/sda
When asked for command, enter “d”
After, enter partition number that you want to delete.

6. How to Create a New Partition in Linux

Use this command:
fdisk /dev/sda
When asked for command, enter “n”
After, enter “e” for extended partition and “p” for the primary partition.
Next, enter the size of the partition.
To save it, enter “w”.

7. How to Format a Partition in Linux

To format any partition, enter this command:
mkfs.ext4 /dev/sda3
Note: change /dev/sda3 with the disk location that you want to format.

8. How to Check Size of a Partition in Linux

Use this command to view size of a partition:
fdisk -s /dev/sda3

9. How to Fix Partition Table Order

Use this command to fix:
fdisk /dev/sda
Enter “f” in command, to fix it.
And enter “w” in command, to save it.

10. How to Disable Boot Flag (*) of a Partition

Use this command to Disable boot flag:
fdisk /dev/sda
Enter “p” command to view the current partition.
Next, enter “a” to disable flag.
Then enter a number of the partition.

You have successfully learned How to Use Fdisk to Manage Partitions on Linux.
Enjoy it.

By Genesis Emmanuel Gonzales – April 26, 2016 – no comments Email article | Print article

How to use fdisk to manage partitions on linux

Fdisk is a text facility which is used to view and manage the hard disk partition on Linux. It can be the most powerful tool for the which can be used for managing the hard disk partitions, but it is also confusing to the new users.

On some of the distributions of the Linux, there is a need of using a word sudo as prefix before the commands fdisk and mkfs, and those distributions which do not require using this word as a prefix are required to use the word “su” to get to the root shell, then you must type every other command without sudo.

List partitions:

To list the partitions, write…

sudo fdisk –l

You can also add the name of the disk devices name to list only partitions on it. For instance to list only the partitions on the first disk device, you have to write:

sudo fdisk –l /dev/sda

Entering command mode:

If you want to work on the partition of the disk, you just need to enter the command mode. You can get the name of the device from the fdisk –l command. If you want to enter the first disk device, following command is used.

sudo fdisk /dev/sda

There is a warning that does not edit partitions when they are in use. If you desire to edit them boot it from the live CD first.

Using command mode:

In this mode, in this mode you just need to single letter to specify your command and tell the system what to do. Just type m and then press to see the list of the commands

Deleting the partition:

If there is a need to delete the partition use the button ‘d’. the screen will prompt you asking the number of partitions you want to delete, which you have got from the ‘p’ command.

After deleting the desired partition, type the command ‘p’ again if you want to view the current partition. At this type it appears that the partitions are deleted but these partitions are not written on the disk until you press the ‘w’ button.

Creating partition:

In order to create the new partition use the button ‘n’. you have the option to create the logical or primary partition. ‘l’ for the logical and ‘P‘ for the primary partition. A disk can only have 4 primary partitions. The next thing to do is to specify the sector of the partition which you want to start portioning at. To choose the default sector press Enter key.

The last thing to do is to specify the last sector of the partition. After selecting the initial sector if you want to select all available space, press Enter. You have the option that you can also specify the size +6G for the partition of 5 Gigabytes and +512M for the partition of 512 megabytes. If you will not specify the unit after pressing +, fdisk will utilize sectors as the unit.

fdisk is a menu-driven command-line utility, and hence new users find this tool confusing and difficult to use. fdisk is not an easy tool to use and should be used with caution. You need to have root access or sudo privileges to make changes to storage partitions.

So, today in this article, I will give you a tutorial on how to create a partition in Linux using the fdisk command.

You can carry out the following operations using the fdisk command.

  • Create New Partition
  • Manage Space
  • Reorganize space on old drives.
  • Move data to a new disk

How to list partitions?

Before making any changes to the drive, you first look at the basic structure of the drive and its partitions. To view all the partitions on the drive, run the following command in the Linux terminal.

How to use fdisk to manage partitions on linux

Partitions are displayed by their device name, such as dev/sda, dev/sdc, etc., as you can see in the above screenshot.

If you wish to view partition on the specific disk, you have to use ‘-l’ followed by the device name as shown in the command below.

This command will show the output as shown in the screenshot below.

How to use fdisk to manage partitions on linux

Creating Partition:

To create a partition on a specific drive, you have to run the fdisk command with the device name, as shown in the command below.

Once you’re on the device where you want to create a partition, type M and press enter.

You will see the list as shown below screenshot. This is the list of single-letter commands you can use to take specific action using fdisk.

How to use fdisk to manage partitions on linux

To create a partition, you have to use the N command. Both logical and primary partitions can be created where I denote logical partition and P denotes primary partition.

By default, the first free sector on the disk is chosen to create a new partition. Just like that, you also have to specify the last sector of the partition. You can also specify the size of the partition according to your requirement.

How to use fdisk to manage partitions on linux

Deleting Partition:

To delete the specific partition, you have to use the D command. Use the P command to make sure which partition you want to delete.

When you fire the D command, it will ask for the partition number you want to delete. Just type that partition number and hit the enter button. This will delete the selected partition.

So, this is how you can use the fdisk command to create and delete the partition on Linux. fdisk is a very useful command-line tool when used efficiently and correctly. Make sure you have the back of the partitions where you are going to use the fdisk command. Good day!

About the author

How to use fdisk to manage partitions on linux

Swapnil Tirthakar

A Software Engineer who loves football and passionate about traveling. I often spend my free time playing with gadgets and exploring new possibilities in tech world. I am Linux enthusiast and have about 6 years of experience in web development. I have good command on Python, Java, SQL and system security.

Fdisk is a text based utility used in Linux to manage disk partitions. Using fdisk you can view all disk partitions, create a new partition, delete an existing hard disk partition and view the size of the partition.

Using fdisk you are allowed to create a maximum of four primary partitions, and any number of logical partitions, based on the size of the disk. Keep in mind that any single partition requires a minimum size of 40MB.

In this tutorial, let us learn how to use fdisk command in linux with some examples.

1) View all existing disk partitions

Before you create a new partition or modify an existing partition, you might want to view all available partitions in the system.

The command will list partitions from all the connected hard disks. When you have more than one disk on the system, the partitions list are ordered by the device’s /dev name. For example, /dev/sda, /dev/sdb, /dev/sdc and so on.

Use fdisk -l to view all available partitions as following:

2) View partitions of a specific hard disk

To view all partitions of the ‘/dev/sda’ hard disk, do the following.

View all fdisk Commands Using fdisk Command m

Use fdisk command m , to view all available fdisk commands as shown below:

3) Delete a hard disk partition

Let us assume that you like to combine several partitions (for example, /dev/sda6, /dev/sda7 and /dev/sda8) into a single disk partition. To do this, you should first delete all those individual partitions, as shown below.

4) Create a new disk partition

Once you’ve deleted all the existing partitions, you can create a new partition using all available space as shown below.

After the partition is created, format it using the mkfs command as shown below.

5) View the size of an existing partition

As shown below, fdisk -s displays the size of the partition in blocks.

In the following example it shows the size ‘/dev/sda7’ as 4900MB.

6) Toggle the boot flag of a partition

Fdisk command displays the boot flag of each partition. When you want to disable or enable the boot flag on the corresponding partition, do the following.

As seen above, the boot flag is disabled on the partition ‘/dev/sda5’.

7) Fix partition table order

When you delete a logical partition and recreate it again, you might see the ‘partition out of order’ issue. i.e ‘Partition table entries are not in disk order’ error message.

For example, when you delete three logical partitions (sda6, sda7 and sda8), and create a new partition, you might expect the new partition name to be sda6. But, the system might’ve created the new partition as sda7. This is because, after the partitions are deleted, ‘sda9’ partition has been moved as sda6 and the free space is moved to the end.

To fix this partition order issue, and assign sda6 to the newly created partition, execute the expert command f as shown below.

Once the partition table order is fixed, you’ll not get the ‘Partition table entries are not in disk order’ error message anymore.

Conclusion

In this tutorial, we learned how to use fdisk command in Linux to manage partitions. I hope you enjoyed reading and please leave your suggestion on the below comment box. Note: Don’t delete, modify, or add partition, if you don’t know what you are doing. You may end up losing your data. Be careful.

There are different options to create or manage partitions under Oracle Linux – fdisk is one of them. This post describes its usage and the use case when creating a new partition table. fdisk is a menu-driven program for the creation and manipulation of partition tables. It understands GPT (experimental for now), MBR, Sun, SGI, and BSD partition tables.

Hard disks can be divided into one or more logical disks called partitions. This division is recorded in the partition table, found in sector 0 of the disk. (In the BSD world one talks about ‘disk slices’ and a ‘disklabel’.)

Linux needs at least one partition, namely for its root file system. It can use swap files and/or swap partitions, but the latter is more efficient. So, usually one will want a second Linux partition dedicated as a swap partition. On Intel-compatible hardware, the BIOS that boots the system can often only access the first 1024 cylinders of the disk. For this reason, people with large disks often create a third partition, just a few MB large, typically mounted on /boot, to store the kernel image and a few auxiliary files needed at boot time, so as to make sure that this stuff is accessible to the BIOS. There may be reasons of security, ease of administration and backup, or testing, to use more than the minimum number of partitions.

The basic fdisk commands needed are:

  • p print the partition table
  • n create a new partition
  • d delete a partition
  • q quit without saving changes
  • w write the new partition table and exit

Changes you make to the partition table do not take effect until you issue the write (w) command.

Further information can be found at the man pages:

Partitioning a disk with fdisk

1. Run fdisk against the desired device “/dev/device_name”.

2. Choose option “p” to print the partition table of the disk:

3. Choose option “n” to create a new partition:

Here is an exaple of creating another partition on the same disk:

4. Check the partition table with the option “p“:

5. Choose the option “w” to write the new partition or partitions.

6. Run the command partprobe to inform the OS of partition table changes:

fdisk command is a free and open source utility widely available on Linux Based Platforms to Manage Disk Partitions Table. It understand different partitions table like BSD, GPT, MBR, SUN and SGI. fdisk command can be used to create a new partition, delete a partition, change the partition ID, resize the partition, move a partition and so on.

One can create a maximum of four primary partition using fdisk command and n number of logical partitions based on available disk size. Partition number from 1 to 4 are reserved for Primary partitions and then logical partition starts from number 5. fdisk command creates partition of minimum 40 MB size and maximum of 2 TB size.

What is Block Device

A Block device in Linux is defined as a file which represents devices like Hard Disk, CD-ROMs from where data can be read in fixed size blocks.

What is Partition

A Block device are usually divided into one or more logical disks which are known as partitions. The information of these partitions are stored in a table Known as Partition Table.

Synopsis

fdisk [-uc] [-b sectorsize] [-C cyls] [-H heads] [-S sects] device

fdisk -l [-u] [device. ]

fdisk -s partition.

fdisk -v

fdisk -h

How to use fdisk to manage partitions on linux

Fdisk Command Examples to Manage Disk Partitions in Linux

Example 1: How to Check fdisk Command Version

To check fdisk command version you need to use -v option as shown below. Here current fdisk version is 2.23.2 .

Example 2: How to List All Disk Partitions in Linux

If you want to List all the disk partitions then you need to use -l option with fdisk command as shown below. Here we have two disks /dev/sda and /dev/sdb but partitions are created only on /dev/sda disk. We have two partitions /dev/sda1 and /dev/sda2 available on /dev/sda disk.

-l : List the partition tables for the specified devices and then exit. More on fdisk Man Page.

Example 3: How to Check total number of partitions in a Disk

If you want to check all the partitions of a Specific disk then you can also use -l option along with the disk name. In this example we are checking all the partition information of /dev/sdc disk using fdisk -l /dev/sdc command as shown below.

Example 4: How to Delete a Disk Partition in Linux

To delete a disk partition you need to use command d as shown below. In this example we are deleting partition /dev/sdc1 using d command and then writing the partition table changes using w command.

Example 5: How to Create a Disk Partition in Linux

To create a disk partition you need to use n command as shown below. In this example we are creating partition on /dev/sdc disk using fdisk /dev/sdc command. Then selecting n command to create a new partition and selecting p to create it as a primary partition type. Here we are using default sector value i.e first sector value as 2048 and last sector value as 16777215 which will create partition on entire 8 GB disk and then writing the changes to partition table using w command.

Example 6: How to Check the Size of Disk Partition in Linux

To check the size of disk partition you can use -s option with fdisk command as shown below. In this example we are checking the size of /dev/sdc1 partition using fdisk -s /dev/sdc1 command. Here you will see size in number of blocks. So the total size of /dev/sdc1 partition is 8387584 blocks.

-s : Print the size (in blocks) of each given partition. More on fdisk Man Page.

Example 7: How to Verify Disk Partition table in Linux

Another useful command that you can use is v command to verify the disk partition table in Linux. In this example we are verifying /dev/sdc disk partition using v command and then writing the changes to partition table using w command.

Example 8: How to Quit fdisk Command without saving any changes

To quit fdisk command utility without saving any changes you need to use q command as shown below. In this example we have exited the /dev/sdc disk changes without saving it by using q command.

Example 9: How to Save and Exit fdisk Utility

If you want to exit after saving partition table information then you need to use w command as shown below. This command will save all the partition table changes and then exit out from fdisk command.

Example 10: How to Change Disk Partition ID to “Linux LVM” type

If you want to change disk partition ID to Linux LVM type then you need to use hex code 8e after selecting the partition through t command as shown below. In this example we are changing the partition Id of /dev/sdc1 partition from 83 to 8e to convert the partition from normal disk to LVM type.

Example 11: How to Change Disk Partition ID to “Linux Swap” Type

For creating a swap partition you need to change the partition type to Linux swap type by using hex code 82 as shown below. In this example we are changing partition ID of /dev/sdc1 from normal partition to Linux swap type.

Example 12: How to Disable/Enable Boot Flag(*) of a Partition

If you want you can also disable/enable boot flag of a partition using fdisk command in Linux. As you can see from below output, partition /dev/sda1 is currently marked as boot partition.

To disable the boot flag from /dev/sda1 partition you need to use command a under fdisk utility and then select the partition to disable the boot flag as shown below.

After writing the disk partition table changes if you check the /dev/sda1 partition status again then you can notice that star(*) marked is removed now. This confirms boot flag is now disabled. You can follow the same procedure to enable it again.

Example 13: How to Correct the Partition Table Order

If you want to correct the partition table order then you need to use the extra functionality available by using x command and then use f command to correct the partition table order. Since in this example our disk partition table is already in order so it won’t do any changes here.

Example 14: How to Disable/Enable DOS Compatibility Mode

If you want to disable/enable DOS compatibility mode from a disk partition then you need to use c command as shown below. In this example we are changing the DOS Compatibility mode of partition /dev/sdc1 using c command.

Example 15: How to Check all the available options of fdisk command

To check all the available options of fdisk command you need to use m command as shown below. This command will list all the available actions that can be done on disk partition using fdisk command.

Example 16: How to check Man Page of fdisk command

Man Page of fdisk command can be checked using man fdisk command as shown below.

How to use fdisk to manage partitions on linux

Fdisk(fixed disk or format disk) is a text-based command-line utility for viewing and managing hard disk partitions on Linux. Using fdisk you can view, create, resize, delete, change, copy and move partitions. Fdisk allows you to create a maximum of four primary partitions as permitted by Linux with each requiring a minimum size of 40mb. You can also have a much larger number of logical partitions by subdividing a primary partition. ​In this article, let us review how to use fdisk command to manage disk partitions by employing some very simple and practical examples.

Important points to note

  1. Fdisk requires root to operate. On Debian and Ubuntu based systems, you can prefix with sudo. On other distros, use su- command to get a root shell and then enter the commands without sudo.
  2. Take note of the form the device names (disks) are presented. Usually, you see the following names
  • /dev/hd[a-h] for IDE disks
  • /dev/sd[a-p] for SCSI disks
  • /dev/ed[a-d] for ESDI disks
  • /dev/xd[ab] for XT disks

​ 3. Warning: Do not delete, modify, or add partition unless you know exactly what you are about. You ran the risk of losing your data!

1. View All Existing Disk Partitions

​The first thing you want to do before tampering with your disks and partition is to view basic details about all available partitions in the system. The commands list the partitions on your system. When you have more than one disk available, the partitions list is ordered by the device’s /dev name as in /dev/sda, /dev/sdb and so on. Use the following command to view all existing partitions in the system –

2. View Partition on a Specific Disk

​If you want to look at all the partitions on a specific disk, use the following command to view all disk partitions on device /dev/sdb. # sudo fdisk -l /dev/sdb

3. View all fdisk Commands

​You can use the following command to view all available fdisk – # sudo fdisk /dev/sda This will provide you with a prompt. Enter m to see the list of all available commands of fdisk which can be operated on /dev/sda.

Command (m for help): m

The output will be this – 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)

4. Delete a Hard Disk Partition

Assuming you need to combine some partitions to form a larger one, you will first have to delete those partitions. To delete a specific partition on /dev/sdb2, follow the following -1. Enter the following command to enter/view the disk – # sudo fdisk /dev/sdb 2. Enter ‘d’ to delete a partition.​Command (m for help): d

3. You will be prompted to enter the partition number. We enter 2 in our case to delete /dev/sdb2. Save changes by entering ‘w’ at command and reboot for changes.

5. Create a New Disk Partition with Specific Size

To create a new partition, enter the following command -1. Enter the following command to enter/view the disk
# sudo fdisk /dev/sdb

2. Enter ‘n’ to create a new partition that will prompt you to specify for either a primary partition or an extended partition. Enter ‘p’ for a primary partition or ‘e’ for an extended partition.

3. You will then be prompted to enter the first cylinder or sector number of the partition to be created. You may press Enter to accept the defaults.

4. You will then be prompted to enter the Last cylinder or sector number of the partition to be created. You may press Enter to use all available space after the first sector or enter a specific size such as +2G or +256M for a 2 gigabyte or 256 megabyte partition respectively.

5. Run ‘w’ command to write the changes and reboot your system.

6. How to Format a Partition?

After you have created your partition, you need to format it with a file system. Use the following command to format to ext4 file format –

7. How to View the Size of your new or existing Partition?

​If you want to check the size of a specific partition, use the following command to check the size of the partition on /dev/sda2.

8. Viewing the Partition Table

​You can use the following command to look at your current partition table –
Use ‘p’ in command mode.

Command (m for help): p

Conclusion

​With this simple tutorial, you can manage your disks and partitions with Fdisk. Hope this has been useful. If you have any issues with anything above, let me know in the comment section below this article. Thank you!

Mohd Sohail is a web developer and a Linux sysAdmin. He also loves to write how-to articles, applications reviews and loves to use new Linux distributions.

2 responses to “‘fdisk’ Command To Manage Disk Partitions In Linux”

Dear Sir,
Thanks so much for sharing your powerful in depth of Linux tutorials(creating partition)which has really change my perspective and also broaden my horizon in Linux.
I say kudos and God bless you.I want to be a master in Linux so kindly show me steps I should take daily for me to be powerful in Linux and Linux Bash.
Thanks
Acquaye

You can start with installing a simple beginner friendly Linux distribution. I have written an article covering best beginner-friendly Linux distributions. Check that out here –

You can follow Linux communities including LinuxAndUbuntu. I have also written an article covering best sources to learn Linux for free.

There are different options to create or manage partitions under Oracle Linux – fdisk is one of them. This post describes its usage and the use case when creating a new partition table. fdisk is a menu-driven program for the creation and manipulation of partition tables. It understands GPT (experimental for now), MBR, Sun, SGI, and BSD partition tables.

Hard disks can be divided into one or more logical disks called partitions. This division is recorded in the partition table, found in sector 0 of the disk. (In the BSD world one talks about ‘disk slices’ and a ‘disklabel’.)

Linux needs at least one partition, namely for its root file system. It can use swap files and/or swap partitions, but the latter is more efficient. So, usually one will want a second Linux partition dedicated as a swap partition. On Intel-compatible hardware, the BIOS that boots the system can often only access the first 1024 cylinders of the disk. For this reason, people with large disks often create a third partition, just a few MB large, typically mounted on /boot, to store the kernel image and a few auxiliary files needed at boot time, so as to make sure that this stuff is accessible to the BIOS. There may be reasons of security, ease of administration and backup, or testing, to use more than the minimum number of partitions.

The basic fdisk commands needed are:

  • p print the partition table
  • n create a new partition
  • d delete a partition
  • q quit without saving changes
  • w write the new partition table and exit

Changes you make to the partition table do not take effect until you issue the write (w) command.

Further information can be found at the man pages:

Partitioning a disk with fdisk

1. Run fdisk against the desired device “/dev/device_name”.

2. Choose option “p” to print the partition table of the disk:

3. Choose option “n” to create a new partition:

Here is an exaple of creating another partition on the same disk:

4. Check the partition table with the option “p“:

5. Choose the option “w” to write the new partition or partitions.

6. Run the command partprobe to inform the OS of partition table changes:

Recently, one of our customers asked us to upgrade his server hardware. Unfortunately, they can’t…

Basic information smartd is a daemon that monitоrs the Self-Monitoring, Analysis and Reporting Technology (S.M.A.R.T)…

Recently, We described how to configure your server for high load and prevention of DDoS.…

Introduction If you are using old software on new software you can receive some weird…

Introduction Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and…

How to use fdisk to manage partitions on linuxOn Linux distributions, fdisk is the best tool to manage disk partitions. fdisk is a text based utility.

Using fdisk you can create a new partition, delete an existing partition, or change existing partition.

Using fidsk you are allowed to create a maximum of four primary partition, and any number of logical partitions, based on the size of the disk.

Keep in mind that any single partition requires a minimum size of 40MB.

In this article, let us review how to use fdisk command using practical examples.

Warning: Don’t delete, modify, or add partition, if you don’t know what you are doing. You will lose your data!

1. View All Existing Disk Partitions Using fdisk -l

Before you create a new partition, or modify an existing partition, you might want to view all available partition in the system.

Use fdisk -l to view all available partitions as shown below.

The above will list partitions from all the connected hard disks. When you have more than one disk on the system, the partitions list are ordered by the device’s /dev name. For example, /dev/sda, /dev/sdb, /dev/sdc and so on.

2. View Partitions of a Specific Hard Disk using fdisk -l /dev/sd

To view all partitions of the /dev/sda hard disk, do the following.

View all fdisk Commands Using fdisk Command m

Use fdisk command m, to view all available fdisk commands as shown below.

3. Delete a Hard Disk Partition Using fdisk Command d

Let us assume that you like to combine several partitions (for example, /dev/sda6, /dev/sda7 and /dev/sda8) into a single disk partition. To do this, you should first delete all those individual partitions, as shown below.

4. Create a New Disk Partition with Specific Size Using fdisk Command n

Once you’ve deleted all the existing partitions, you can create a new partition using all available space as shown below.

In the above example, fdisk n command is used to create new partition with the specific size. While creating a new partition, it expects following two inputs.

  • Starting cylinder number of the partition to be create (First cylinder).
  • Size of the partition (or) the last cylinder number (Last cylinder, +cylinders or +size ).

Please keep in mind that you should issue the fdisk write command (w) after any modifications.

After the partition is created, format it using the mkfs command as shown below.

5. View the Size of an existing Partition Using fdisk -s

As shown below, fdisk -s displays the size of the partition in blocks.

The above output corresponds to about 4900MB.

6. Toggle the Boot Flag of a Partition Using fdisk Command a

Fdisk command displays the boot flag of each partition. When you want to disable or enable the boot flag on the corresponding partition, do the following.

If you don’t know why are you are doing this, you’ll mess-up your system.

As seen above, the boot flag is disabled on the partition /dev/sda5.

7. Fix Partition Table Order Using fdisk Expert Command f

When you delete a logical partition, and recreate it again, you might see the “partition out of order” issue. i.e “Partition table entries are not in disk order” error message.

For example, when you delete three logical partitions (sda6, sda7 and sda8), and create a new partition, you might expect the new partition name to be sda6. But, the system might’ve created the new partition as sda7. This is because, after the partitions are deleted, sda9 partition has been moved as sda6 and the free space is moved to the end.

To fix this partition order issue, and assign sda6 to the newly created partition, execute the expert command f as shown below.

Once the partition table order is fixed, you’ll not get the “Partition table entries are not in disk order” error message anymore.

The fdisk command is a text-based utility for viewing and managing hard disk partitions on Linux. It’s one of the most
powerful tools you can use to manage partitions, but it’s confusing to new users.
This tutorial will go through the basics of using fdisk to manage a partition table. After using fdisk, you’ll have to use a mkfs
command to format new partitions with a �le system.

Sudo vs. Su
On Ubuntu, Linux Mint or other Ubuntu-derived distributions, the fdisk and mkfs commands must be pre�xed with sudo.
On distributions that don’t use sudo, use the su – command �rst to get a root shell, then type every command without sudo.

List Partitions
The sudo fdisk -l commands lists the partitions on your system.

You can add a disk’s device name to list only partitions on it. For example, use the following command to only list partitions
on the �rst disk device:
sudo fdisk -l /dev/sda

Entering Command Mode
To work on a disk’s partitions, you have to enter command mode. You’ll need the device name of a disk from the fdisk -l
command. The following command enters command mode for the �rst disk device:
sudo fdisk /dev/sda

How to Use Fdisk to Manage Partitions on Linux

Don’t edit partitions while they’re in use. If you want to edit system partitions, boot from a live CD �rst.

Using Command Mode
In command mode, you use single-letter commands to specify actions you want to take. Type m and press Enter to see a list
of the commands you can use.

Viewing the Partition Table
Use p to print the current partition table to the terminal from within command mode.

Deleting a Partition
Use the d command to delete a partition. You’ll be asked for the number of the partition you want to delete, which you can
get from the p command. For example, if I wanted to delete the partition at /dev/sda5, I’d type 5.

How to Use Fdisk to Manage Partitions on Linux

After deleting the partition, you can type p again to view the current partition table. The partition appears deleted, but fdisk
doesn’t write these changes to disk until you use the w command.

Creating a Partition
Use the n command to create a new partition. You can create a logical or primary partition (l for logical or p for primary). A
disk can only have four primary partitions.

Next, specify the sector of the disk you want the partition to start at. Press Enter to accept the default sector, which is the
�rst free sector on the disk.
Last, specify the last sector of the partition on the disk. If you want to use up all available space after the initial sector, just
press Enter. You can also specify a speci�c size, such as +5G for a �ve gigabyte partition or +512M for a 512 megabyte
partition. If you don’t specify a unit after the + sign, fdisk uses sectors as the unit. For example, +10000 results in the end of
the partition being 10000 sectors after its beginning.

System ID
The n command I just ran recreated the swap partition I deleted earlier — or did it? If I run the p command again, I’ll see that
the new /dev/sda5 partition is a “Linux” partition instead of a “Linux swap” partition.
If I want to change its type, I can use the t command and specify the partition’s number.
I’ll be asked for the hex code of the type. I don’t know it, so I can type L to view a list of hex codes.

How to Use Fdisk to Manage Partitions on Linux

It says 82 is the code for Linux swap partitions, so I can type that.

This doesn’t format the partition with the �le system you select. You’ll have to do that later with the appropriate mkfs
command.

Writing Changes
Use w to write the changes you’ve made to disk.
Use q if you want to quit without saving changes.

Formatting a Partition
You must format new partitions with a �le system before you can use them. You can do this with the appropriate mkfs
command. For example, this command formats the �fth partition on the �rst disk with the ext4 �le system.
sudo mkfs.ext4 /dev/sda5

Use the mkswap command if you want to format a partition as a swap partition:

How to Use Fdisk to Manage Partitions on Linux

sudo mkswap /dev/sda5

Fdisk contains a variety of other commands, including expert commands you can access by running the x command �rst.
Check out fdisk’s man page with the man fdisk command for more detailed information.

JOIN THE DISCUSSION

Chris Ho�man is a technology writer and all-around computer geek. He’s as at
home using the Linux terminal as he is digging into the Windows registry.
Connect with him on Google+.

Linux users create partitions to organize their data in an efficient manner. As easy as they are created, Linux partitions can be deleted to reformat a storage device and regain storage space.

In this step-by-step tutorial, you will learn how to delete a partition on a Linux system.

How to use fdisk to manage partitions on linux

  • A system running Linux
  • A user account with sudo or root privileges
  • Access to a terminal window / command line (Activities > Search > Terminal)
  • The fdisk command-line utility

Delete a Partition in Linux

Deleting a partition in Linux requires selecting the disk containing the partition and using the fdisk command-line utility to delete it.

Follow the steps outlined in the sections below to delete partitions in Linux.

Note: The fdisk command-line utility is a text-based partition table manipulator. It is used for partitioning and repartitioning storage devices.

Step 1: List Partition Scheme

Before deleting a partition, run the following command to list the partition scheme.

In our case, the terminal prints out information about two disks: /dev/sda and /dev/sdb . Disk /dev/sda contains the operating system, therefore its partitions should not be deleted.

How to use fdisk to manage partitions on linux

On the /dev/sdb disk, there is a /dev/sdb1 partition which we are about to delete.

Note: The number 1 in /dev/sdb1 indicates the partition number. Make a note of the number of the partition you intend to delete.

Step 2: Select the Disk

Select the disk that contains the partition you intend to delete.

Common disk names on Linux include:

Step 3: Delete Partitions

Before deleting a partition, back up your data. All data is automatically deleted when a partition is deleted.

To delete partition, run the d command in the fdisk command-line utility.

The partition is automatically selected if there are no other partitions on the disk. If the disk contains multiple partitions, select a partition by typing its number.

The terminal prints out a message confirming that the partition is deleted.

Note: If you wish to delete multiple partitions, repeat this step as many times as necessary.

Step 4: Verify Partition Deletion

Reload the partition table to verify that the partition has been deleted. To do so, run the p command.

The terminal prints out the partition structure of the disk selected in Step 2.

Step 5: Save Changes and Quit

Run the w command to write and save changes made to the disk.

After following the instructions in this tutorial, you should have learned how to delete a partition in Linux.

If you want to learn more about partition manipulation, we recommend reading our tutorial on creating partitions in Linux or if you are running NTFS partitions, check out our article on how to mount NTFS partitions in Linux.

Partitioning a disk allows you to create separate sections within the disk inorder to manage your data more efficiently. For example, in Windows you have C, D or E drives. There are various uses of partitioning a disk like:
a) Allows to boot different OS from same disk
b) Allocate separate partition to different users
c) Creating separate area for OS virtual memory swapping
In this post we will cover how to create disk partitions in Linux using the fdisk utility.

How to use fdisk to manage partitions on linux

Prerequisite

  1. root user access
  2. free disk

The process to create disk partitions

Step1: List the disks

Use the lsblk command to list the exiting disks along with partitions. An alternate command is fdisk -l.

How to use fdisk to manage partitions on linux

The output shows that two disks sdb and sdc are both un-partitioned whereas sda is partitioned into sda1 and sda2. Think of sda disk as your hard disk which is partitioned into C(sda1) and D(sda2) drive. All the devices are listed in /dev directory. So the path of these disks are /dev/sda, /dev/sdb, /dev/sdc and so on.

How to use fdisk to manage partitions on linux

Note: If you do not have a free disk like /dev/sdb or /dev/sdc, add a new virtual disk from your Virtual Box or VMWare software. As an alternate, you can run the below commands to add a disk temporarily.

This will add a disk /dev/loop1 whose size is 5Gb. Keep in mind that on the next reboot, the disk and the created partition(s) will be lost.

Step2: Specify the disk to partition

As the root user run the fdisk command by specifying the disk name as an argument. I will use the /dev/sdc disk for partitioning.

How to use fdisk to manage partitions on linux

Step3: Request a new partition

Press ‘n‘ to request for a new partition. On pressing “enter” key, the system will ask for partition type, either primary or extended. Press ‘p‘ for primary and ‘e‘ for extended partition. The default is primary. maximum 4 primary partitions can be created. If you intend to create more then create 3 primary partitions and 1 extended partition. Then this extended partition can be used as a container to create more logical partitions can be created.

Step4: Specify partition number, first sector and last sector

Next, assign a partition number. Then specify the size of partition. For size you need to tell two things: first sector and last sector. The best way is to choose the default value of First sector (manual specification may result in wastage of sectors if you are not aware about the details). Simple press “enter” key to choose the default starting sector. F theor Last sector you can either specify the sector or give the exact size. Its better that you specify the size in Mb or Gb as shown in the example below.

So, here I choose the size to be 1Gb and the partition is created.

Step5: Specify the partition type

The default partition type is “Linux” as shown in the previous figure. If you want to change it to something else then press the ‘t’ key. If you don’t know the type code then use the ‘L’ key to see a list of all the available types.

Step6: Save the Partition Table

Use the ‘w‘ command to save the partition table. It is must to do this otherwise the partition will not be created.

Step7: Initiate kernel re-read of partition table

Use the partprobe command to force a re-read of the partition table

Now, re-run the lsblk command and you will see the partition.

How to use fdisk to manage partitions on linux

Creating File systems

A partition can not be used until a file system is applied over it. There are different file systems but the most popular ones are xfs and ext4. mkfs command can be used for the same

How to use fdisk to manage partitions on linux

Mounting file system

Finally, to access the partition we need to attach it to the directory structure. So, first create a mount point, which is nothing but a directory using the mkdir command.

There are two ways to mount: manually or persistently

Manually mounting the file system

Use the below command to manually mount the device or partition at the mount point

and start creating or accessing files as shown below.

However, on the next reboot, the file system will not be automatically mounted. For a persistent mount as an admin you need to make the entry in the /etc/fstab file

Persistently mounting the file system

To permanently mount a device at a mount point add an entry for that device in the /etc/fstab file. /etc/fstab is a white space-delimited file with six fields.

The first filed is the device name, second is the mount point, third is the file system type, fourth field applies default mount settings. The last two fields are the dump flag and the fsck order.

Caution:

A wrong entry in /etc/fstab file my render the system unbootable. Hence, use the

command. If the command returns no error this means everything is perfect.

fdisk command is a free and open source utility widely available on Linux Based Platforms to Manage Disk Partitions Table. It understand different partitions table like BSD, GPT, MBR, SUN and SGI. fdisk command can be used to create a new partition, delete a partition, change the partition ID, resize the partition, move a partition and so on.

One can create a maximum of four primary partition using fdisk command and n number of logical partitions based on available disk size. Partition number from 1 to 4 are reserved for Primary partitions and then logical partition starts from number 5. fdisk command creates partition of minimum 40 MB size and maximum of 2 TB size.

What is Block Device

A Block device in Linux is defined as a file which represents devices like Hard Disk, CD-ROMs from where data can be read in fixed size blocks.

What is Partition

A Block device are usually divided into one or more logical disks which are known as partitions. The information of these partitions are stored in a table Known as Partition Table.

Synopsis

fdisk [-uc] [-b sectorsize] [-C cyls] [-H heads] [-S sects] device

fdisk -l [-u] [device. ]

fdisk -s partition.

fdisk -v

fdisk -h

How to use fdisk to manage partitions on linux

Fdisk Command Examples to Manage Disk Partitions in Linux

Example 1: How to Check fdisk Command Version

To check fdisk command version you need to use -v option as shown below. Here current fdisk version is 2.23.2 .

Example 2: How to List All Disk Partitions in Linux

If you want to List all the disk partitions then you need to use -l option with fdisk command as shown below. Here we have two disks /dev/sda and /dev/sdb but partitions are created only on /dev/sda disk. We have two partitions /dev/sda1 and /dev/sda2 available on /dev/sda disk.

-l : List the partition tables for the specified devices and then exit. More on fdisk Man Page.

Example 3: How to Check total number of partitions in a Disk

If you want to check all the partitions of a Specific disk then you can also use -l option along with the disk name. In this example we are checking all the partition information of /dev/sdc disk using fdisk -l /dev/sdc command as shown below.

Example 4: How to Delete a Disk Partition in Linux

To delete a disk partition you need to use command d as shown below. In this example we are deleting partition /dev/sdc1 using d command and then writing the partition table changes using w command.

Example 5: How to Create a Disk Partition in Linux

To create a disk partition you need to use n command as shown below. In this example we are creating partition on /dev/sdc disk using fdisk /dev/sdc command. Then selecting n command to create a new partition and selecting p to create it as a primary partition type. Here we are using default sector value i.e first sector value as 2048 and last sector value as 16777215 which will create partition on entire 8 GB disk and then writing the changes to partition table using w command.

Example 6: How to Check the Size of Disk Partition in Linux

To check the size of disk partition you can use -s option with fdisk command as shown below. In this example we are checking the size of /dev/sdc1 partition using fdisk -s /dev/sdc1 command. Here you will see size in number of blocks. So the total size of /dev/sdc1 partition is 8387584 blocks.

-s : Print the size (in blocks) of each given partition. More on fdisk Man Page.

Example 7: How to Verify Disk Partition table in Linux

Another useful command that you can use is v command to verify the disk partition table in Linux. In this example we are verifying /dev/sdc disk partition using v command and then writing the changes to partition table using w command.

Example 8: How to Quit fdisk Command without saving any changes

To quit fdisk command utility without saving any changes you need to use q command as shown below. In this example we have exited the /dev/sdc disk changes without saving it by using q command.

Example 9: How to Save and Exit fdisk Utility

If you want to exit after saving partition table information then you need to use w command as shown below. This command will save all the partition table changes and then exit out from fdisk command.

Example 10: How to Change Disk Partition ID to “Linux LVM” type

If you want to change disk partition ID to Linux LVM type then you need to use hex code 8e after selecting the partition through t command as shown below. In this example we are changing the partition Id of /dev/sdc1 partition from 83 to 8e to convert the partition from normal disk to LVM type.

Example 11: How to Change Disk Partition ID to “Linux Swap” Type

For creating a swap partition you need to change the partition type to Linux swap type by using hex code 82 as shown below. In this example we are changing partition ID of /dev/sdc1 from normal partition to Linux swap type.

Example 12: How to Disable/Enable Boot Flag(*) of a Partition

If you want you can also disable/enable boot flag of a partition using fdisk command in Linux. As you can see from below output, partition /dev/sda1 is currently marked as boot partition.

To disable the boot flag from /dev/sda1 partition you need to use command a under fdisk utility and then select the partition to disable the boot flag as shown below.

After writing the disk partition table changes if you check the /dev/sda1 partition status again then you can notice that star(*) marked is removed now. This confirms boot flag is now disabled. You can follow the same procedure to enable it again.

Example 13: How to Correct the Partition Table Order

If you want to correct the partition table order then you need to use the extra functionality available by using x command and then use f command to correct the partition table order. Since in this example our disk partition table is already in order so it won’t do any changes here.

Example 14: How to Disable/Enable DOS Compatibility Mode

If you want to disable/enable DOS compatibility mode from a disk partition then you need to use c command as shown below. In this example we are changing the DOS Compatibility mode of partition /dev/sdc1 using c command.

Example 15: How to Check all the available options of fdisk command

To check all the available options of fdisk command you need to use m command as shown below. This command will list all the available actions that can be done on disk partition using fdisk command.

Example 16: How to check Man Page of fdisk command

Man Page of fdisk command can be checked using man fdisk command as shown below.

Sometimes we stuck in a situation where we just have access to the terminal and need to delete partition, or you have stepped down in Linux, and your logical mind pushes you to delete the partition using a command.

But you don’t know how to delete the partition using the terminal, not a big deal, and I’m here to rescue you just follow the instruction, and your motive will be accomplished.

In this article, we will use fdisk command-line based utility to delete the partition.

Table of Contents

Prerequisites

  • Privilege account (sudo users)
  • Terminal
  • fdisk (Already preinstalled in Linux)
  • A partition should exist (If you think you can delete unallocated partition, then I suppose you might be wrong)

Delete a Partition in Linux using fdisk

To Delete partition first, we need to list out the available partitions and select the appropriate disk. For that, we can use the fdisk command to list out and delete the selected partition.

If you don’t know what is fdisk read these short descriptions and move ahead to delete partitions.

In short fdisk is a command-line utility tool to manage disk it allow you to view, create, delete, change, resize, copy and move partitions on a hard drive.

Note:- This below step will format your USB drive, so it requests you to make a backup of your USB drive else you may lose your valuable data.

How to use fdisk to manage partitions on linux

Step 3: Delete Partitions using fdisk

We are one step back to delete partition, from the above measure we confirmed we had selected correct Disk.

In this step we will delete partition If you have important data, please make sure to take a backup and proceed next.

To delete partition type d or del command in the fdisk command-line utility followed by partition number in case of multiple partitions.

In my case, multiple partitions are available to /dev/sda because of that, and I have to pass partition number manually.

If you are thinking How to get the partition number, check the above output of p command and note it down.

How to use fdisk to manage partitions on linux

Step 4: Verify changes

From the above command, we are able to delete selected partitions, To make sure whether the partition is deleted pass the p command in the fdisk command-line utility.

How to use fdisk to manage partitions on linux

We can see the change earlier total of 10 partitions was available in /dev/sda now it’s 9, hold on our work is not completed one last command is required to type.

Step 5: Save and exit

This is the last step to delete partition, after that you will not be able to revert changes.

Inside the same terminal type w command to write changes once the changes apply you will get the exit from fdisk utility tool.

This is a demo of the above explanation you can a look at the below output screen.

How to use fdisk to manage partitions on linux

Wrap-up

That’s all my friend to delete the partition using the fdisk command-line utility. In case you need some assistance from my end, feel free to comment Down.

If you want to share some feedback or any topic feel free to comment down.

A man with a tech effusive, who has explored some of the amazing technology stuff and exploring more, While moving towards, I had a chance to work on Android Development, Linux, AWS, DevOps with several Open source tools.
One of my life mottos “Always be lifelong Students.”

The fdisk command utility is useful for viewing and editing hard drive or SSD partitions on Linux and its distributions such as Ubuntu. It is one of the most powerful tools for managing hard drive partitions on Linux. There are other tools available to do hard drive partition on Linux, but fdisk is one of the most commonly used.

fdisk is a menu-driven command line utility, and new users will find this tool confusing and difficult to use. fdisk is not a simple tool and should be used with caution. You need root access or sudo permissions to make changes to storage partitions.

Today in this article I am giving you a tutorial on how to create a partition on Linux using the fdisk command.

You can perform the following operations with the fdisk command.

  • Create a new partition
  • Manage storage space
  • Relocate the space on old drives.
  • Move data to a new hard drive

How to List Partitions

Before making any changes to the drive, first look at the basic structure of the drive and its partitions. To view all of the partitions on the drive, run the following command in the Linux terminal.

Partitions are displayed by their device name, e.g. B. dev / sda, dev / sdc etc. as you can see in the screenshot above.

If you want to view the partition on the particular hard drive, you need to use ‘-l’ followed by the device name as shown in the following command.

$ sudo fdisk -l / developer / sda3

This command will display the output as shown in the screenshot below.

How to use fdisk to manage partitions on linux

Create partition:

To create a partition on a specific drive, you need to run the fdisk command with the device name as shown in the following command.

$ sudo fdisk / developer / Loop1

Once you are on the device you want to create a partition on, type M and hit Enter.

You will see the list as shown in the screenshot below. This is the list of single letter commands you can use to perform specific actions with fdisk.

How to use fdisk to manage partitions on linux

To create a partition, you must use the N command. Both logical and primary partitions can be created, where I will denote the logical partition and P denotes the primary partition.

By default, the first free sector on the hard drive is selected to create a new partition. You must also specify the last sector of the partition in the same way. You can also set the size of the partition according to your needs.

How to use fdisk to manage partitions on linux

Delete partition:

In order to delete the specific partition, you need to use the D command. Use the P command to make sure which partition you want to delete.

When you trigger the D command, it asks for the partition number you want to delete. Just type in that partition number and hit enter. This will delete the selected partition.

Here’s how you can use the fdisk command to create and delete the partition on Linux. fdisk is a very useful command line tool when used efficiently and correctly. Make sure you have the back of the partitions that you will be using the fdisk command on. Good day!

Fdisk is a partition table manipulator for Linux. Linux partitioning with fdisk is quick and simple. We can view, create, modify and delete partitions using fdisk from the Linux command line. In this post i will explain how to manage Linux partitioning with fdisk. I will view existing partitions on system, add a new hard disk, create new partitions, modify partitions system ID and delete partitions.

How to use fdisk to manage partitions on linux

Linux partitioning

1. Viewing existing partitions with fdisk

To list the existing partitions on your hard disk we use “fdisk -l” command. “Fdisk -l” lists the partition tables for the specified devices and then exit. If no devices are given, those mentioned in /proc/partitions (if that exists) are used.

Let’s see an example:

We can see, that this system has one disk /dev/sda the size of 16.1 GB. There are two partitions created on this disk:

  • /dev/sda1

This is the first partition which is a bootable partition (this is the * at “Boot” column). This partition starts at cylinder 1 and ends at cylinder 64. It takes 512000 Blocks on disk and has the ID 83 which means it is a Linux partition (seen in the “System” column).

  • /dev/sda2

This is the second partition and it starts at 64 cylinder and ends at 1959. It takes 15215616 Blocks on disk and has the ID 8e which means it is a Linux LVM partition.

We see the error “Partition 1 does not end on cylinder boundary.” This is not a big deal. Drives have not used cylinder boundaries since hard drives exceeded 8GB and BIOS uses LBA or large block allocation. But all the partition tools thru Linux & Win installs still followed cylinders so partition tools never changed the error messages.

We also see two more disks called /dev/mapper/vg_foo-LogVol01 and /dev/mapper/vg_foo-LogVol00 but these disks are actually mapped and located on /dev/sda disk (mind the “mapper” in the name). This is an additional layer on top of our /dev/sda disk. We can create this kind of mapped disk using LVM (Logical Volume Management). Since this is not the topic of this post we won’t lose time on this… I am preparing a separate post on Logical Volume Management on CentOS.

We will now add another hard disk to our system. Let’s check “fdisk -l” output again:

We can see that there is a new /dev/sdb hard disk the size of 1073MB without any partitions on it. In order to use the disk and create a filesystem we need to create a partition on it.

2. Creating a new partition with fdisk

To start linux partitioning for a desired hard drive we must enter a command “fdisk /dev/harddrive“.

Here is an example:

I think help is self explanatory. Since we want to create a new partition, we need to enter “n”.

We will be creating a primary partition this is why we entered “p”. One hard drive can only hold 4 primary partitions – to increase the number of partitions over 4, the 4th partition should be made an extended partition. If we continue…

Partition number is the /dev/sdbX number. Since this is the first partition on this hard drive, the partition number is 1 (/dev/sdb1). The first partition will start at the first cylinder (just press “Enter” and first cylinder is the default value) and the last cylinder may be entered as the number of cylinder or the size of partition. We have entered “+500M” so our partition will be 500MB large (mind the “+” in front of the partition size!!). You can also enter the size in Kilobytes (K) and Gigabytes (G).

No changes have yet been done to the disk and partitions until we write changes out. We can see what we did with the “p” key:

We can see a new partition on /dev/sdb hard drive. Now i want to create another partition on the same disk but with different partition system ID. Let’s try and do that:

OK. So we can now see two new partitions on /dev/sdb. We can see the start and end cylinder and the partition system ID. For cylinders (size) we used the default values by entering “Enter“. Both partitions ID is 83 which means it is a Linux partition. Let’s change the partitions system ID:

As we can see, we must enter “t” for partitionss system ID change. Next we must enter the number of the partition which ID we want to change – in our case this is partition number 2 (/dev/sdb2). Next we list the codes of partition’s system ID with “L” and choose the one we like. We have changed our /dev/sdb2 system ID to “Linux swap“. Let’s check if we successfully changed the ID:

We are now satisfied with the partitioning of the /dev/sdb hard drive and can write changes out by entering “w”. DO NOT FORGET to write changes out. If you hit “q” to quit, all changes are lost!

If we check for partitions, we see that our changes have been written out and our partitions exist. To start using them you still need to create a filesystem. This is not the topic of this post – i will try to cover it in a post about filesystem creationg on Linux.

3. Delete partition with fdisk

To start linux partitioning for a desired hard drive we must enter a command “fdisk /dev/harddrive“.

We enter “p” to print out the existing partitions on /dev/sdb hard drive. We can hit “m” for a quick list of fdisk help:

Again, help is self explanatory. To delete a partition we need to enter “d”.

After entering “d” we need to choose the partition number we want to delete. We need to delete partitions in the order they were created! We chose to delete partition number 2 (/dev/sdb2). We can now check partitions again:

We can see, that partition 2 (/dev/sdb2) no longer exists – it was deleted. Now we can delete the first partition too, if we choose to..

After making changes to with fdisk linux partitioning we MUST ALWAYS CONFIRM changes with “w” – write out. DO NOT FORGET TO WRITE OUT!

Last check to see, if changes were successfully applied with “fdisk -l“:

This was a quick guide on how to use fdisk for linux partitioning on CentOS 6. You can do alot more with fdisk regarding the partitions. Fdisk help can be very useful so do not hesitate to read it!

..and do not forget to always write your changes to partition changes with “w”!

The first thing that we need to know is there is no C drive or E drive in Linux. We will see something like /dev/sda, /dev/sdb, /dev/sdc, … etc. instead. The dev is short for device. The sd was short for Small Computer System Interface (SCSI) mass-storage driver.

  • /dev/sda – The first SCSI disk SCSI ID address-wise.
  • /dev/sdb – The second SCSI disk address-wise and so on.
  • /dev/sda1 – The first partition of disk sda
  • /dev/sdb1- The first partition of disk sdb

Understanding Disk Partition Tables

A partition table shows the partitions of a Hard Drive or any other storage device. There are two standards for the partition table:

  • Master Boot Record (MBR) – MBR also know as MS-DOS, is what us might call the original standard. MBR is still the most widely used partition table, it comes with two major limitations.
  • GUID Partition Table (GPT) – GPT came later. While MBR is still in use, the limitations are what led to the development of GPT. GPT overcomes the two limitations of MBR. We can have multiple primary partitions, and the drive sizes can exceed 2 TB.

The limitation of MBR:

  1. It does not allow us to create more than four main partitions. Those partitions are called primary partitions.
  2. Disk partitions may not exceed 2 TB.

Understanding Disk Partitions and Partition Numbering

To install an operating system on a hard drive, we have to divide it into distinct storage units. We call those storage units partitions. Under MBR – which is the default on virtually all Linux distributions, there are three different types of partitions – Primary, Extended, and Logical.

  • Primary – Holds the operating system files. Only four primary partitions can be created.
  • Extended – Special type of partition in which more than the four primary partitions can be created.
  • Logical – Partition that has been created inside of an extended partition.

With MBR any partition that is not explicitly created as an extended or logical partition, is a primary partition.

Any unallocated space is shown as Free. While it may be free, we cannot use it in that state. As far as the system is concerned that free space does not exist until it is partitioned.

Names without a trailing digit refer to the whole disk, while names with a trailing digit refer to a partition of that whole disk. By convention, SCSI disks have a maximum of 16 minor numbers mapped to a single disk.

Thus, for each whole disk, there is a maximum of 15 partitions per disk because one minor number is used to describe the entire disk (for example /dev/sda), and the other 15 minor numbers are used to refer to partitions for that disk (for example /dev/sda1, /dev/sda2, etc).

Understanding Disk Extended Partition

By tagging a partition as an extended partition, it is possible to create many more partitions under the extended partition. Those partitions are called logical partitions, and there is effectively no limit to the number of logical partitions that us can create. This means us can have three primary partitions and one extended partition and then create numerous partitions from there.

Check disk Partition with fdisk command in Linux

The best way to check disk partition in Linux is using fdisk command. It is a text-based utility for viewing and manipulating disk partitions. Typing fdisk -l at the command prompt will list all of the partitions on your Linux system.

You can also use the fdisk command to create, delete, or resize partitions. For more information on using fdisk, consult the man page by typing: man fdisk at the command prompt.

parted -l and fdisk -l can both list disk partitions on Linux.

How to use fdisk to manage partitions on linux How to use fdisk to manage partitions on linuxHow to use fdisk to manage partitions on linuxHow to use fdisk to manage partitions on linux

  • Model – Model of the storage device.
  • Disk – Name and size of the disk.
  • Sector size – Logical and physical size of the memory. Not to be confused with available disk space.
  • Partition Table – Partition table type (msdos, gpt, aix, amiga, bsd, dvh, mac, pc98, sun, and loop).
  • Disk Flags – Partitions with information on size, type, file system, and flags.

Check disk Partitions with ls command in Linux

Here we can see that there are 3 partitions on disk sda.

  • # ls -l /dev/sda*
  • brw-rw—- 1 root disk 8, 0 May 24 08:09 /dev/sda
  • brw-rw—- 1 root disk 8, 1 May 24 08:09 /dev/sda1
  • brw-rw—- 1 root disk 8, 10 May 24 08:09 /dev/sda2
  • brw-rw—- 1 root disk 8, 11 May 24 08:09 /dev/sda3

Check disk partitions with cat /proc/partitions in Linux

/proc/partitions file also has much important info about disk and disk Partitions.

  • major minor # blocks name
  • 8 0 17774160 sda
  • 8 1 1052226 sda1
  • 8 10 208845 sda2
  • 8 11 10490445 sda3

What is the difference between a partition and a filesystem?

A partition is a section of a disk that is allocated for use by an operating system. A filesystem is a method for organizing and storing files on a partition.

What is the purpose of partitions?

Partitions are used to separate different types of data. For example, you might have a partition for your operating system and another partition for your personal files. Partitions can help to improve performance and stability by isolating different types of files. For example, you can use a separate partition for your operating system files and another partition for your personal data files. This way, if one partition becomes corrupted, the other partition will not be affected.

What is the difference between primary and extended partitions?

Primary partitions are the first four partitions on a disk. Extended partitions are any partitions after the fourth partition. You can create up to four primary partitions or three primary partitions and one extended partition.

How do I delete a partition in Linux?

You can use the fdisk command to delete partitions in Linux. First, you need to find the device name of your disk. You can do this by running the lsblk command. Next, you need to run the fdisk command with the device name as an argument. Then, you will need to enter d to delete a partition. Finally, you will need to enter w to write the changes to the disk.

1. View All Disk Partitions:The first thing to do before doing any thing with the disks and partition is to view basic details about all available partition in the system. The below command is used to list the partitions on your system and see their /dev names. For example /dev/sda, /dev/sdb or /dev/sdc.

How to use fdisk to manage partitions on linux

2. View Partition on a Specific Disk: Below command is used to view all disk partitions on device /dev/sda.

How to use fdisk to manage partitions on linux

3. View all fdisk Commands: To see all the command which are available under fdisk command you can use /dev/sda partition with fdisk command.

How to use fdisk to manage partitions on linux

Note: This will prompt for a command. Type m for seeing all the operations which can perform on /dev/sda. After pressing m you will get:

How to use fdisk to manage partitions on linux

4. Create a Hard Disk Partition: For this go inside the hard drive partition that is the /dev/sda partition, and use the following command:

Now you have to type n to create new partition and then type p for making a primary partition and e for making an extended or logical partition depending on the type of partition to make.

How to use fdisk to manage partitions on linux

Run w command to write the changes and reboot your system.

5. Delete a Hard Disk Partition: To delete a partition for the hard disk and free up space occupied by that partition for example /dev/sdb. Go to the command menu using following:

and then type d to go to the delete partition menu. It will prompt the partition number you want to delete(type the number).

How to use fdisk to manage partitions on linux

Run w command to write the changes and reboot the system.

fdisk یک ابزار خط فرمان است که بهترین ابزار برای پارتیشن بندی یک هارد به حساب می آید.وقتیکه یک توزیع لینوکسی را توسط واسط گرافیکی نصب میکنید در اصل با آن واسط کاربری دارید از fdisk استفاده میکنید فرض کنید یک هارد جدید به سیستم اضافه کرده اید با اجرای دستور زیر میتوانید فهرست تمامی پارتیشن های هارد اول را مشاهده کنید

برای توضیحات تکمیلی در ارتباط با نام گذاری دیسک ها پیشنهاد میکنم مطلب آشنایی با نام گذاری دیسک ها در لینوکس را مطالعه کنید.
برای شروع به پارتیشن بندی ابتدا دستور fdisk را به همراه نام هارد دیسک جدید بصورت زیر اجرا کنید:

وقتی دستور فوق را اجرا کنید یک اعلان مانند زیر نشان داده میشود که بیان میکند برای نمایش help باید کلید M را وارد کنید

کلید M را بزنید تا فهرستی از تمام دستورهای fdisk را مشاهده نمایید. در زیر مهمترین آنها فهرست شده اند:

با اجرای دستور fdisk -l جدول پارتیشن های موجود روی سیستم نشان داده میشود وقتی با دستور N یک پارتیشن جدید ایجاد میکنید و یا با دستور D یک پارتیشن را حذف میکنید تا زمانیکه W را اجرا نکنید هیچ تغییری ذخیره نخواهد شد و برای خروج بدون ذخیره تغییرات باید Q را بزنید. هر نوع سیستم فایل مانند ext3 یک عدد منحصر به فرد را دارد بطور مثال پارتیشن نوع ext3 عدد ۸۳ و پارتیشن swap عدد ۸۲ را دارد. برای مشاهده این اعداد باید دستور L را اجرا کنید.

فرق گزینه L که بصورت fdik -l بکار رفته با دستور L ی که جلوی اعلان Command m for help استفاده کردید این است که L اولی برای نمایش جدول پارتیشن ها و L دومی برای نمایش اعداد معادل هر کدام از نوع سیستم های فایل ها استفاده شده است.

اگر دستور P برای چاپ کردن جدول پارتیشن ها را اجرا کنید خروجیش همانند زیر خواهد بود

یعنی که هیچ پارتیشنی وجود ندارد.

ایجاد پارتیشن

برای ایجاد یک پارتیشن جدید باید ۶ گام را دنبال کنیم.

گام اول

جلوی اعلان Command (m for help) دستور o را بزنید تا جدول پارتیشن ساخته شود

گام دوم

حالا دستور n را بزنید تا اعلان command action ظاهر شود.

گام سوم

از بین گزینه های e و p باید p را نوشته و Enter بزنید.

گام چهارم

عدد ۱ را برای ایجاد شدن اولین پارتیشن primary انتخاب کنید.

گام پنجم

باید شروع sector را انتخاب کنید در داخل پرانتز عددی – default است که میتوانید همین عدد را وارد کنید.

گام ششم

باید انتهای سکتور را انتخاب کنید در داخل پرانتز عددی – default است که میتوانید همین عدد را انتخاب کنید یا اینکه سایز دلخواهتان را بر حسب K یا مگابایت M انتخاب کنید( دقت کنید که حروف K و M می بایست بزرگ نوشته شوند) برای ساخت پارتیشن های دیگر مراحل دوم تا ششم را تکرار کنید برای ساخت پارتیشن extended در جلوی اعلان command action باید دستور e را وارد کنید و برای اعمال تغییرات روی هارد باید دستور w را وارد کنید. در این مرحله هارد شما آماده است و فقط باید پارتیشن های خود را فرمت کنید برای اینکار باید از دستور mkfs استفاده کنید در صفحه خودش به طور کامل آموزش لازم ارائه شده است

حذف یک پارتیشن

برای حذف یک پارتیشن جلوی اعلان Command (m for help) باید از دستور d استفاده کنیم.
وقتی جلوی اعلان دستور d را میزنید اعلان partition number ظاهر میشود که درون پرانتز تعداد پارتیشن ها را نشان میدهد اگر ۴ پارتیشن داشته باشیم تعداد ۴ پارتیشن وجود دارد و برای پاک کردن پارتیشن اول باید جلوی اعلان partition number عدد یک را بزنید و یا برای پاک کردن پارتیشن چهارم عدد ۴ را بزنید. دقت کنید اگر دستور w را وارد نکنید هیچ تغییری در جدول پارتیشن ها نوشته نمیشود.
اگر میخواهید پیش از پاک کردن پارتیشنی بدانید که کدام عدد متعلق به چه پارتیشنی است میتوانید جلوی اعلان Command (m for help) دستور p برای چاپ جدول پارتیشن استفاده کنید.

Creating partitions via the command line interface is simple and quick, also having the kernel acknowledge the partition table has been modified and having the partition scheme take effect without having to reboot your workstation/server and without having to use the parted utility.

The first step to this guide is to see what hard drives are available and what hard drive you would like to partition. If you have multiple hard drives in your machine, fdisk will report them to you as shown in Figure 1. There are two hard drives shown in Figure 1 “sda” and “sdb”.

The fdisk command with the -l qualifier (“fdisk -l“) will display the current partition tables along with the hard drives that are attached to your workstation/server as shown in Figure 1.

Figure 1: fdisk -l output.

Once you have determined which hard drive you want to partition you can issue the fdisk command followed by the hard drive (“fdisk /dev/sda“), in this article we will use the first hard drive (sda).

Figure 2: Partitioning the first hard drive (sda).

Once fdisk has been executed your command prompt will change to “Command (m for help):” and you are ready to examine and partition your hard drive. The commands that are supported by fdisk can be displayed by pressing the “m” character followed by the return key. The commands that we will be using are “p” for printing the partition table, “n” for creating a new partition and “w” for saving the changes and exiting.

Displaying the current partition table

The first step before we start to partition our hard drive is to get a sense of what disk space we have available and what the current partition table looks like. The command we will use from fdisk is the “p” character which will print the current partition table as show in Figure 3.

Figure 3: Current partition table.

The output shown in Figure 3 shows that we have 1809 cylinders available (9729 ? 7920 = 1809) and with this we can use the calculation (1809 * 16065 * 512 = 14879531520 Bytes) which is roughly about 14 gigabytes. (The 16065 and 512 were taken from the “Units =” statement and may differ on your system, so you may be require to substitute them if necessary).

Creating a new partition

The next step is to create our new partition. For our example, we will create a partition of type “Linux” with the partition size of one gigabyte. The command that we will use to create our new partition is the “n” character, we will then be asked to select what cylinder to start from (I recommend sticking with the default) and the size of the partition as shown in Figure 4.

Figure 4: Creating a new partition with the size of one gigabyte.

Once the partition has been created, using the “p” character we can display our new partition table which will show the newly created partition, as shown in Figure 5.

Figure 5: Newly created partition (sda6).

Writing the new partitions to disk

Now that the partition has been successfully created you can save the changes by issuing the “w” character which will write the new partition table to the hard disk as shown in Figure 6.

Activating the newly created partition

Once the new partition table has been written to the hard disk it is possible to have the kernel read the new partition table without the need of rebooting. The first step is to create a mount point for the new partition, in our example we will use “/media/newpart” and also use the command “partprobe” to have the kernel re-read the partition table, as shown in Figure 7.

Figure 7: Creation of a new mount point and a re-read of the partition table.

Now that a new mount point has been created and the partition table has been re-read by the kernel you can now format the partition and place a file system of your choice on it. (ext2, ext3, ResierFS etc) In our example we have chosen the ResierFS file system as shown in Figure 8.

Figure 8: Formatting the newly created partition.

Once the partition has been formatted with the file system of your choice you can mount and use your newly created partition, Figure 9 shows the newly created partition being mounted.

Figure 9: Mounting the newly created partition.

Final thoughts

Once you have mounted your newly created and formatted partition you can store your files on the partition without having any problems, you may also add your new partition to the “/etc/fstab” file so that the partition will be mounted after every reboot.

The latest version of this article, and future updates, will be located at the Computer Underground.

  1. /usr/src/linux/Documentation/ide.txt
  2. man fdisk
  3. man lilo
  4. man lilo.conf
  5. man sfdisk # for scripting fdisk
  6. man hdparm # side issue — you might find this useful for other things.

Introduction to Fdisk

The purpose of this article is to explain how we use fdisk manually to initialize IDE hard drives instead of using Disk Druid that comes with Red Hat 6.0. Why do you want to do this? Well, it can be one way of many ways to solve the problem where you need to have your bootable files for Linux before the 1024th cylinder. There are many ways to solve this problem, but for this article, we are only going to concentrate on redoing the number of heads and cylinders for your IDE hard drive. For further information about why you can do this, read the file “/usr/src/linux/Documentation/ide.txt” on your RedHat 6.0 computer (assuming you have the source code for the kernel installed).

Fdisk is a tool that allows you to change the partitioning of your hard disks. For example, you can make DOS, Linux, FreeBSD, and many other types of partitions.

Disk Druid is a tool, used by Red Hat 6.0, to set up partitions and to initialize hard drives when you install Red Hat 6.0. The problem is, I never found out how to use Disk Druid to initialize a second hard drive after RedHat 6.0 is installed.

There are also extra functions in fdisk. Some of them can be used to initialize your new hard drive for use with linux, which requires you to have 1024 or less cylinders. For most of the people these functions will never be needed, but let’s say that you have your computer with Linux on it and decide to add an extra hard disk. The easiest and fastest way to initialize it is to use fdisk.

Having 1024 cylinders or less is not an issue before you start using very large hard disks. The brief reason is, you can remap the heads and cylinders using LBA to make it so you reduce the number of cylinders and maximize the number of heads. In general up to 10G bytes there shouldn’t be any problem, but with the fairly low prices of large hard disks today, more and more people go past the 10G bytes size and this is when having 1024 or less sectors becomes an issue. Also, there are other simple ways to get around the 1024 cylinder problem without remapping your hard drive (like using a partition for /boot) but we won’t get into that here.

How to use Fdisk to initialize your new hard disk

There are two steps you need to do. First, find out the number of heads, cylinders, and sectors your hard drive has in the LBA setting in your BIOS. Then, use fdisk to you remap the hard drive.

First, turn off your computer, install the second hard drive, turn your computer on, and before it boots your operating system, get into your computers BIOS. Usually pressing the delete key, the F1 key, or perhaps some other key, gets you into the BIOS of your computer before it gets ready to start booting your operating system. It is recommended that you get professional help when your install another hard drive.

Then, in your BIOS you should be able to find the LBA settings for the hard drive. Sometimes if you have a feature in your BIOS which lets you auto-detect your IDE devices, it will let you know what the LBA settings for your hard drives should be. Get the LBA settings for the cylinders for your new hard drive. The LBA settings for the sectors and heads should be: heads=255, sectors=63.

Second, you want to start fdisk with one of the following commands after booting your Linux operating system: After you choose which command is appropriate, you will have the following prompt: Pressing ‘m’ will result in the following help menu: At this point you have to enter ‘x’, which will lead you to the extra functionality menu: Pressing ‘m’ again will show you the options in this menu: Notice that you have ‘c’ for changing the number of cylinders, and ‘h’ for changing the number of heads. These are the options we are going to use. The idea is to maximize the number of cylinders and minimize the number of heads. In order to do this, enter ‘c’ at the Expert menu and you will get the following prompt: At this prompt you should enter the number that the LBA shows for cylinders in the BIOS. After you enter the number of cylinders, you will be back at the expert menu. Press ‘h’ and this prompt will ask you for the number of heads: You can leave the number of heads at 64, since this is the lowest you can go. After you enter 64, you are back at the Expert prompt. At this point you need to save the changes by entering ‘w’. After that the fdisk will exit and you are done.

Actually, before typing “w”, I might enter “r” or return you to the main menu, and then enter “p” just to see what you have done. If the number of heads is 255 and the sectors are 63, and the number of cylinders is correct, then you are done and you can enter “w”.

How to use fdisk to manage partitions on linuxLes Chatfield (CC BY 2.0)

How much do you need to know about disks to successfully manage a Linux system? What commands do what? How do you make good decisions about partitioning? What kind of troubleshooting tools are available? What kind of problems might you run into? This article covers a lot of territory – from looking into the basics of a Linux file systems to sampling some very useful commands.

Disk technology

In the beginning days of Unix and later Linux, disks were physically large, but very small in terms of storage capacity. A 300 megabyte disk in the mid-90’s was the size of a shoebox. Today, you can get multi-terrabyte disks that are the size of a slice of toast.

Traditionally, files resided within file systems that resided in disk partitions that were themselves simply slices of disks. This organization still dominates today, though servers in large data centers often take on an entirely different structure.

This simplistic view still works for many systems, but these days there are lot of complexities that make disk management harder in some ways and easier in others. A file system might be virtual – no longer residing on a single disk and more complex to manage, but far easier to resize as needed. In fact, the entire system could be virtual. And what we might manage as if it were a single disk could actually be some portion of a very large disk array.

Disk management

Sysadmins generally have to deal with many issues when it comes to managing disks. These include:

  • Partitioning disks
  • Creating file systems
  • Mounting file systems
  • Sharing file systems
  • Monitoring free space within file systems
  • Backing up (and sometimes restoring) file systems

The reasons to partition a disk include:

  • protecting some file systems from running out of space (e.g., you may want the OS partition to be separated from home directories or applications to keep it from being affected if users’ files begin to take up far an excessive amount of disk space)
  • improving performance
  • allocating swap space
  • facilitating maintenance and backups (e.g., you might be able to unmount /apps if it’s not part of / and you might want to back up /home more frequently than /usr)
  • more efficient (and targeted) fsck
  • maintaining (particularly on test systems) multiple operating systems
  • reserving enough disk space for file system expansion
  • sharing select file systems with other systems

Partitioning commands

For most Linux servers, partitioning is done before the servers are deployed. On the other hand, you might add disks at some later time or hold back some significant amount of free disk space for future use.

To make changes or verify partitions, enter a command such as fdisk /dev/sda to start fdisk interactively and then type m to see a list of the things that you can do with the fdisk command.

As you can see, the fdisk command provides a lot of functionality. The partitions that you set up may look something like this configuration in which four partitions have been set up on a single disk – /dev/sda.

Examining disk space and disk partitions

There are a number of excellent commands for examining disk partitions. The df command is one of the most commonly used commands for reporting on disk space usage. With the -h option, the df command displays the measurements in the most “human-friendly” format and that is, in fact, what the “h” is meant to imply. As you can see in the example below, the measurements are displayed in kilobytes, megabytes or gigabytes depending on the sizes rather than all using the same scale.

The pydf command (think “python df” as it’s really a python script) also provides a very useful disk usage display showing mount points and cute little illustrations for how full each partition is.

The parted command displays partition information in a different format:

The lsblk (list block devices) command illustrates the relationship between disks and their partitions graphically and also supplies the major and minor device numbers and mount points.

The fdisk command reports more details on disk partitions and uses very different numbers. You can also use fdisk to create or delete partitions, list unpartitioned space, change a partition type, or verify the partition table.

The sfdisk command is similar to fdisk, but makes some partition manipulation activities easier to perform.

NOTE: A mebibyte (MiB) = 2 20 bytes or 1,048,576 bytes.

The cfdisk command can also be used to display or manipulate disk partitions.

Monitoring disk performance

The iostat command can display statistics that illustrate how disks are performing, including how heavily they are being used. It also displays important measurements that show how busy the CPU is and how much of its resources are used for types of work. The system described below is idle more then 95% of the time. More importantly for our focus on disks, the %iowait (CPU waiting on disk IO) is very low. This would not be true if the disk were unusually busy and disk IO were a bottleneck.

Probably one of the most informative commands for looking at disk health is smartctl (part of smartmontools). While the command generates a lot of output, it provides valuable measurements that might help you pinpoint disk problems, particularly once you get used to working with its extensive output.

In this article, we will look at how to extend/increase a standard partition online (no downtime) without losing data and we will use the root partition (/) as an example.

How To Increase / Extend The Root (/) Partition In Linux Using The fdisk Utility

NOTE 1: Take a backup of your system if you can. If it a VM on Azure or any other cloud services provider, take the snapshot of the OS disk

NOTE 2: The reason for the backup is to roll back if anything goes wrong. If your filesystem is healthy, it is very rare for an issue to occur. These steps are the steps I usually take and have used in a production environment before and was successful. So, no worries.

NOTE 3: When you use the “d” option to delete the partition and use the “n” option afterward, what you are doing is actually creating a partition table and remains in memory and not deleting the whole partition, so no worries.

Step By Step Process

1. verify the root (/) filesystem size

How to use fdisk to manage partitions on linux

2. verify the root(/) filesystem type

You can see that the root filesystem type is “xfs”

3. Initialize /dev/sda using the fdisk utility

enter the letter “p” to print all the partitions on sda

enter the letter “d”to delete a partition

enter the partition number 3 or press enter to leave it at default which is 3

NOW, ENTER THE LETTER “n” TO RECREATE THE PARTITION TO YOUR DESIRED SIZE

enter the letter “p” to make it a primary partition, yours might be secondary depending on the number of partitions you have. A disk can have only four partitions as primary partitions

enter the partition number which is 3 or press enter to leave at default which is 3

press the enter key again to get to the last sector

enter the new partition size or press the enter key to use the whole available space on sda. In this scenario, we are using the whole available space

enter “no” not to remove the signature

enter the letter “w” to write or save the changes and press enter

4. verify the partition increment

5. resize the filesystem,

If the filesystem is xfs, use the command,

If you get the error above, use the command,

If the filesystem is ext (2,3,4), use “resize2fs” instead

6. you may also run the command below to make immediate changes to the kernel

7. verify the filesystem new size

How to use fdisk to manage partitions on linux

You can see that the size has increased.

Tutorial Video On How To Extend Root Partition Using fdisk In Linux

Your feedback is welcomed. If you love others, you will share with others.

• Create a new partition using fdisk. Set the partition’s system id as “82” (Linux Swap / Solaris).

Note: You should be familiar with the Linux fdisk command to learn this lesson. Click the following link to learn how to use the Linux fdisk command.

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-783, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-783, default 783): +512M

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 82
Changed system type of partition 1 to 82 (Linux swap / Solaris)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

• Create the swap filesystem on the raw partition using “mkswap” command.

]# mkswap /dev/sdd1
Setting up swapspace version 1, size = 518156 kB

• Enable swaping on the swap partition created now.

• Update /etc/fstab to make use the new swap partition whenever the system is rebooted.

LABEL=/ / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
devpts /dev/pts devpts gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs defaults 0 0
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
LABEL=SWAP-sda3 swap swap defaults 0 0
# Beginning of the block added by the VMware software
.host:/ /mnt/hgfs vmhgfs defaults,ttl=5 0 0
# End of the block added by the VMware software
/dev/sdd1 swap swap defaults 0 0

Note: To disable swaping on a partition, use “swapoff” command.

Enable file based swaping

We can create file based swap space (similar to Windows pagefile), if there is not available partition, but space is available in already existing partitions.

• To enable file based swap space, you must create a file using “dd” command.

]# dd if=/dev/zero of=/root/swap bs=1024 count=524288
524288+0 records in
524288+0 records out
536870912 bytes (537 MB) copied, 4.98139 seconds, 108 MB/s

• Overlay a swap file system on the binary file created using the above command.

]# mkswap /root/swap
Setting up swapspace version 1, size = 536866 kB

• Enable swaping on the swap file created now by using “swapon” command.

• Update /etc/fstab to make sure that the swap file is available to the OS after a reboot.

LABEL=/ / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
devpts /dev/pts devpts gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs defaults 0 0
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
LABEL=SWAP-sda3 swap swap defaults 0 0
# Beginning of the block added by the VMware software
.host:/ /mnt/hgfs vmhgfs defaults,ttl=5 0 0
# End of the block added by the VMware software
/dev/sdd1 swap swap defaults 0 0
/root/swap swap swap defaults 0 0