How to Install ERPNext on Rocky Linux 9 3

How to Use fdisk Command in Linux

Disk partitioning can be a challenging task for new Linux users who aren’t used to the command line yet. Almost all Linux distro installers nowadays offer disk partitioning on the get-go, but most first-time users opt for the default option. If you want to make a new partition after installation, one of the best options to use is the fdisk command in Linux.

What is the fdisk Command?

Fixed Disk Editor or fdisk is a system administration tool designed to manipulate disk partition tables. It can modify, delete, or make partitions for DOS, Linux, Windows, FreeBSD, and other operating systems. You need elevated permission to utilize this tool.

Uses of fdisk command in Linux

fdisk has a bunch of available commands for different usage needs. Some are only for showing the disk status, some are more advanced and complicated. However, most people only use fdisk for a few reasons: viewing or fixing the partition table, as well as creating, modifying, and deleting partitions.

how to use fdisk command in linux

Default Syntax

fdisk command in Linux offers numerous functionalities such as changing the sector size, boot flag option, compatibility, and even locking the device to limit usage. The default syntax is as follows:

$ fdisk [options] device

As usual, you can check all the possible arguments using the Linux man-pages or by using the -h argument.

If you want to check all the available devices, you can use the list option:

$ fdisk -l /dev/sda

The Command Mode in fdisk

fdisk is available in “command mode” allowing users to operate the tool by inputting single letters instead of lengthy and confusing prompts. To avoid unnecessary disk damages, all the commands executed will not be written on the disk without the user’s final write confirmation. You can access the command mode with this code:

$ fdisk /dev/sda
how to use fdisk command in Linux

This safety net was placed to prevent users from making damages if they are still inexperienced in using the command line or Linux in general. But do note that there are still chances of errors to occur. Backing up your content is still the best move before doing anything permanent with your partition table.

Viewing all Available Partitions

You can enter the command mode by typing fdisk followed by your device name. The default is usually set to /dev/sda, while the next set of drives attached would start at /dev/sdb. Once you’re in, you can press m to view all the available commands.

Enter p to view all the partitions in the table that are currently active. Each partition is noted by a number so that the first partition would be /dev/sda1. It will also tell you the starting and ending sectors, as well as the type and size.

showing all partition using fdisk command in Linux

In the example, you can see that the hard drive has 10GB capacity, 512MB is reserved for the bootloader, while 7.66GB is already used for the Linux installation. You can tell the beginning and end of each partition through their sectors.

Creating a New Partition (With Unallocated Space)

It’s a good idea to check the unallocated space to know how big the new partition can be. You can check the unallocated space by pressing F (case sensitive) in the command mode. Continuing on the example, there is about 2GB unallocated space from the overall 10GB drive.

fdisk unallocated space

Use n to execute the prompt for adding new partitions. It will ask for input on what sector the new partition would start. Press F or p to get the specific sector details. Conveniently, the prompt will also show the first and last sector available.

The command will notify you if your unallocated space was once an active partition. Usually, it will inform the user what kind of partition it previously was before it was formatted or deleted. In the example below, the 2GB was formatted in ext3. Pressing Y will remove this warning to progress to the next step.

fdisk new partition

Once you’re satisfied with your configuration, press w to write the changes in the disk. The new partition is now applied, but it will not be visible on your file explorer yet.

writing to disk fdisk

You need to format the partition to make it usable. mkfs is one of the Linux commands you can use to create file systems. Replace “fs” with the filesystem that you prefer(ext, ext4, etc.) and replace “N” with the partition number.

$ sudo mkfs -t fs /dev/sdaN
formatting a disk partition

Once formatted properly, the new partition will automatically get mounted in the system. You can use it just like any other hard drive or USB.

Deleting a Partition using fdisk

Enter the fdisk command mode first to delete a partition. Use d to choose the partition number for deletion. Don’t forget to input w, reboot the system to make the changes sync to the hard drive.

Note: Before confirming w, you might want to double-check which partition number is set to be deleted. There is no way to reverse the changes, so always check your input or configuration before confirming. In a worst-case scenario, you can erase the partition that contains the bootloader, which will make your operating system inaccessible.

fdisk delete partition

Re-sizing an Existing Partition

The only way to resize a partition using fdisk is to back up your data, delete the partition(s), and re-create a new one with the desired capacity. It is the proven and trusted method in expanding and shrinking storage to avoid future complications.

Fixing Re-arranged Partition Table

The partition order might get re-arranged if the user deleted a logical partition. For example, if you remove sda3 between sda2 and sda4, the new partition will replace the deleted ones. However, it will still get the name sda5 and not sda3. Hence, you will get a partition table that looks like this: sda1 | sda2 | sda5 | sda4.

To fix this problem, access the expert command mode by inputting x. You will then have the option f to issue the command “fix partitions order.” Don’t forget to write the changes in the disk for it to take effect.

fixing partition order
fixing partition order

Final Thoughts

Although fdisk command in Linux was created almost 40 years ago, it is still a reliable way to modify disk partitions on Linux and UNIX devices. There are other disk utilities, including parted (or gparted for the GUI version) and gdisk, but fdisk is still reliable on modern devices less than 2TB in capacity. It’s still a good tool to look into, especially for older systems. If this guide helped you, please share it.

If this guide helped you, please share it. ?

Leave a Reply
Previous Article
How to Install ERPNext on Rocky Linux 9 4

How to Use Basename Command in Linux

Next Article
How to Install ERPNext on Rocky Linux 9 5

How to Use mount Command in Linux

Related Posts