+4 votes
325 views
How to use Fdisk command to manage Linux partitions

in Linux / Unix by (552k points)
reopened | 325 views

1 Answer

+5 votes
Best answer

1. List current Linux partitions
2. How to access and use Linux Command mode
3. How to create a Linux partition
4. How to format a Linux partition

Management of local hard drives of a computer is a task that involves great responsibility because on those disks is sensitive information from the user, is mounted the operating system and applications we use also can be used as backup many more tasks..

 

In Linux operating systems the Fdisk utility allows us to perform various actions on hard drives such as:

  • Create and edit new partitions
  • Delete partitions
  • Modify file system and more.

 

TechnoWikis will do a detailed analysis on how to use fdisk in Linux and in this way have better control over the hard drive partitions .

 

What is Fdisk
The fdisk command is a text-based utility for viewing and managing hard drive partitions on Linux. It is one of the most powerful tools that we can use to manage partitions.

 

The options to use with fdisk, some of which we will cover later are:

 

Delete a partition
 d 

List the current partition types in the system
 I 

Display menu options
 m 

Create a new partition on the hard drive
 n 

Deploy current partitions
 p 

Exit fdisk without saving modifications
 what 

Change the selected partition type
 t 

Parse the partition table
 v 

Save changes and exit fdisk
 w 

 

In Ubuntu, Linux Mint, and other Ubuntu-derived distributions, the fdisk and mkfs commands must be preceded by sudo to be run as the root user. In distributions that do not use sudo, we must first use the su command to get a root Shell, and then type each command without sudo..

 

In this case we will use Ubuntu 17.10.

 

 


1. List current Linux partitions

 

Step 1

To list the partitions of the current hard disk we will execute the following line:
 sudo fdisk -l 
image

 

Step 2

In case of having some type of partition we will see the following:

 

image

 

There we find detailed information such as
  • Partition path
  • Used size
  • Sectors
  • ID and type

 

Step 3

You can 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 
image

 

 


2. How to access and use Linux Command mode


To work on the partitions of a hard disk, it will be necessary to enter the command mode. This will require using the device name of a disk from the fdisk -l command. The following command allows us to enter command mode for the first disk device:
 sudo fdisk / dev / sda 
We can see that we access the command mode of fdisk:

 

 

image

 

Note
Partitions cannot be edited while in use. If we want to edit system partitions, we must first boot from a live CD.

 

In command mode, we will use single letter commands to specify the actions we want to carry out. We enter the letter m and press Enter to see a list of the commands available for use:

 

image

 

View the partition table
We can use the p parameter to print the current partition table to the terminal from command mode:

 

image

 

 

 


3. How to create a Linux partition

 

Step 1

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 disk..

 

Finally, we specify the last sector of the partition on the disk. If we want to use up all the available space after the initial sector, just press Enter. You can also specify a specific size, such as + 5G for a five gigabyte partition or + 512M for a 512 megabyte partition. If you don't specify a drive after the + sign, fdisk uses sectors as the drive. For example, +10000 results in the end of the partition being 10,000 sectors after its start.

 

image

 

Step 2

We will use the d command to delete a partition, at the time of its execution, we will be asked for the number of the partition that we want to delete, which can be obtained from the p command. For example, if you wanted to delete partition 1 in / dev / sdb, we enter the respective number:

 

image

 

Step 3

If we run the p command again, we will see that the selected partition is a "Linux" partition, now we see this in the Type column:

 

image

 

Step 4

If we want to change its type, we can use the command t and specify the partition number. We will be asked for the hex code of the type, if we do not know it, we can type L to see a list of hexadecimal codes:

 

image

 

Step 5

There we must enter the desired number, for example, if we want the partition type to be swap, or swap, we enter the number 82 and press Enter:

 

image

 

Step 6

This will not format the partition with the selected file system. This must be done later by running mkfs.

 

Write changes
  • We will use w to write the changes that have been made to disk
  • We will use q if you want to exit fdisk without saving changes

 

 

 

image

 

 


4. How to format a Linux partition


New partitions must be formatted with a file system before they can be used. This can be done with the appropriate mkfs command.

 

image

 

The syntax to use is:

 sudo mkfs. (Type) Route 
Options for type are
  • bfs
  • cramfs
  • ext2
  • ext3
  • ext4
  • ext4dev
  • minix
  • msdos
  • ntfs
  • wat

 

In this case we will format the / dev / sdb partition with ext4:

 sudo mkfs.ext4 / dev / sdb 
image

 

If we want to use that partition as an exchange, or 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.

 


by (3.5m points)

Related questions

+5 votes
1 answer
asked Sep 25, 2019 in Linux / Unix by backtothefuture (552k points) | 268 views
+3 votes
1 answer
+3 votes
1 answer
asked Oct 20, 2020 in Linux / Unix by backtothefuture (552k points) | 241 views
+4 votes
1 answer
asked Sep 22, 2020 in Linux / Unix by backtothefuture (552k points) | 334 views
+3 votes
1 answer
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

10,659 questions
10,791 answers
510 comments
3 users