Introduction
In the world of Linux system administration, one of the most fundamental tasks is partitioning hard drives. Whether you are setting up a new server, rescuing data from an old machine, or simply organizing storage space, knowing how to use fdisk gives you the precise control needed to create, modify, and delete partitions safely.
What is fdisk?
fdisk (fixed disk) is a command-line utility present in practically all Linux distributions. It allows you to view the partition table of a block device, create new partitions, change their type, delete existing ones, and write the changes to the disk. Unlike graphical tools, fdisk works directly with the device, making it ideal for server environments or systems without a graphical interface.
When to use fdisk?
Use fdisk when you need to partition a new disk, resize existing partitions (although for resizing it is often better to combine it with resize2fs or parted), change the partition type (for example, from Linux to swap or to a GPT type), or when you want to inspect the current partition table to diagnose boot or hardware recognition problems.
Installation and verification
In most distributions, fdisk comes preinstalled. You can verify its presence by running which fdisk or fdisk --version. If for some reason it is missing, install it via the package manager: on Debian/Ubuntu use sudo apt-get install util-linux, on RHEL/CentOS use sudo yum install util-linux, and on Fedora use sudo dnf install util-linux.
Starting fdisk
To work with a disk, first identify it with lsblk or fdisk -l. Then invoke the utility specifying the device, for example sudo fdisk /dev/sda. Note: always use sudo or run as root, since privileged access to the hardware is required. Upon entering, you will be presented with a command prompt where you can type the various options.
Basic fdisk commands
Within the fdisk interface, commands are typed as a single letter followed by Enter. The most important ones are: p to print the current partition table, n to create a new partition, d to delete a partition, t to change the type (ID) of a partition, w to write the changes and exit, and q to quit without saving. There is also m which displays the help menu.
Creating a partition
To create a partition, first type n. Then choose whether it will be a primary partition (p) or an extended partition (e) – on MBR disks the limit is four primary partitions or three primary and one extended. Next, specify the partition number, the first sector (you can accept the default value
This post is also available in ESPAÑOL.