How to Resize a Partition in Linux Without Data Loss

Linux system administration needs to have efficient management of disk space. As systems expand and applications require additional disk storage, administrators frequently need to resize disk partitions. Resize partition lets you reduce or expand the space on any partitions without deleting any existing data.

Before you resize a partition, you should prepare for the task carefully. Check the current partition layout, verify the filesystem type, and make sure there is unallocated disk space available before you make any changes.

This tutorial describes different ways to resize a partition in Linux without losing data. The guide covers verifying disk layout, resizing the partitions with command line tools, and safely expanding your filesystems.

Understanding Disk Partitions in Linux

A disk partition divides a physical disk, such as a hard drive or an SSD, into logical sections of storage. Each partition can use its own filesystem, and you can mount it independently in the Linux directory system.

Common partition types include the root partition (/), the home partition (/home), the boot partition (/boot), and the swap partition. Each partition uses a filesystem, such as ext4, xfs, or btrfs, to store and manage data. When you resize a partition, you work with two components: the partition size, which defines the physical disk space allocated to the partition, and the filesystem size, which defines the structure that stores data inside the partition.

Linux provides several utilities that enable you to resize partitions and file systems safely. You can use tools like lsblk, fdisk, parted, resize2fs, and growpart to manage disk partitions without losing the data stored on them. These tools are especially useful when you work with virtual machines, cloud servers, or Linux environments where you may need to adjust storage size over time.

Check Current Disk Partitions

The first thing you need to do before you resize any partition is check your current disk layout. This step also helps make sure you know the disk device, partition numbers, and how much free space is available. The displayed command shows all disks and partitions.

lsblk

The output displays storage devices hierarchically, including disks and their associated partitions.

Check Current Disk Partitions to resize a partition

You can use the following commands to list partition details:

sudo fdisk -l
list partition details

This will show all disk partitions along with their types, sizes, and starting sectors. Proceed with caution and identify a partition to resize.

Check Free Space

To expand a partition, you must have unallocated space next to it on the disk. If no free space is available, you cannot extend the partition. You can use the following command to view disk usage and mounted filesystems.

df -h

This command will show the filesystem sizes, used, and available space in human-readable form.

Check Free Space before resizing a partition

There will be preparation if there are no empty places; you need to reduce another partition before expanding the given.

Method 1: Resize a Partition Using growpart 

Administrators widely use the growpart tool on cloud servers such as AWS or other VPS platforms. It allows you to expand a partition after increasing the size of the underlying disk.

Step 1: Install growpart

If your system does not have growpart you can install it using the command below.

On Ubuntu or Debian systems, run the following command:

sudo apt install cloud-guest-utils
Install growpart

For CentOS or RHEL systems, execute the following command:

sudo yum install cloud-utils-growpart

Step 2: Resize a Partition

After you increase the disk size, run the following command to expand the partition.

sudo growpart /dev/sda 2
resize a Partition

Here 2 means partition, /dev/sda 2 is a disk device. This will then expand the partition; i.e., this will let it take over the free space on that disk.

Step 3: Resize the Filesystem

After you enlarge the partition, you need to adjust it to use the additional space. For a partition with the ext4 filesystem, run the following command.

sudo resize2fs /dev/sda2
Resize the Filesystem

This command resizes the filesystem to fill the new partition.

Method 2: Using fdisk to Resize a Partition

Another utility to tweak disk partitions is the fdisk tool. But this method is delicate, as it means you have to delete the partition without deleting its data.

Step 1: Launch the fdisk Utility

Invoke the fdisk utility for the disk and specify the particular disk name. The command below will start up the fdisk utility for the specific disk.

sudo fdisk /dev/sda2
Launch the fdisk Utility

Step 2: Remove the Entry from Existing Partition 

Within the fdisk interface, remove the partition entry with the d command. This step removes the entry for the partition from the table but does not delete data residing on that partition.

Step 3: Recreate the Partition

Use the n command to recreate your partition once you have deleted it. Use the original partition, but increase its end sector.

Recreate the Partition

This allows us to format the space without losing the data already located in the partition.

Step 4: Write Changes to Disk

After completing the partition configuration, write the output to the disk.

Write Changes to Disk

This will update the partitioning table.

Step 5: Resize the Filesystem

After you resize the partition, run the following command to resize the filesystem.

sudo resize2fs /dev/sda2
Resize the Filesystem

This command resizes the filesystem to the new partition size.

Method 3: Resize a Partition Using parted

The third tool we can use to manage partitions on Linux systems is the parted utility. In this method, we will perform the resizing of the partition via the following steps.

Step 1: Launch the parted Utility

Create a new partition table using the parted utility and specify the partition:

sudo parted /dev/sda

When you run the command, the system drops you into the interactive parted environment.

Launch the parted Utility

Step 2: View Partition Layout

Now lets print the partition table inside parted.

print
View Partition Layout

The command lists the partitions on the disk and their start/end sectors.

Step 3: Resize a Partition

Next, run the command resizepart followed by the partition number.

resizepart 2
Resize a Partition

The tool then asks you to specify the new end position for the partition. You could choose the size according to the free space on the disk. After you enter the new size, the system resizes the partition.

Step 4: Resize the Filesystem

Once you resize the partition, you also need to increase the size of the filesystem.

sudo resize2fs /dev/sda2
Resize the Filesystem

And this makes sure that we use the extra space in the filesystem.

Best Practices to Resize a Partition 

Before you resize your partition, here are some best practices:

  • Back up all important data before proceeding with partitions.
  • Make sure Nginx and other services are not using the partition you are modifying.
  • Make sure to check the type of filesystem, so you will be able to use the correct resize command.
  • gdisk-fdisk verifies the partition starting sector to avoid corruption.
  • For safer partition management, always prefer saving output using verified tools like growpart or parted.

Following these steps greatly reduces the risk of losing your data.

Conclusion

Partition resizing in Linux is a very common task. It allows the administrator to be more efficient with disk space utilization as storage needs change. With the right tools and preparation, you can increase or modify partitions without losing data. There are several utilities in Linux for it, including growpart, parted, and fdisk. In addition to changing the partition size in storage, I need to run filesystem tools such as resize2fs for ext-based filesystems. You will discover how these tools function and can seamlessly resize partitions while ensuring Linux systems continue to operate uninterrupted by following best practices.

FAQs

Is it safe to resize a Linux partition without data loss?
Which command do you use to check disk partitions in Linux?
How can I reduce the size of a partition in Linux?
What do you use GrowPart for?
How do I check for successful partition resizing?
Is resizing partitions risky?

Related Post

Exploring the at Command in Linux

The at command in Linux is used to schedule tasks to ru...

How to use Linux export Command

Linux export command is a powerful tool for managing en...

How to Use apropos Command in Linux

The apropos command in Linux is an essential tool for b...

How to Check CentOS Version

Understanding which version of CentOS you're running is...

Nohup Command in Linux

The Nohup command, which stands for "no hang up," is a ...

How to Use the dmesg Linux Command

The dmesg command is a useful tool in Linux that lets y...

Leave a Comment