1. Home
  2. Linux
  3. Manually partition a hard drive command line linux

How To Manually Partition A Hard Drive From The Command Line On Linux

Looking to manually partition a hard drive before installing your favorite Linux distribution? If so, consider doing it on the command line. It’s not as tedious as you think. In fact, manually partitioning a hard drive in the terminal is much faster, more efficient, and allows you to get your OS installed much quicker.

In addition to the terminal method being faster, it will also teach you a lot more about how partitions work on Linux.

That’s why in this guide we’re going to go over how you manually partition a hard drive from the command line on Linux, using GNU Parted.

UFEI

These are the instructions for manually partitioning a hard drive while using UEFI/secure boot.

Single root

A “single root” setup just means that all data for your Linux installation is on one single partition. There’s no separate /home, /var or anything like that. This setup is good for new users who are just getting into manual partition setups, and don’t understand how (or why) separate mount points interact with the system.

A straightforward way to quickly partition a hard drive on Linux is with the parted tool. There isn’t a confusing terminal graphical interface to use. Instead, users only have to input partition sizes, then format them later on.

For a UEFI setup, follow these steps. First, open the terminal and run lsblk. This command will list all block devices. Use this to find your drive label. Then, take the name and do:

Note: replace X with the letter lsblk shows.

sudo parted /dev/sdX

For a UEFI setup, the hard drive table needs to be GPT. Using the parted tool, create a GPT partition table on the hard drive.

mklabel gpt

The next step in the UEFI process is to create a separate boot partition.

mkpart ESP fat32 1MiB 513MiB

If you need a swap partition for your UEFI system, make one using parted. Keep in mind that it should be around the same size as your RAM (a 2GB machine should have a 2GB swap, etc.). That said, if you’ve got 8 GB of RAM or more, consider not making a swap partition above 4 GB.

mkpart primary linux-swap 513MiB 4GiB

With /boot and swap out of the way, the last thing left to do in your manual partition setup is to create the /root partition. This partition will house pretty much everything on your PC so it needs to take up the rest of the hard drive.

mkpart primary ext4 4GiB 100%

Enter quit into the prompt to exit. It is at this point we can use the mkfs command to format all of the file systems so that they can be used in any Linux distribution installer later on. In this example, we’ll be using /dev/sda as the drive label. Yours may differ.

mkfs.vfat -F32 /dev/sda1

mkfs.ext4 -f /dev/sda3

Split Home

Want a split home for your UEFI setup instead? If so, follow all the instructions above till you get to the /root portion. Ignore the root steps above, and follow these instead:

In this example, the hard drive is 500 GB. Keep in mind that yours may have a different capacity and you will need to change the commands accordingly.

Note: though the drive is 500 GB, 4 GB+ 512 MB is in use. That leaves us roughly 495 GB left. In this next step, we’ll give 100 GB to the /root partition, as the /home partition should always be more significant in size, for this setup.

mkpart primary ext4 4GiB 104GiB

With the /root partition using 100 GB of the hard drive, we have about 395 GB left to apply for the /home partition. The numbers for this last part do not need to be exact. Instead, we can tell parted to fill up the rest of the drive.

mkpart primary ext4 104GiB 100%

Partitions are all set, so it’s OK to exit the Parted tool. Use quit to exit the program. Then, format the new partitions with mkfs to finalize everything.

sudo mkfs.vfat -F32 /dev/sda1
sudo mkfs.ext4 -f /dev/sda3 

sudo mkfs.ext4 -f /dev/sda4

BIOS

Open your drive in the parted tool:

sudo parted /dev/sdX

Inside Parted, create a create an MS-DOS partition table.

mklabel msdos

Single Root

In this layout, swap should come first. Using Parted, make the new swap partition. Swap partitions should always be the same size as your RAM. However, if you’ve got 8 GB, 16 GB or more, consider using 4 instead. Having a 32 GB swap partition is very much overkill.

mkpart primary linux-swap 1MiB 4GiB

To finish up your single root layout, tell the Parted tool to use the rest of the hard drive for this last partition.

mkpart primary ext4 4GiB 100%

From here, enter quit to exit the Parted tool, and then use mkfs to format the newly created partitions so that Linux operating system installation tools can correctly read them.

sudo mkfs.ext4 -f /dev/sda2

Split Home

For a split home setup, follow these instructions instead. First, create your MS-DOS partition table.

mklabel msdos

Create a swap partition for the system to use:

mkpart primary linux-swap 1MiB 4GiB

In this next step, we divide up the hard drive so that the root partition has 100 GB of space, and the home partition has the rest. For the sake of example, our drive has 500 GB. Yours may differ. Create your root partition in Parted, and tell the tool to give it 100 GB of space to use.

mkpart primary ext4 4GiB 104GiB

Make your home partition using “100%” so that it uses the rest of the available space.

mkpart primary ext4 104GiB 100%

Quit the parted tool using quit and then use mkfs to format the new partitions.

sudo mkfs.ext4 -F /dev/sda2

sudo mkfs.ext4 -F /dev/sda3

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.