Linux Partitions: Create File System Partitions in Terminal
In this article
To create file system partitions in Linux terminal, use fdisk or parted to define the partition layout on a disk, then format each partition with mkfs to build the file system. This guide walks you through the entire process, from identifying disks to mounting the new partitions, with commands and examples you can run immediately.
Background: Why Use the Terminal for Partitioning?
Graphical tools like GParted are convenient, but the terminal offers several advantages. It works on headless servers, in recovery environments, and over SSH. Scripts can automate partitioning tasks, and you have finer control over alignment, partition types, and file system options. Understanding the command-line tools also helps you troubleshoot boot issues and customize installations.
Modern Linux systems use fdisk (for MBR and GPT), parted (for GPT and MBR), and gdisk (specifically for GPT). All are text-based and available in most distributions' default repositories.
Identifying Disks and Partitions
Before making any changes, you must know which device file corresponds to your disk. Use lsblk or fdisk -l to list block devices. The output shows device names like /dev/sda, /dev/nvme0n1, or /dev/mmcblk0, along with existing partitions (e.g., /dev/sda1).
lsblk– shows a tree of devices, mount points, and sizes.sudo fdisk -l– lists all disks and their partition tables.sudo blkid– displays UUIDs and file system types of existing partitions.
Double-check the device name—writing to the wrong disk can destroy data. A common mistake is confusing /dev/sdb with /dev/sda. Use lsblk to see sizes and model names to confirm.
Step-by-Step: Creating a Partition with fdisk
fdisk is the classic tool for MBR and GPT. Here’s a typical workflow for a new disk (e.g., /dev/sdb):
- Start fdisk on the target disk:
sudo fdisk /dev/sdb - Type
gto create a new GPT partition table (orofor MBR). - Type
nto add a new partition. Choose the partition number (default 1), first sector (press Enter to use default), and last sector (e.g.,+20Gfor 20 GiB, or press Enter to use the whole disk). - Type
tto change the partition type if needed (e.g.,1for EFI System,20for Linux filesystem). The default type is Linux filesystem, which is fine for most uses. - Type
wto write the changes and exit.
After writing, the kernel may not immediately recognize the new partition. Run sudo partprobe /dev/sdb to update the partition table without rebooting.
Formatting the Partition with mkfs
Once the partition exists (e.g., /dev/sdb1), you must create a file system on it. The mkfs command has front-ends for each file system type:
sudo mkfs.ext4 /dev/sdb1– ext4, the default for most Linux distributions.sudo mkfs.xfs /dev/sdb1– XFS, good for large files and high performance.sudo mkfs.btrfs /dev/sdb1– Btrfs, with snapshots and compression.sudo mkfs.vfat -F 32 /dev/sdb1– FAT32, for compatibility with Windows and UEFI boot.
Formatting erases all data on that partition. There is no confirmation prompt, so verify the device name one more time. You can add a label with -L mylabel to make the partition easier to identify later.
Choosing a File System
The file system you choose affects performance, features, and compatibility. Here are the most common options:
- ext4 – Mature, reliable, and the default on many distros. Supports journaling, extents, and online defragmentation.
- XFS – Excellent for large files and parallel I/O; scales well on big storage arrays.
- Btrfs – Offers snapshots, compression, and RAID-like features, but has a more complex learning curve.
- FAT32 / exFAT – Needed for USB drives that must work with Windows, macOS, and game consoles. exFAT handles files larger than 4 GB.
- ZFS – Not in the standard kernel, but available via DKMS or external repositories; provides advanced data integrity and pooling.
For a typical Linux data partition, ext4 is a safe choice. For a root filesystem on modern hardware, many prefer Btrfs or XFS. If you plan to dual-boot with Windows, use NTFS or exFAT for shared partitions.
Mounting and Making It Permanent
After formatting, mount the partition to access it:
sudo mkdir /mnt/mydata
sudo mount /dev/sdb1 /mnt/mydataTo mount automatically at boot, add an entry to /etc/fstab. Use the UUID instead of the device name to avoid issues if device names change. Get the UUID with sudo blkid /dev/sdb1, then add a line like:
UUID=your-uuid-here /mnt/mydata ext4 defaults 0 2Test the fstab entry with sudo mount -a before rebooting. A wrong fstab line can prevent the system from booting, so always double-check the syntax and the mount point exists.
Common Problems and Safety Tips
Partitioning mistakes can be costly. Here are frequent pitfalls and how to avoid them:
- Wrong disk – Always verify with
lsblkandfdisk -l. Consider usingwipefsto remove old signatures if you get “invalid partition table” errors. - Partition table not recognized – If the kernel doesn’t see the new partition, run
partprobeor reboot. - Alignment issues – For SSDs, ensure partitions are aligned to 1 MiB boundaries.
fdiskdoes this by default when you accept the default first sector. - Forgetting to format – A partition without a file system is invisible to the OS. Always run
mkfsafter creating the partition. - fstab errors – Use the
nofailoption in fstab for non-critical mounts to prevent boot hangs if the device is missing.
Back up important data before partitioning. Tools like dd can clone entire disks, but they are not a substitute for regular backups. If you are resizing an existing partition, use resize2fs for ext4 or xfs_growfs for XFS, but only after shrinking the file system first—never the other way around.
Alternatives to fdisk
While fdisk is ubiquitous, other tools offer different interfaces:
- parted – More scriptable and handles GPT better than older fdisk versions. Use
parted /dev/sdb mklabel gptandparted /dev/sdb mkpart primary ext4 1MiB 100%. - gdisk – Specifically for GPT, with a similar interface to fdisk.
- cfdisk – A curses-based interactive menu that is easier for beginners.
- GParted – Graphical, but can be installed on a live USB for non-destructive resizing.
For automated provisioning, tools like sfdisk can read partition layouts from a script or file, making it easy to replicate configurations across many machines.
Frequently Asked Questions
Is it safe to partition a disk that already has data?
No. Partitioning tools assume you know what you are doing. Creating a new partition table or deleting partitions will erase data. Always back up first, and double-check the device name.
What is the difference between MBR and GPT?
MBR (Master Boot Record) is older, supports up to 4 primary partitions (or 3 plus an extended partition), and disks up to 2 TB. GPT (GUID Partition Table) is modern, supports unlimited partitions (in practice limited by the OS), and disks larger than 2 TB. UEFI systems typically require GPT.
Can I create a partition without formatting it?
Yes, you can create the partition and skip mkfs. The partition will exist but have no file system, so you cannot store files on it until you format it later.
Why do I get “partition table entries are not in disk order”?
This warning appears when partition start sectors are not in increasing order. It is often harmless, but you can fix it by deleting and recreating the partitions in order, or using parted which handles this automatically.
How do I resize a partition from the terminal?
Use parted to resize the partition itself (e.g., parted /dev/sdb resizepart 1 100%), then resize the file system with resize2fs (ext4) or xfs_growfs (XFS). Always shrink the file system before shrinking the partition, and grow the partition before growing the file system.