Linux cfdisk Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux cfdisk command, a powerful tool for partitioning and managing disk drives. The lab will cover the introduction to the cfdisk command, partitioning a disk using cfdisk, and managing disk partitions with cfdisk. We will provide practical examples and step-by-step instructions to help you understand and utilize the cfdisk command effectively.

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/rm("`File Removing`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/SystemInformationandMonitoringGroup -.-> linux/dd("`File Converting/Copying`") subgraph Lab Skills linux/rm -.-> lab-422592{{"`Linux cfdisk Command with Practical Examples`"}} linux/sudo -.-> lab-422592{{"`Linux cfdisk Command with Practical Examples`"}} linux/dd -.-> lab-422592{{"`Linux cfdisk Command with Practical Examples`"}} end

Introduction to cfdisk Command

In this step, we will explore the cfdisk command, a powerful tool for partitioning and managing disk drives in Linux. The cfdisk command provides a user-friendly, interactive interface for creating, deleting, and modifying disk partitions.

To begin, let's start by checking the available disk devices on our system. We can use the lsblk command to list all the block devices:

sudo lsblk

Example output:

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk
└─sda1   8:1    0   20G  0 part /

As you can see, our system has a single disk device named sda with one partition sda1 mounted as the root file system (/).

Now, let's launch the cfdisk utility to manage the disk partitions:

sudo cfdisk

This will open the cfdisk interactive interface, where you can perform various disk partitioning tasks.

The cfdisk interface is divided into several sections:

  • The top section displays the disk information, including the disk name, size, and partition table type.
  • The middle section shows the existing partitions and their details, such as the partition name, size, type, and flags.
  • The bottom section displays the available commands and options.

You can use the arrow keys to navigate through the partitions, and the function keys (F1-F6) to perform various actions, such as creating, deleting, or modifying partitions.

For example, to create a new partition, press the New option (usually F2) and follow the on-screen instructions to specify the partition size and type.

When you're done with the partitioning tasks, you can write the changes to the disk by selecting the Write option (usually F6) and confirming the action.

Remember, the cfdisk command operates directly on the disk, so be careful when making changes to ensure you don't accidentally delete or modify important data.

Partitioning a Disk using cfdisk

In this step, we will use the cfdisk command to partition a disk in our Linux environment.

First, let's create a new virtual disk that we can use for partitioning. We can use the dd command to create a 1GB disk image file:

sudo dd if=/dev/zero of=~/project/disk.img bs=1M count=1000

This will create a 1GB disk image file named disk.img in the ~/project directory.

Now, let's use cfdisk to partition the disk:

sudo cfdisk ~/project/disk.img

This will open the cfdisk interface, where we can create new partitions.

To create a new partition, follow these steps:

  1. Use the arrow keys to select the "Free Space" option.
  2. Press the New function key (usually F2) to create a new partition.
  3. Specify the partition size (in MB) and press Enter.
  4. Select the partition type (e.g., Linux, Linux swap, etc.) and press Enter.

Once you have created the desired partitions, press the Write function key (usually F6) to save the changes to the disk.

Finally, let's verify the partitions we created:

sudo fdisk -l ~/project/disk.img

Example output:

Disk ~/project/disk.img: 1 GiB, 1073741824 bytes, 2097152 sectors
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: 0x4f3d8c9d

Device             Start       End   Sectors   Size Type
~/project/disk.img1     2048  2097151  2095104   1G Linux

As you can see, we have successfully created a 1GB Linux partition on the disk.

Managing Disk Partitions with cfdisk

In this step, we will explore how to manage disk partitions using the cfdisk command.

First, let's launch the cfdisk utility on the disk image we created in the previous step:

sudo cfdisk ~/project/disk.img

This will open the cfdisk interface, where we can see the partition we created earlier.

Now, let's try some common partition management tasks:

  1. Deleting a Partition:

    • Use the arrow keys to select the partition you want to delete.
    • Press the Delete function key (usually F3) to delete the selected partition.
    • Confirm the deletion by selecting the Yes option.
  2. Changing Partition Type:

    • Use the arrow keys to select the partition you want to modify.
    • Press the Type function key (usually F4) to change the partition type.
    • Select the desired partition type and press Enter.
  3. Resizing a Partition:

    • Use the arrow keys to select the partition you want to resize.
    • Press the Resize function key (usually F5) to resize the partition.
    • Specify the new size for the partition and press Enter.
  4. Writing Changes to Disk:

    • After making the desired changes, press the Write function key (usually F6) to save the changes to the disk.
    • Confirm the write operation by typing "yes" and pressing Enter.

Remember, the changes you make to the disk partitions using cfdisk will only be applied after you select the Write option and confirm the operation.

Once you're done with the partition management tasks, you can exit cfdisk by selecting the Quit option.

Summary

In this lab, we explored the cfdisk command, a powerful tool for partitioning and managing disk drives in Linux. We learned how to check the available disk devices using the lsblk command, launch the cfdisk utility, and navigate through its interactive interface. We also discovered how to create, delete, and modify disk partitions using the various options and function keys provided by cfdisk. Finally, we learned the importance of being cautious when making changes to the disk to avoid accidentally deleting or modifying important data.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like