How do I create partitions in Linux?
introduction
Management is an essential part of control and productivity. In the world of computers, the control factor depends on the operating system. An operating system allows the user to take control of the computer and Linux is a great example of this operating system. It helps in accomplishing various computing tasks by managing the hardware and software components of the computer.
Do you know that the entire management task requires sufficient space on the hard disk? So how does Linux manage this space allocation? The term “partitioning” supports data organization. So, Linux creates partitions to manage the computer.
In this article, we will discuss the important elements of partitioning in Linux. We will also discuss the importance of partitioning and how Linux creates partitions about the fdisk and parted commands. This informative article will help you learn how to format and mount partitions, which are crucial steps to make partitions usable. Get ready to enter the world of Linux disk partitioning!
Why is partitioning important for a Linux operating system?
Data organization is important, regardless of the type of organization. Linux organizes data for a computer through partitioning. Before we get into this How Linux partitionsLet’s look at the importance of this process.
Organize data:
Partitions help organize data by separating different file types and system areas. For example, you can have one partition for the operating system, another for user data, and another for system backups.
Manage storage space:
Partitioning a drive allows you to allocate specific amounts of space for different purposes. This helps prevent the operating system from taking up all the available space and helps keep your data organized.
System stability and recovery:
Storing the operating system on a separate partition from user data can improve system stability and facilitate data recovery in the event of a system failure or reinstallation.
Multi-boot systems:
The partition approach is extremely beneficial when there are multiple operating system requirements. Yes, a user can install multiple operating systems if Linux creates a partition.
Performance and safety:
Different partitions can improve performance by reducing fragmentation and increase security by isolating sensitive files.
System Restore:
Suppose there is a box with four partitions. Each partition has a different Spice strength. Now, water is accidentally spilled into one of the partitions. Will the other three partitions get wet? The answer is no, as there is a clear partition inside the box. In much the same way, a partition on a system stores the data and allows for quick recovery.
Do you know that Linux without implementing a disk partition can pose a risk to data? Yes, it can lead to data loss or very little flexibility in system organization. Therefore, it is extremely important to Create a partition in Linux.
Partition the hard drive using the fdisk command
The fdisk command is the best choice when a user sets up a new hard drive in Linux. Follow the steps below to run the command fdisk command to create a partition:.
Step 1: List available disks
Many systems have multiple hard drives and as a user, you don't want to confuse them, so the very first step is to list the available hard drives to identify the correct one.
sudo fdisk -l
Step 2: Start fdisk on the target disk
Once the correct hard drive has been identified, the next step is to run the fdisk command on it. To do this, you need to use the following command.
sudo fdisk /dev/sdb
In the above command, the term “/dev/sdb” is generic. Here you need to replace it with the correct volume identifier.
Step 3: View existing partitions
Creating partitions in Linux is a complex process if not done properly. It is always advisable to check the existing partitions first. This will provide insights about the available space and prevent important data from being lost. Overall, it is a great way to prepare for backup. You can view the existing partition by entering the following command:
P
Step 4: Create a new partition
Now, with all the information you just went through, you are ready to Partitioning a hard drive under LinuxTo do this, run the following command:
n
The creation process starts when you type the command “n”. Then you need to use the following command to select the partition type. The type usually indicates the role and usage of the partition. There are different types of partitions including primary, extended, logical, etc. Here we choose the primary partition.
p
Next, the newly created partition needs to be assigned a number. This is important to create a unique identity. The usual range of primary partitions is from 1 to 4, so in the command line interface we enter the number “1”.
1
The last step in partition creation is about the size. You need to enter a specific number in the CLI to create a specific partition size. For example, here you can type “+20G” in the CLI and once you hit enter, you will have a 20GB partition available.
Step 5: Write changes to disk
The above changes are temporary until you write them with the following command:
W
That's it; your partition in Linux is ready to use!
Partition the hard drive using the “Parting” command
The parted command is another tool for managing disk partitions in Linux and offers more advanced options than fdisk. Follow these steps to use the command:parted” to create a partition:
Step 1: List available disks
Just as we first identified the existing partitions in the fdisk part, we will do the same here. This will help us identify the right disk for the operation. You can do this with the following command:
sudo parted -l
Step 2: Run the Parted command on the target disk
Once you have identified the correct disk, run the Parted command for that disk. Replace /dev/sdb with your actual disk ID. This step is similar to fdisk.
sudo parted /dev/sdb
Step 3: View existing partitions
To view the existing partitions and available space, use the print command. This helps in understanding the current disk configuration.
print
Step 4: Create a new partition
Now it's time to create a new partition using the Parted command. See it below.
- Select partition type: Choose between primary or logical partitions. For this example, we will create a primary partition.
Mkpart
- Specify the partition type and file system: Enter the partition type (primary, extended, etc.) and file system type when prompted.
mkpart primary ext2 1MiB 20GiB
This will create a primary partition formatted with ext4 starting at 1 MiB and ending at 20 GiB.
Step 5: Review and apply changes
Once everything is done, you need to make sure there are no problems. Review the new partition configuration to make sure it meets your needs.
print
If everything looks good, the changes are saved automatically. You don't need to use a separate command like w in fdisk to write changes because parted applies changes immediately.
Format partition
Now that you created a partition in LinuxYour next action should be formatting. Why is this important? The formatting approach ensures the proper provision of a file system on the disk. This helps in organizing the data. To do this, you need to use the “mkfs” command.
Step 1: List existing partitions
While formatting, the user also needs to list the existing partitions. This helps in identifying the correct disk to format. To do this, use the following command.
sudo fdisk -l
Step 2: Choose the correct partition
From the list, identify the partition you want to format. For this example, let's assume the partition you want to format is /dev/sdb1.
Step 3: Format the partition
Use the mkfs (make file system) command to format the partition. You can choose different file system types according to your needs. Some common examples of file system types are ext4, xfs, ntfs, etc.
- For the ext4 file system: Command:
sudo mkfs.ext4 /dev/sdb1
- For the xfs file system: Command:
sudo mkfs.xfs /dev/sdb1
- For NTFS file system: Command:
sudo mkfs.ntfs /dev/sdb1
Step 4: Check the formatting
Once the formatting steps are complete, you need to make sure the process goes smoothly by entering one of the following commands.
sudo blkid
or
lsblk -f
The above steps will complete the formatting process in a simplified manner for the user.
Also read: How to format a hard drive in Linux/Ubuntu operating system?
Mount partition
Now we have created a new partition and saved the changes, but what's next? Can we use it? Well, there is one last important thing that needs to be done. You need to mount the partition, which will attach the partition to the directory. This will allow you to read and write the partition easily. Here is a step-by-step guide on how to mount a partition in Linux:
Step 1: List existing partitions
Before mounting, list the existing partitions to identify the partition you want to mount.
sudo fdisk -l
Step 2: Create a mount point
A mount point is a directory where the partition is attached to the file system. You must create this directory.
sudo mkdir /mnt/my_partition
Step 3: Mount the partition
Use the mount command to attach the partition to the mount point.
sudo mount /dev/sdb1 /mnt/my_partition
Step 4: Check the bracket
Check if the partition is properly mounted and accessible.
df -h
Step 5: Access the partition
Navigate to the mount point to access the files on the partition.
cd /mnt/my_partition
Step 6: Unmount partition
If you no longer need the partition, you can unmount it. This is required before disconnecting the hard drive or making any changes.
sudo umount /mnt/my_partition
Diploma
Linux is an amazing operating system that works with clear partitions. In this article, we have discussed the meaning and concept of Creating partitions under Linux. We shed light on how to partition a hard drive and how this leads to proper data organization. Here we talked about how to manage these partitions. Our informative post perfectly explained the implementation of important commands like “fdisk” and “parted” to create a new partition. We explained the process of partition formatting and mounting in detail. Remember, each step is important to make sure your data is organized. So next time you need to set up a new hard drive or reorganize an old one, remember these steps to make sure it is done right!