Linux fdisk Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux fdisk command to manage disk partitions. The fdisk command is a powerful tool that allows you to create, delete, and resize disk partitions on your Linux system. The lab covers the purpose and syntax of the fdisk command, as well as practical examples of creating, deleting, and resizing partitions. This lab is suitable for users who need to manage the storage space on their Linux systems.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/SystemInformationandMonitoringGroup -.-> linux/dd("`File Converting/Copying`") subgraph Lab Skills linux/ls -.-> lab-422678{{"`Linux fdisk Command with Practical Examples`"}} linux/sudo -.-> lab-422678{{"`Linux fdisk Command with Practical Examples`"}} linux/dd -.-> lab-422678{{"`Linux fdisk Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the fdisk Command

In this step, you will learn about the purpose and syntax of the fdisk command in Linux. The fdisk command is a command-line tool used to create, delete, and manage disk partitions.

To begin, let's start by understanding the purpose of the fdisk command. The fdisk command is used to create, delete, and modify disk partitions. It allows you to create new partitions, change the size of existing partitions, and change the type of partitions. This is useful when you need to manage the storage space on your Linux system.

Now, let's look at the basic syntax of the fdisk command:

sudo fdisk [options] [device]

Here's what the different parts of the command mean:

  • sudo: This is used to run the command with elevated privileges, as the fdisk command requires root access.
  • fdisk: This is the name of the command.
  • [options]: These are optional flags that you can use to customize the behavior of the fdisk command.
  • [device]: This is the name of the disk device that you want to work with, such as /dev/sda.

Some common options for the fdisk command include:

  • -l: Lists the partition table for the specified device.
  • -u: Uses sectors as the unit of measurement instead of cylinders.
  • -c=dos: Sets the compatible mode to be DOS.

Example output:

$ sudo fdisk -l /dev/sda
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start      End  Sectors  Size Id Type
/dev/sda1        2048 41943039 41940992   20G 83 Linux

In this example, the fdisk -l /dev/sda command lists the partition table for the /dev/sda device, which is a 20 GB virtual disk with a single Linux partition.

Create a New Partition Using the fdisk Command

In this step, you will learn how to create a new partition on your Linux system using the fdisk command.

First, let's list the current partitions on the /dev/sda device:

$ sudo fdisk -l /dev/sda
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start      End  Sectors  Size Id Type
/dev/sda1        2048 41943039 41940992   20G 83 Linux

As you can see, there is currently one partition, /dev/sda1, which takes up the entire 20 GB disk.

Now, let's create a new partition using the fdisk command:

$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2):
First sector (2048-41943039, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943039, default 41943039): +10G

Created a new partition 2 of type 'Linux' and of size 10 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

In this example, we:

  1. Entered the fdisk interactive mode by running sudo fdisk /dev/sda.
  2. Chose to create a new partition by pressing n.
  3. Selected a primary partition (p) as the partition type.
  4. Accepted the default partition number 2.
  5. Accepted the default first sector 2048.
  6. Specified the last sector as +10G to create a 10 GB partition.
  7. Wrote the changes to the disk by pressing w.

Example output:

$ sudo fdisk -l /dev/sda
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start       End   Sectors   Size Id Type
/dev/sda1        2048  41943039  41940992    20G 83 Linux
/dev/sda2    41943040  61071359  19128320    10G 83 Linux

As you can see, the new partition /dev/sda2 has been created with a size of 10 GB.

Delete and Resize Partitions with the fdisk Command

In this step, you will learn how to delete and resize partitions using the fdisk command.

First, let's list the current partitions on the /dev/sda device:

$ sudo fdisk -l /dev/sda
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start       End   Sectors   Size Id Type
/dev/sda1        2048  41943039  41940992    20G 83 Linux
/dev/sda2    41943040  61071359  19128320    10G 83 Linux

Now, let's delete the /dev/sda2 partition:

$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

In this example, we:

  1. Entered the fdisk interactive mode by running sudo fdisk /dev/sda.
  2. Chose to delete a partition by pressing d.
  3. Specified the partition number 2 to delete the /dev/sda2 partition.
  4. Wrote the changes to the disk by pressing w.

Now, let's resize the remaining /dev/sda1 partition to use the entire disk:

$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): d
Partition number (1,2, default 1): 1

Partition 1 has been deleted.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943039, default 41943039):

Created a new partition 1 of type 'Linux' and of size 20 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

In this example, we:

  1. Deleted the /dev/sda1 partition by pressing d and entering 1 as the partition number.
  2. Created a new primary partition (p) by pressing n.
  3. Accepted the default partition number 1.
  4. Accepted the default first sector 2048.
  5. Accepted the default last sector 41943039 to use the entire 20 GB disk.
  6. Wrote the changes to the disk by pressing w.

Example output:

$ sudo fdisk -l /dev/sda
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start       End   Sectors   Size Id Type
/dev/sda1        2048  41943039  41940992    20G 83 Linux

As you can see, the /dev/sda1 partition now takes up the entire 20 GB disk.

Summary

In this lab, you learned about the purpose and syntax of the fdisk command in Linux. The fdisk command is a command-line tool used to create, delete, and manage disk partitions. You explored the basic syntax of the fdisk command, including the use of sudo to run the command with elevated privileges, the different options available, and how to list the partition table for a specific device. Additionally, you learned how to create a new partition using the fdisk command, as well as how to delete and resize existing partitions.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like