Creating and managing disk partitions is a fundamental skill for any Linux system administrator. Whether you’re setting up a new server, adding a raw disk to your environment, or reorganizing storage for optimized performance, knowing how to use the fdisk command effectively is essential. This guide will walk you through how to create partitions in Linux using the fdisk utility with clear, practical steps and real-world insights from years of system administration experience. By the end of this tutorial, you’ll be comfortable identifying disks, creating partitions, formatting them, and preparing them for mounting in your Linux infrastructure.
Understanding the fdisk Command and Disk Partitioning in Linux
The fdisk utility is a classic and powerful disk partitioning tool available on almost all Linux distributions. It allows you to create, modify, and delete partitions on block devices such as physical hard drives, SSDs, and virtual disks. Partitioning is crucial because Linux manages disks by dividing them into sections—partitions—that can then hold file systems or be used for other purposes such as swap space. In real production environments, correct partitioning helps optimize storage layout, improve backup strategies, and isolate system data from application data.
Linux device names typically follow conventions like /dev/sda, /dev/sdb for SATA/SCSI drives or /dev/nvme0n1 for NVMe SSDs. Before partitioning, it’s important to identify the correct disk to avoid data loss. A common mistake I have seen admins make is accidentally partitioning the system disk instead of an additional data disk. Always double-check which disk you are working on.
sudo fdisk -l Disk /dev/sda: 500 GiB, 536870912000 bytes, 1048576000 sectors Disk model: Samsung SSD 860 Units: sectors of 1 * 512 = 512 bytes Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors Disk model: Virtual Disk Units: sectors of 1 * 512 = 512 bytes
This command lists all disks and partitions. The -l flag tells fdisk to list the partition tables on all devices. Typically, you’ll use this to identify unpartitioned disks or verify existing partitions before making any changes.
Step-by-Step: Creating Partitions with fdisk
Once you’ve identified the target disk (for example /dev/sdb), invoke fdisk interactively to create new partitions:
sudo fdisk /dev/sdb Welcome to fdisk (util-linux 2.36.1). Changes will remain in memory only, until you decide to write them. Command (m for help):
Inside the interactive prompt, typing m shows help commands. To create partitions, you press n for a new partition. You will be guided through specifying partition type (p for primary or e for extended), partition number, and size. Using sectors directly is error-prone; instead, you can specify human-readable sizes like +4G for 4 gigabytes.
For example, creating two 4GB primary partitions:
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-20971519, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +4G
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (xxxx-xxxx, default xxxx):
Last sector, +size or +sectors (default xxxx): +4G
Command (m for help): w
The partition table has been altered.
Syncing disks.
Here, w writes the new partition table to disk and exits fdisk. One useful trick many administrators overlook is to double-check partition boundaries and sizes before writing, as a wrong partition table can cause data loss.
Formatting and Mounting the New Partitions
With the partitions created, the next step is to format them with the desired file system based on your workload. Ext4 is the default for many Linux distributions due to stability and performance, while XFS excels in handling large files and high I/O loads.
sudo mkfs.ext4 /dev/sdb1
Creating filesystem with 5242880 4k blocks and 1310720 inodes
Filesystem UUID: 12345678-90ab-cdef-1234-567890abcdef
Superblock backups stored on blocks: 32768, 98304, 163840
...
sudo mkfs.xfs /dev/sdb2
meta-data=/dev/sdb2 isize=512 agcount=4, agsize=262080 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=1048320, imaxpct=25
The commands mkfs.ext4 and mkfs.xfs create ext4 and XFS file systems respectively on the specified partitions. Choosing the right file system depends on factors such as compatibility, performance needs, and feature sets.
After formatting, create mount points and mount the partitions:
sudo mkdir /mnt/data1 sudo mkdir /mnt/data2 sudo mount /dev/sdb1 /mnt/data1 sudo mount /dev/sdb2 /mnt/data2
Finally, verify the mounts using:
df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sdb1 ext4 4G 50M 4G 2% /mnt/data1 /dev/sdb2 xfs 4G 10M 4G 1% /mnt/data2
The df -Th command displays mounted filesystems, their types, sizes, and mount points. It’s a quick way to confirm successful mounting.
For permanent mounts that persist across reboots, add entries to /etc/fstab. Be cautious editing this file; a typo can make your system unbootable.
/dev/sdb1 /mnt/data1 ext4 defaults 0 0 /dev/sdb2 /mnt/data2 xfs defaults 0 0
Best Practices for Partitioning with fdisk
Over my 15+ years managing Linux infrastructure, I’ve learned a few best practices that help avoid common pitfalls:
- Backup before you partition: Always back up important data before altering disk layouts, even on test or new systems.
- Use
lsblkfor device mapping: Beforefdisk, runlsblk -fto view block devices and existing filesystem labels for context. - Review partition layout before writing: Inside
fdisk, use the print command (p) frequently to verify your changes. - Use GPT for disks larger than 2TB: For large disks,
fdiskcan manage GPT partitions but consider usingpartedorgdiskfor enhanced features and modern schemes. - Reload partition tables carefully: After changes, reload tables with
partprobeor reboot if necessary to avoid stale device references.
Troubleshooting Scenario: New Partition Not Visible After Creation
In one troubleshooting case I handled, an administrator created partitions on a newly attached disk, but lsblk didn’t list them. Here’s what I did:
sudo partprobe /dev/sdb [sometimes no output, just reloads partition table]
The partprobe command informs the kernel of partition table changes. If you skip this step, the kernel may still use the old disk layout, not reflecting your new partitions. If issues persist, unmount related devices, ensure no processes use them, or reboot the system as a last resort.
Another common problem I’ve seen is trying to format a partition that’s still mounted, causing an error. Always verify partitions are unmounted before formatting:
sudo umount /dev/sdb1
Following these steps systematically helps avoid common mistakes encountered in real-world system administration.
Conclusion
The fdisk command remains a reliable and widely used tool to create and manage disk partitions on Linux systems, especially useful on older hardware or virtual machines. Understanding how to safely identify disks, create partitions, format them, and mount them properly is a critical skill in a sysadmin’s toolkit. By following this detailed guide and applying best practices, you’ll minimize risk and maximize the reliability of your storage setup. Whether you are scaling storage capacity or setting up new services, partitioning with fdisk ensures your Linux systems stay organized, efficient, and ready for production workloads.