Gaming industry under DDoS attack. Get DDoS protection now. Start onboarding
  1. Home
  2. Developers
  3. How to Partition a Disk in Linux

How to Partition a Disk in Linux

  • By Gcore
  • October 29, 2023
  • 3 min read
How to Partition a Disk in Linux

Mastering disk management is a pivotal skill for anyone diving into the Linux ecosystem. Whether you’re setting up a new drive, carving out space for different file systems, or optimizing storage for virtual machines, understanding disk partitioning can be a lifesaver. In this guide, we’ll demystify the process and walk you through the steps to partition a disk in Linux, ensuring you have the foundational knowledge to handle your storage needs with confidence.

Key Reasons to Partition a Disk

Partitioning a disk in Linux offers several advantages. Here are some of the primary reasons why you might want to partition a disk:

  1. Organization: Creating distinct partitions allows you to segregate and systematically manage data, making it easier to locate and access files or directories based on their importance or frequency of use.
  2. Performance Optimization: Separate partitions, especially on hard disk drives, can lead to faster access times. Having a dedicated partition for specific tasks, like swapping or caching, can boost system performance.
  3. Multi-OS Booting: For those wanting to run multiple operating systems on a single machine, separate partitions are essential. Each operating system typically requires its own dedicated partition.
  4. Data Security and System Stability: By isolating the operating system on its own partition, any failure or corruption in other partitions won’t affect the system’s operation. This also makes system backups or reinstalls simpler without impacting personal data.
  5. Flexibility in File System Choices: Different tasks or applications might benefit from different file systems. With multiple partitions, you can allocate specific file systems (like ext4, NTFS, Btrfs) based on your particular needs for each partition.

In essence, partitioning a disk provides a blend of efficiency, flexibility, and safety, making it a recommended practice for both casual users and IT professionals.

Process to Partition a Disk in Linux

Here’s a step-by-step guide to partitioning a disk in Linux, including expected output and descriptions.

#1 Identify the Disk

Identify the disk you want to partition. In this example, we’ll be working with /dev/sda. This command lists all the disks and their partitions:

sudo fdisk -l

Sample Output:

#2 Start the Partitioning Tool

This command opens the fdisk utility for the specified disk (/dev/sda in this example).

sudo fdisk /dev/sda

Sample Output:

#3 Create a New Partition

Use this command to create a new partition (inside fdisk):

n

Sample Output:

Partition type:   p   primary (1 primary, 0 extended, 3 free)   e   extended (container for logical partitions)Select (default p): 

Choose p for primary or e for extended. Most users will choose p for a primary partition.

#4 Specify Partition Details

After selecting “p” this is the expected output:

Partition number (1-4, default 2): First sector (499712-488397134, default 499712):Last sector, +sectors or +size{K,M,G,T,P} (499712-488397134, default 488397134): 

Description:

  • Choose a partition number (usually default is fine).
  • Specify the starting sector (usually the default is fine).
  • Specify the size of the partition. You can use the +sizeG format to specify size in gigabytes (e.g., +20G for a 20GB partition).

#5 Save Changes

This command writes the changes, creating the new partition.

w

Sample Output:

The partition table has been altered.Calling ioctl() to re-read partition table.Syncing disks.

#6 Format the New Partition

To set up the new partition, execute the following command. Ensure you substitute ‘X‘ with the specific partition number you’ve established.

sudo mkfs.ext4 /dev/sdaX

Sample Output:

mke2fs 1.43.4 (31-Jan-2017)Creating filesystem with 5120000 4k blocks and 1280000 inodes...

This step formats the new partition with the ext4 filesystem, making it ready for use.

#7 Mount the New Partition (Optional)

You can now mount the partition to a directory to start using it. This mounts the newly created partition to the desired directory path.

sudo mount /dev/sdaX /desired/directory/path

Congratulations! Now you’re able to partition a disk in Linux. Remember, before making any changes to disk partitions, it’s crucial to back up important data. Partitioning can result in data loss if not done carefully. Always double-check your commands and actions!

Conclusion

Looking to deploy Linux in the cloud? With Gcore Cloud, you can choose from Basic VM, Virtual Instances, or VPS/VDS suitable for Linux:

Choose an instance

Related articles

What's the difference between multi-cloud and hybrid cloud?

Multi-cloud and hybrid cloud represent two distinct approaches to distributed computing architecture that build upon the foundation of cloud computing to help organizations improve their IT infrastructure.Multi-cloud environments involve us

What is multi-cloud? Strategy, benefits, and best practices

Multi-cloud is a cloud usage model where an organization utilizes public cloud services from two or more cloud service providers, often combining public, private, and hybrid clouds, as well as different service models, such as Infrastructur

What is cloud migration? Benefits, strategy, and best practices

Cloud migration is the process of transferring digital assets, such as data, applications, and IT resources, from on-premises data centers to cloud platforms, including public, private, hybrid, or multi-cloud environments. Organizations can

What is a private cloud? Benefits, use cases, and implementation

A private cloud is a cloud computing environment dedicated exclusively to a single organization, providing a single-tenant infrastructure that improves security, control, and customization compared to public clouds.Private cloud environment

What is a cloud GPU? Definition, types, and benefits

A cloud GPU is a remotely rented graphics processing unit hosted in a cloud provider's data center, accessible over the internet via APIs or virtual machines. These virtualized resources allow users to access powerful computing capabilities

What is cloud networking: benefits, components, and implementation strategies

Cloud networking is the use and management of network resources, including hardware and software, hosted on public or private cloud infrastructures rather than on-premises equipment. Over 90% of enterprises are expected to adopt cloud netwo

Subscribe to our newsletter

Get the latest industry trends, exclusive insights, and Gcore updates delivered straight to your inbox.