How to resize a partition + filesystem on Linux from the command line

Resizing a Partition + Filesystem on Linux from the CLI

Introduction

In this article, you'll learn how to resize a partition and filesystem on GNU/Linux using the CLI tools parted and resize2fs

Note that we'll only be covering EXPANSION / GROWING, and not shrinking, since it's much more difficult to shrink a partition than to grow it.

What you'll need:

  • A system running Linux (Ubuntu, Debian, Linux Mint, Fedora, CentOS, RedHat or anything else)
  • Free space in your partition table - AFTER the partition you want to resize - to extend the partition into.
  • The third-party program parted (available in most package managers)

Looking to buy a Virtual or Dedicated server? Do you like privacy and low prices? Try Privex!

We have virtual servers starting from just US$0.99/mo, and dedicated servers starting from as low as US$50/mo

Unlike other hosts, we don't ask for any personal details - only a name (can be an alias / username), and an e-mail address so we can send you your server details and renewal invoices.

We also accept several different cryptocurrencies with our own in-house payment processor - no third parties involved in processing your payments.

At the time of writing, we currently accept: Bitcoin (BTC), Litecoin (LTC), Monero (XMR), Dogecoin (DOGE), HIVE, and HBD

Order a server TODAY! Privacy is affordable™

1. Installing GNU parted

For Debian-based distros such as Ubuntu, Debian, Linux Mint, etc. - you can install parted from apt:

sudo apt update
sudo apt install -y parted

For Redhat-based distros (Fedora, Redhat, CentOS, Oracle, Amazon Linux, etc.), you can use either dnf (for modern distros) or yum (for older distros):

# for modern distros
sudo dnf install -y parted
# for older distros, or if you get an error saying dnf isn't installed
sudo yum install -y parted

For Arch Linux, you can use pacman:

sudo pacman -Sy parted

2. Check fdisk to find the right disk / partition + disk/partition info

Run the following command to get information about all disks and partitions on your system:

sudo fdisk -l

Look through the output to find the disk (e.g. /dev/sda) that you want to resize, as well as the partition you want to resize.

You might also be able to find the disk /dev/ path by running df -h / (if you want to resize a different partition instead of the rootfs / - then replace / with the mountpoint of the partition you want to resize)

3. Resize the disk partition ("physical" partition) on your partition table using parted

Once you've located the disk you want to resize a partition on (e.g. /dev/sda), open it using parted:

sudo parted /dev/sda

Now use the print command within parted, which will print information about that disk, and the partitions on the disk, so you can figure out how much space you have available to expand the partition into, and the partition number of the partition you want to resize:

print

Finally, use resizepart [partition_number] with the number of the partition you want to resize, to actually resize the partition:

resizepart 1

You may be warned that the disk is in use, this is generally okay, so enter yes

When asked to enter the End: - if the partition is at the end of the disk, you can enter the size of the disk outputted by print, but take off 0.2GB for safety, e.g. if it was 48.5GB - you'd enter 48.3G.

If the partition is between two partitions, then you'll want to enter an end location at least 100mb before the "Start" of the following partition. For example, if you're resizing partition 1 (start: 1020MB, end: 20GB), and partition 2 starts at 41.2GB - then you could set the End of part 1 to 41GB (~200mb before part 2).

4. Resize the actual filesystem to fit the grown partition

The command you'd run to grow a filesystem, depends on what filesystem the partition is actually formatted with.

Most Linux systems use the filesystem ext4 (or an older version such as ext3 - which uses the same command to resize anyway), though either yourself or the installer for your distro may have formatted some/all of the partitions with an alternative filesystem such as XFS, or Btrfs.

To discover the filesystem used by each partition on your system, use the following command:

lsblk -f

To grow an ext2, ext3 or ext4 filesystem, use the command (where /dev/sda1 is the partition device to resize):

sudo resize2fs /dev/sda1

To grow an xfs filesystem, use the command (where / is the MOUNTPOINT of the XFS formatted device):

sudo xfs_growfs /

To grow a btrfs filesystem, use the command (where / is the MOUNTPOINT of the Btrfs formatted device):

sudo btrfs filesystem resize max /

Finally, check the filesystem was resized by running df -h to list the size, used space, and avail space on all mounts:

df -h

Looking to buy a Virtual or Dedicated server? Do you like privacy and low prices? Try Privex!

We have virtual servers starting from just US$0.99/mo, and dedicated servers starting from as low as US$50/mo

Unlike other hosts, we don't ask for any personal details - only a name (can be an alias / username), and an e-mail address so we can send you your server details and renewal invoices.

We also accept several different cryptocurrencies with our own in-house payment processor - no third parties involved in processing your payments.

At the time of writing, we currently accept: Bitcoin (BTC), Litecoin (LTC), Monero (XMR), Dogecoin (DOGE), HIVE, and HBD

Order a server TODAY! Privacy is affordable™