The fdisk command in Linux: partition hard drives

Introduction

In the world of Linux system management, one of the most fundamental tasks is hard drive partitioning. Whether you are preparing a new server, rescuing data from an old team or simply organizing the storage space, knowing how to usefdiskgives you the precise control necessary to create, modify and remove partitions safely.

What's fdisk?

fdisk(fixed disk) is a command line utility present in virtually all Linux distributions. It allows you to view the partition table of a block device, create new partitions, change its type, remove existing ones and write changes to the disk. Unlike graphic tools,fdiskworks directly with the device, making it ideal for server environments or systems without a graphic environment.

When to use fdisk?

Usefdiskwhen you need to partition a new disk, resize existing partitions (although to resize it is usually better to combine withresize2fsorparted), change the partition type (e.g. from Linux to swap or to a GPT type), or when you want to inspect the current partition table to diagnose hardware boot or recognition problems.

Installation and verification

In most distributions,fdiskcomes pre-installed. You can check your presence by runningwhich fdiskorfdisk --version. If for any reason missing, install it using the package manager: in Debian / Ubuntu usesudo apt-get install util-linux, in RHEL / CentOS usesudo yum install util-linuxand in Fedorasudo dnf install util-linux.

Initiating fdisk

To work with a disk, first identify it withlsblkorfdisk -l. Then invoke the utility by specifying the device, for examplesudo fdisk /dev/sda. Note: always usesudoor run as root, as you need privileged access to hardware. When you enter, you will be presented with a command prompt where you can type the different options.

Basic fdisk commands

Within the interfacefdiskthe commands are typed as a single letter followed byEnter. The most important are:pto print the current partition table,nto create a new partition,dto delete a partition,tto change the type (ID) of a partition,wto write the changes and go out, andqto go out without keeping. It also existsmwhich shows the help menu.

Creating a partition

To create a partition, first typen. Then choose if it will be a primary partition (p) or extended (e) - on MBR disks the limit is four primary partitions or three primary partitions and one extended. Then indicate the partition number, the first sector (can accept the default value) and the last sector or size (e.g.,+20Gfor 20 gigabytes). Repeat the process as needed.

Change the partition type

Once the partition is created, it may be necessary to modify its type for the system to properly recognize (for example, to mark it as a swap or as a Linux file system partition). Tecleet, select the partition number and then enter the corresponding hexadecimal code (as82for swap Linux or83for standard Linux). You can see the list of available codes withLinside the same menu.

Erasing a partition

If you need to remove a partition that no longer serves, use the commandd. fdiskYou will be asked to delete the partition number. Confirm selection and partition will be marked for removal; changes will not apply until you writew. Be careful: delete a partition removes the entry in the partition table, but the data can remain physically until they are overwritten.

Write changes and good practices

After making all the desired modifications, typewto write the new partition table on the disk and leave. If you decide not to save, useq. It is always recommended to back up critical data before partitioning and, where possible, work on a disk that is not mounted. In addition, after writing the changes, you may need to reread the partition table withpartprobeor restart the system so that the kernel recognizes the new partitions.

Practical example

Suppose you've added a new 500 GB disk identified as/dev/sdband want to create a 100 GB partition for the file systemext4and another 400 GB for storage. The steps would be:sudo fdisk /dev/sdb, pto see that it is empty,np→ partition number 1 → accept first sector →+100G, t → 1 → 83, repeatnfor the second partition with+400Gand type83, finallyw. Then form withmkfs.ext4 /dev/sdb1andmkfs.ext4 /dev/sdb2and mount as you need.

Conclusion

Domainfdiskis an essential skill for any Linux administrator. Although there are more modern alternatives such aspartedor graphic tools,fdiskremains reliable, fast and available in virtually any environment. With practice and attention to details, you can safely and efficiently partition hard drives, ensuring that your systems are well organized and ready to work.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish