Disk Partitioning with parted
GNU parted is the preferred tool for disk partitioning in scripts and automation because it supports non-interactive one-liner commands. It also handles disks larger than 2 TB, supports resizing partitions (with file system support), and outputs structured information. Ubuntu’s Subiquity installer uses parted internally, and most automation tools (Ansible, cloud-init) use it for disk setup.
parted vs fdisk
| Feature | fdisk | parted |
|---|---|---|
| Interactive menu | Yes (simple) | Yes (more options) |
| Non-interactive | No | Yes (parted -s flags) |
| Resize partitions | No | Yes (limited) |
| GPT support | Yes | Yes |
| Disks > 2 TB | Yes (GPT mode) | Yes |
| Script-friendly | Difficult | Excellent |
Interactive parted mode
# Start parted on a disk
sudo parted /dev/sdb
# Inside parted interactive mode:
(parted) print free # Show current partition table and free space
(parted) mklabel gpt # Create GPT partition table
(parted) mkpart primary ext4 0% 100% # One partition using full disk
(parted) print # Verify the partition
(parted) quit # Exit (changes are saved immediately, no 'w' needed!)
# IMPORTANT: Unlike fdisk, parted writes changes IMMEDIATELY when you run commands.
# There is no staging step. Be careful with destructive commands.
⚠️ WARNING: parted applies changes immediately — unlike fdisk which stages changes and requires
wto write. This means there is no review step before changes go to disk. Always confirm you have the right disk before running any parted commands.
Non-interactive scripted partitioning
# Create a GPT table and one partition in a single command
sudo parted -s /dev/sdb mklabel gpt mkpart primary ext4 0% 100%
# Create a GPT table and two partitions:
sudo parted -s /dev/sdb mklabel gpt mkpart primary ext4 0% 50% mkpart primary xfs 50% 100%
# Set the partition name (GPT supports partition names)
sudo parted -s /dev/sdb name 1 "data" name 2 "backup"
# Create a Linux LVM partition (for use with LVM)
sudo parted -s /dev/sdb mklabel gpt mkpart primary 0% 100% set 1 lvm on
# Verify
sudo parted /dev/sdb print
parted print output
Model: ATA SAMSUNG MZNLN256 (scsi)
Disk /dev/sdb: 500GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 500GB 500GB ext4 data lvm
Partition alignment
Proper partition alignment ensures that partition boundaries align with the storage device’s internal block size (especially 4K native sector drives and SSDs). Using percentage-based sizes (0%, 50%, 100%) in parted automatically ensures optimal alignment.
# Check partition alignment
sudo parted /dev/sdb align-check optimal 1 # Check partition 1 alignment
Expected output for properly aligned partition
1 aligned
# Always use percentage-based boundaries (0%, 100%) for automatic alignment
# NOT sector-based: mkpart primary ext4 2048s 1048575966s ← can be misaligned
# For multiple partitions, use percentages or round MiB boundaries
sudo parted -s /dev/sdb mklabel gpt mkpart primary 1MiB 201MiB \ # Boot: 200 MiB
mkpart primary 201MiB 100% # Data: rest of disk
Resizing partitions with parted
# Resize a partition (grow it to use more space)
# First shrink or remove the next partition if needed
sudo parted /dev/sdb
(parted) resizepart 1 80% # Resize partition 1 to end at 80% of disk
# After resizing a partition, resize the file system
sudo resize2fs /dev/sdb1 # ext4
# or
sudo xfs_growfs /mountpoint # XFS
Conclusion
Use parted when partitioning in scripts or automation: sudo parted -s /dev/sdb mklabel gpt mkpart primary ext4 0% 100% creates a properly aligned GPT partition in one line. Use percentage-based sizes for automatic alignment. Remember that parted applies changes immediately — always verify the target disk with lsblk first. After partitioning, format with mkfs.ext4 or mkfs.xfs, then mount or hand off to LVM.
FAQ
Is Disk Partitioning with parted important for Ubuntu administrators?+
Yes. It supports practical Ubuntu administration because it connects directly to server reliability, security, troubleshooting, or daily operations.
Should I practice this on a live server?+
Use a lab VM first. After you understand the command output and rollback path, apply the workflow carefully on real systems.
What should I do after reading this article?+
Run the practice commands, write down what each one shows, and continue to the next article in the Ubuntu roadmap.
Need help with Ubuntu administration?
Work directly with Muhammad Irfan Aslam for Ubuntu Server, Linux, cloud, Docker, DevOps, CI/CD, or infrastructure troubleshooting support.
Hire Me for Support