
Efficient disk partitioning is a fundamental skill for anyone working with Linux systems. It allows you to organize your hard drive, separate system files from user data, set up swap space, or prepare for dual-booting operating systems. While graphical tools exist, mastering the command-line utility fdisk provides powerful and granular control over your storage devices.
fdisk is a traditional, menu-driven program used to manipulate partition tables. It works with various partition table formats, including MBR (Master Boot Record) and GPT (GUID Partition Table), though utilities like gdisk
are often preferred for full GPT support. Using fdisk typically requires root privileges because you are directly altering the structure of a hard drive.
The process begins by identifying the target disk you wish to modify, often done using commands like lsblk
or fdisk -l
to list available storage devices (e.g., /dev/sda
, /dev/sdb
, /dev/nvme0n1
). Once the device is identified, you invoke fdisk by running sudo fdisk /dev/sdX
, replacing /dev/sdX
with your target disk.
Inside the fdisk interactive mode, you’ll use single-letter commands to perform actions. The most common commands include:
p
: Prints the current partition table for the selected disk. This is crucial to see the existing structure before making changes.n
: Creates a new partition. You will be prompted to specify the partition type (e.g., primary, extended, logical for MBR), the starting sector, and the ending sector or size.d
: Deletes an existing partition. You will need to specify the number of the partition you wish to remove. Be extremely cautious with this command as it results in data loss.w
: Writes the changes to the partition table on the disk and exits fdisk. This command is permanent and applies all your modifications.q
: Quits fdisk without saving changes. Use this if you make a mistake or decide not to proceed.
Creating partitions with fdisk sets aside portions of the disk space. After writing the changes with w
, the operating system might need to be notified of the changes (sometimes requiring a reboot or using tools like partprobe
). However, partitioning is only the first step. Before you can use the new partitions for storage, you must format them with a filesystem (e.g., ext4, XFS, NTFS) using commands like mkfs.ext4 /dev/sdXY
(where /dev/sdXY
is the new partition, e.g., /dev/sda1
). Finally, these formatted partitions must be mounted to a mount point in your file system tree to be accessible.
In essence, fdisk provides a powerful, albeit lower-level, way to define how your Linux system sees and utilizes the raw space on your storage devices by creating and managing partitions directly from the command-line.
Source: https://www.linuxtechi.com/create-partitions-in-linux-using-fdisk/