In this tutorial, we explain how to use FDISK command to manage Linux partitions
The management of the local hard drives of a computer is a task that implies excellent responsibility because in these discs is sensitive information of the user, the operating system is mounted, and the applications that we use can also be used as a backup copy many more tasks.
What is Fdisk?
The fdisk command is a text-based utility for viewing and managing hard drive partitions in Linux. It is one of the most powerful tools we can use to manage partitions.
Ubuntu, Linux Mint and other distributions derived from Ubuntu, the fdisk and mkfs commands must be preceded by sudo to be executed as the root user.
In the distributions that don't use sudo, we must first use the su command to get a Root Shell and then write each command without sudo.
For this tutorial, we will use Ubuntu 17.10.
List Current Linux Partitions
To list the partitions of the current hard disk we will execute the following line:
sudo fdisk -l
There we found detailed information such as
- Partition route
- ID and type
It is possible to add the name of a disk device to show only the partitions associated with it. For example, we can use the following command to show only the partitions on the first disk device:
sudo fdisk -l /dev/sda
How To Get & Use The Command Mode
To work with partitions of a hard disk, it will be necessary to enter the command mode.
For this it will be necessary to use the device name of a disk from the fdisk -l command.
The following command allows us to enter the command mode for the first disk device:
sudo fdisk /dev/sda
We can see that we access the command mode of fdisk:
In command mode, we will use single-letter commands to specify the actions we want to perform.
Enter the letter m and press Enter to see a list of the commands available for use:
Display the partition table
We can use the p parameter to print the current partition table in the terminal from the command mode:
How To Create a Linux Partition
This is one of the most common and practical tasks to manage hard drives.
For this, we will use the n command to create a new partition.
It is possible to create a logical or primary partition (l for logic op for primary).
A disk can only have four primary partitions.
Next, we will specify the sector of the disk in which you want the partition to start; we can press Enter to accept the default sector, which is the first free sector on the drive.
Finally, we specify the last sector of the partition on the disk.
If we want to exhaust all available space after the first sector, just press Enter.
You can also specify a specific size, such as + 5G for a partition of five gigabytes or + 512M for a partition of 512 megabytes.
If you do not specify a unit after the + sign, fdisk uses sectors as the unit.
For example, +10000 results at the end of the partition being 10000 sectors after its start.
We will use the command d to eliminate a partition, at the moment of its execution, we will be asked for the number of the partition that we want to remove, which can be obtained from the p command.
For example, if I wanted to delete partition 1 in / dev / sdb, we enter the respective number:
If we execute the p command again, we will see that the selected partition is a “Linux” partition, now we see it in the Type column:
If we want to change its type, we can use the command t and specify the partition number. We will be asked for the hexadecimal code of the type, if we do not know, we can write L to see a list of hexadecimal codes:
There we must enter the desired number, for example, if we want the type of partition to be a swap, enter the number 82 and press Enter:
This will not format the partition with the selected file system. We must do this later by executing mkfs.
- We will use w to write the changes that have been made to the disk
- We will use q if you want to exit fdisk without saving the changes
How to Format a Linux Partition
You need to format new partitions with a file system before you can use them. This can be done with the appropriate mkfs command.
The syntax to use is:
- sudo mkfs.(Type) Path
The options for type are
In this case, we will format the partition /dev/sdb with ext4:
sudo mkfs.ext4 /dev/sdb
If we want to use that partition as swap, we must execute the following line:
sudo mkswap /dev/sdb
We see how fdisk becomes an ally when it comes to managing disk partitions in Linux.