How to Create a Partition in Linux?

Topics Covered

How to Create Partition in Linux

Creating a linux partition is a fundamental task that allows you to organize and manage your storage space effectively. Whether you're a beginner or an experienced user, understanding the process of creating a partition is essential. In this article, we will explore how to create a partition in Linux using the command mode, including steps for creating a partition, setting up the partition type, finalizing the changes, and formatting the partition.

In this article, we'll build a linux disc partition using the fdisk command. On a Linux system, the fdisk utility is a text-based command-line tool for inspecting and managing disc partitions. It is necessary to list every linux partition on our system before adding a new one. This is crucial because we must select a disc before partitioning it.

We will use the following command to inspect every partition that is currently present on your system:

To confirm your sudo access, you might be asked to enter your password once more. Here, we used the -l option of the fdisk command to list the partitions. You should receive a result resembling the one below.

sudo fdisk output

fdisk list

We will now select one disc to partition from this list. We will pick the disc for this lesson. We employ the 'command mode' of the fdisk function to create partitions. We execute this command on our terminal to enter the command mode.

You have successfully entered the command mode if you see an output that resembles this.

fdisk list output

Partitioning a Disk

You can divide your hard drive into several autonomous pieces by creating linux disc partitions. Before using storage devices (USB and hard discs) in Linux, users must arrange them. When installing numerous operating systems on a single workstation, partitioning is also helpful.

The Command Mode

The command mode in Linux provides a powerful interface for executing commands directly. To create a partition, you will need to access the command mode, commonly known as the terminal. You can open the terminal by pressing Ctrl+Alt+T or by searching for "Terminal" in the applications menu.

Many beginners could become perplexed once we reach command mode owing to the strange interface. Fdisk's command mode allows users to express the system's desired action via single-character commands. Pressing m will display the list of commands that are now accessible, as seen below.

fdisk m output

Partition Creation

Here, making a partition is our major goal. We use the command n to make a new partition. You will then be prompted to choose the sort of linux partition you want to create.

Select l to create a logical partition. You can also select p for a primary partition as an alternative. We'll establish a primary partition for this lesson.

partition creation output

Creation of linux partition

Our new partition's starting sector will now need to be specified. To select the first free sector on your system that is available, press ENTER. You will then be asked to choose the final sector for the partition. You can either specify the size of your partition or simply hit ENTER to use all available space after the first sector.

linux partition

Sector type

For this demonstration, we decided to make a 10 MB partition, as seen in the screenshot above. The letter M here designates the megabyte as the unit. The letter G stands for gigabytes.

By default, if you do not specify a unit, sectors will be used. So if you enter +1024, it will refer to 1024 sectors from the initial sector.

Alternatively, we can follow the below step-wise procedure. So, once you have the terminal open, follow these steps to create a partition:

  • Step 1:
    Identify the Disk. Use the command lsblk to list all available disks on your system. Identify the disk on which you want to create the partition. It will be labelled as something like /dev/sda or /dev/nvme0n1.
  • Step 2:
    Launch the Partitioning Tool Next, launch a partitioning tool such as fdisk or parted to create the partition. For example, to use fdisk on /dev/sda, enter the command: sudo fdisk /dev/sda.
  • Step 3:
    Create a New Partition Within the partitioning tool, use the appropriate commands to create a new partition. For fdisk, type n and choose the partition type (primary or extended). Specify the partition size, or press Enter to use the default size (utilising the entire disk). Finally, save the changes.

Setting for the Type of the Partition

Partitioning storage devices in Linux is a common task in order to prepare them for use. This involves creating and deleting partitions, which are separate sections on large storage devices such as hard drives and USB drives. Partitioning enables you to divide your hard drive into isolated sections, each of which behaves like its own hard drive. This is especially helpful if you need to run multiple operating systems.

Linux sets the default partition type to Linux once we create a partition. But let's say we want my partition to be a Linux LVM partition. We'll use the command 't' to modify the partition's ID.

We are now prompted to provide the HEX code for the partition ID we want. The HEX codes for the various partition kinds are not immediately recallable.

To list all of the HEX codes for the available partition kinds, we will use the L command. This list should resemble the example below.

setting type of partition

Partition Type List

Based on our observation, the HEX code 8e corresponds to the partition ID for the Linux LVM partition type. Therefore, we will input the necessary HEX code. Upon execution, the output confirms that the partition ID has been modified successfully.

partition type list

Finishing the Changes

We must verify our changes now that we have established a new partition and assigned it the partition ID we wanted. All of the modifications made up to this point have been saved in the memory and are awaiting writing to our disc.

As seen in the screenshot below, we use the command 'p' to view our current disc's comprehensive list of partitions.

current disc list of partitions

Description of all the partitions

To ensure the changes made to the disk are accurate, you must confirm them before applying them permanently. Once confirmed, press w to write the new partition to your disk. If you do not want to apply the changes permanently, you can use the q command to exit the fdisk command mode without saving any changes.

So stepwise guide would be:

  • Step 1:
    Review the Changes Use the partitioning tool to review the changes you made to ensure they are accurate. Double-check the disk, partition size, and type.
  • Step 2: Write the Changes For fdisk, type w to write the changes to the disk. Confirm your decision, as this action is irreversible.

Formate the Created Partition

It is recommended to format your new partition after creating it using the proper mkfs command.

This is due to the possibility of future problems when using a new partition without formatting it. We type the following into our command line to get the whole set of mkfs commands that are available.

A list of the available mkfs commands appears as a result. This command is used to format a partition on our current disc with the ext4 file system.

You now understand how to use the fdisk command to create a partition in Linux. You can set aside space for particular projects. Additionally, you don't have to be concerned about the data on other partitions if one partition becomes corrupt.

Data on other partitions is safe since each partition is viewed as a distinct disc. Although the fdisk application is a potent tool for managing disc partitions, inexperienced users sometimes find it to be complicated.

Conclusion

To conclude, here are the key points to remember about creating a partition in Linux:

  • Creating a partition in Linux involves using the command mode or terminal, which provides a powerful interface for executing commands directly.
  • The first step is to identify the disk on which you want to create the linux partition using the lsblk command.
  • Launch a partitioning tool such as fdisk or parted to create the partition on the chosen disk.
  • Within the partitioning tool, use commands like n to create a new partition, specifying the partition type (primary or extended) and size.
  • After creating the partition, set up the partition type using commands like t to change the partition's system ID.
  • Review the changes you made in the partitioning tool and then finalize the modifications by writing the changes to the disk.
  • Once the partition is created, it needs to be formatted with a file system before it can be used.
  • Identify the partition you want to format and choose a file system such as ext4, XFS, or Btrfs.
  • Format the linux partition using a command like sudo mkfs.ext4 /dev/sda1, replacing ext4 with your chosen file system and /dev/sda1 with the appropriate partition.

By following these steps, you can successfully create a partition in Linux, allowing you to organize and manage your storage space effectively.