cfdisk Command in Linux

Introduction

This tutorial delves into the Linux cfdisk command, an essential tool for disk partitioning and management. You'll learn the fundamentals of the cfdisk command, how to partition a disk effectively using cfdisk, and how to manage disk partitions. Practical examples and clear, step-by-step instructions will empower you to understand and utilize the cfdisk command with confidence as a systemadmin.

Introduction to cfdisk Command

This section introduces the cfdisk command, a potent utility for managing disk drives in a Linux environment. The cfdisk command provides an intuitive, interactive interface for creating, deleting, and modifying disk partitions, making it an indispensable tool for any systemadmin working with Linux.

First, identify the available disk devices on your system. The lsblk command lists all 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 /

The example shows a single disk device, sda, with one partition, sda1, mounted as the root file system (/).

Launch the cfdisk utility to manage disk partitions:

sudo cfdisk

This command opens the cfdisk interactive interface, enabling you to perform disk partitioning tasks.

The cfdisk interface is structured into these sections:

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

Use the arrow keys to navigate partitions. Function keys (F1-F6) perform actions like creating, deleting, or modifying partitions.

To create a new partition, select the New option (typically F2) and follow the on-screen prompts to define partition size and type.

After partitioning, write the changes to the disk by selecting the Write option (usually F6) and confirming.

Remember, cfdisk operates directly on the disk. Exercise caution when making changes to prevent accidental data loss or modification. Understanding the risk is very important for any systemadmin.

Partitioning a Disk using cfdisk

This section outlines how to partition a disk within a Linux environment using the cfdisk command. This is a very typical task for a systemadmin.

Create a new virtual disk for partitioning. 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 creates a 1GB disk image file named disk.img in the ~/project directory.

Use cfdisk to partition the disk:

sudo cfdisk ~/project/disk.img

This opens the cfdisk interface for creating 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.

After creating the desired partitions, press the Write function key (usually F6) to save changes to the disk.

Verify the created partitions:

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

The output confirms a 1GB Linux partition on the disk.

Managing Disk Partitions with cfdisk

This section details how to manage disk partitions using the cfdisk command. This is crucial knowledge for any aspiring systemadmin.

Launch the cfdisk utility on the disk image created earlier:

sudo cfdisk ~/project/disk.img

The cfdisk interface displays the previously created partition.

Perform common partition management tasks:

  1. Deleting a Partition:

    • Use the arrow keys to select the partition 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 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 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 changes, press the Write function key (usually F6) to save them to the disk.
    • Confirm the write operation by typing "yes" and pressing Enter. Requires root privileges.

Changes to disk partitions using cfdisk are applied only after selecting the Write option and confirming the operation. Always double check before writing any changes to your disk as root!

Exit cfdisk by selecting the Quit option.

Summary

This tutorial explored the cfdisk command, a powerful tool for disk partitioning and management in Linux. You learned how to identify disk devices with the lsblk command, launch the cfdisk utility, and navigate its interface. The tutorial covered creating, deleting, and modifying disk partitions using cfdisk's options and function keys. Finally, it highlighted the importance of caution when modifying disks to prevent data loss. This is important knowledge for anyone working as systemadmin in a Linux environment. Always back up your data before doing any partitioning as root!

400+ Linux Commands