BLOG POSTS
Create a Partition in Linux – Step-by-Step Guide

Create a Partition in Linux – Step-by-Step Guide

Whether you’re setting up a shiny new server or reconfiguring an existing one, partitioning storage drives is one of those fundamental skills that’ll save your bacon more times than you can count. This guide will walk you through creating partitions in Linux using various tools and methods, from the trusty old fdisk to the more modern parted utility. You’ll learn when to use each tool, how to avoid common pitfalls that could turn your data into digital confetti, and pick up some pro tips that’ll make your server management life significantly easier. By the end of this, you’ll be confidently slicing and dicing storage like a seasoned sysadmin.

How Linux Partitioning Actually Works

Before we dive into the commands, let’s talk about what’s happening under the hood. Linux treats everything as a file, including your storage devices. When you plug in a drive, it shows up as something like /dev/sda or /dev/nvme0n1. Partitions are just logical divisions of this physical space, appearing as /dev/sda1, /dev/sda2, and so on.

There are two main partition table types you’ll encounter:

  • MBR (Master Boot Record) – The old-school format, limited to 4 primary partitions and 2TB drives
  • GPT (GUID Partition Table) – The modern standard, supporting 128 partitions and drives up to 9.4ZB (yeah, that’s zettabytes)

Here’s a reality check: if you’re working with anything larger than 2TB or need more than 4 partitions, go with GPT. MBR is basically legacy at this point, though you’ll still see it on older systems.

Tools of the Trade

Linux gives you several partitioning tools, each with its own personality:

  • fdisk – The classic, text-based, works great with MBR
  • parted – More powerful, handles both MBR and GPT like a champ
  • gdisk – GPT specialist, part of the gptfdisk package
  • cfdisk – fdisk’s prettier cousin with a pseudo-GUI

Step-by-Step Partitioning Guide

Let’s get our hands dirty. First, you’ll want to see what storage devices you’re working with:

lsblk
# or for more detailed info
fdisk -l

This shows you all available block devices. Look for your target drive – let’s say it’s /dev/sdb for our examples.

Method 1: Using fdisk (The Classic Approach)

fdisk is like that reliable old truck – not fancy, but it gets the job done. Here’s how to create a partition:

# Start fdisk on your target drive
sudo fdisk /dev/sdb

# Inside fdisk, use these commands:
# p - print partition table
# n - create new partition
# d - delete partition
# w - write changes and exit
# q - quit without saving

Let’s create a new partition step by step:

# Start fdisk
sudo fdisk /dev/sdb

# Create new partition
Command (m for help): n

# Choose partition type
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p

# Partition number
Partition number (1-4, default 1): 1

# First sector (accept default)
First sector (2048-41943039, default 2048): [Enter]

# Last sector (specify size)
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +10G

# Write changes
Command (m for help): w

Method 2: Using parted (The Modern Way)

parted is more straightforward for scripting and handles GPT natively:

# Create GPT partition table
sudo parted /dev/sdb mklabel gpt

# Create a partition
sudo parted /dev/sdb mkpart primary ext4 0% 10GB

# Or do it interactively
sudo parted /dev/sdb
(parted) print
(parted) mkpart primary ext4 0% 100%
(parted) quit

Method 3: One-liner for Automation

For scripting and automation (perfect for server deployments), you can use parted in non-interactive mode:

# Create GPT table and partition in one go
sudo parted -s /dev/sdb mklabel gpt mkpart primary ext4 0% 100%

# Multiple partitions
sudo parted -s /dev/sdb \
  mklabel gpt \
  mkpart primary ext4 0% 50% \
  mkpart primary ext4 50% 100%

Formatting and Mounting

Creating a partition is only half the battle. You need to format it and mount it:

# Format the partition
sudo mkfs.ext4 /dev/sdb1

# Create mount point
sudo mkdir /mnt/mydata

# Mount temporarily
sudo mount /dev/sdb1 /mnt/mydata

# Make it permanent by adding to /etc/fstab
echo '/dev/sdb1 /mnt/mydata ext4 defaults 0 2' | sudo tee -a /etc/fstab

Real-World Examples and Use Cases

Scenario 1: Web Server Setup

You’ve got a fresh server and need to separate your web files from system files:

# Create separate partitions for different purposes
sudo parted -s /dev/sdb \
  mklabel gpt \
  mkpart primary ext4 0% 40% \
  mkpart primary ext4 40% 80% \
  mkpart primary ext4 80% 100%

# Format them
sudo mkfs.ext4 /dev/sdb1  # Web files
sudo mkfs.ext4 /dev/sdb2  # Database
sudo mkfs.ext4 /dev/sdb3  # Logs

# Mount them
sudo mkdir -p /var/www /var/lib/mysql /var/log/custom
sudo mount /dev/sdb1 /var/www
sudo mount /dev/sdb2 /var/lib/mysql  
sudo mount /dev/sdb3 /var/log/custom

Scenario 2: The Disaster Recovery Setup

Here’s a real-world gotcha: you create a partition, format it, copy 500GB of data, then realize you made the partition too small. Don’t panic – you can extend it:

# Extend partition (assuming it's the last one)
sudo parted /dev/sdb resizepart 1 100%

# Resize filesystem to match
sudo resize2fs /dev/sdb1

Common Pitfalls and How to Avoid Them

Problem Symptoms Solution
Wrong device selected Data loss, system won’t boot Always double-check with lsblk first
MBR on large drives Can’t use full capacity Use GPT for drives >2TB
Forgetting to update fstab Partitions not mounted after reboot Always add entries to /etc/fstab
Alignment issues Poor performance on SSDs Use percentage-based sizing in parted

Advanced Tricks and Automation

Here’s where things get interesting. You can script entire server setups:

#!/bin/bash
# Automated partition setup script

DEVICE="/dev/sdb"
WEB_SIZE="20GB"
DB_SIZE="30GB"

# Partition the drive
sudo parted -s $DEVICE \
  mklabel gpt \
  mkpart primary ext4 0% $WEB_SIZE \
  mkpart primary ext4 $WEB_SIZE $DB_SIZE \
  mkpart primary ext4 $DB_SIZE 100%

# Wait for the kernel to recognize new partitions  
sudo partprobe $DEVICE

# Format partitions
for i in {1..3}; do
    sudo mkfs.ext4 ${DEVICE}${i}
done

# Create mount points and mount
sudo mkdir -p /var/www /var/lib/mysql /var/backups
sudo mount ${DEVICE}1 /var/www
sudo mount ${DEVICE}2 /var/lib/mysql
sudo mount ${DEVICE}3 /var/backups

echo "Partitioning complete!"

Performance Considerations

Here’s something most guides don’t tell you: partition alignment matters, especially on SSDs. Modern tools handle this automatically, but if you’re working with older systems:

# Check alignment
sudo parted /dev/sdb align-check optimal 1

# Force proper alignment (parted does this by default now)
sudo parted -a optimal /dev/sdb mkpart primary ext4 0% 100%

LVM: The Next Level

Once you’re comfortable with basic partitioning, consider LVM (Logical Volume Manager). It’s like partitioning on steroids – you can resize, snapshot, and move volumes around like nobody’s business:

# Create physical volume
sudo pvcreate /dev/sdb1

# Create volume group  
sudo vgcreate mydata /dev/sdb1

# Create logical volume
sudo lvcreate -L 10G -n webfiles mydata

# Format and mount
sudo mkfs.ext4 /dev/mydata/webfiles
sudo mount /dev/mydata/webfiles /var/www

Monitoring and Maintenance

Don’t just set it and forget it. Keep tabs on your partitions:

# Check disk usage
df -h

# Check filesystem health
sudo fsck /dev/sdb1

# Monitor disk I/O
iostat -x 1

When Things Go Wrong

Murphy’s Law applies to storage too. Here’s your emergency toolkit:

# Recover partition table
sudo testdisk /dev/sdb

# Check filesystem integrity
sudo e2fsck -f /dev/sdb1

# Force filesystem check on next boot
sudo touch /forcefsck

Cloud and VPS Considerations

If you’re working with cloud instances or VPS servers, you might need to handle dynamic disk attachment. Most cloud providers let you add storage on the fly, which is perfect for scaling applications. When setting up a VPS or dedicated server, plan your partition layout in advance – it’s much easier than reshuffling later.

# Scan for new disks (useful after hot-plugging)
echo "- - -" | sudo tee /sys/class/scsi_host/host*/scan

# Refresh partition table
sudo partprobe

Conclusion and Recommendations

Partitioning might seem intimidating at first, but it’s one of those skills that becomes second nature with practice. Use fdisk for simple, quick jobs on smaller drives, but embrace parted for anything serious – especially if you’re dealing with large storage or need to script your setup. GPT should be your default choice unless you’re dealing with legacy systems that absolutely require MBR.

For production environments, always test your partitioning scripts on disposable systems first. Consider LVM for complex setups where you might need flexibility later. And remember – measure twice, partition once. A typo in device names can turn a routine maintenance task into a resume-generating event.

The key is understanding your use case: simple web server? Basic partitioning is fine. Complex database cluster? You’ll want LVM with proper monitoring. Container orchestration? Maybe look into modern filesystems like ZFS or Btrfs. Whatever you choose, document your decisions and automate what you can – future you will thank present you.

Most importantly, always have backups. Partitioning tools are powerful, and with great power comes great responsibility (and the occasional spectacular failure). But master these techniques, and you’ll handle storage like a pro, whether you’re managing a single VPS or a fleet of dedicated servers.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

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