How to Create and Manage Partitions Using `fdisk` in Debian 12 Bookworm

This article provides a step-by-step guide on how to create and manage partitions using fdisk in Debian 12 Bookworm.

Partitioning a disk is a crucial step in setting up and managing storage on a Debian 12 Bookworm system. fdisk is a command-line tool that allows users to create, modify, and delete partitions on a storage device. This guide provides a detailed walkthrough on how to use fdisk to create and manage partitions effectively in Debian 12.

Prerequisites

Before proceeding, ensure that you have the following:

  • A Debian 12 Bookworm system.
  • Root or sudo privileges to execute administrative commands.
  • A basic understanding of Linux disk management concepts.

Understanding fdisk

fdisk is a command-line utility that helps manage disk partitions in Linux systems. It allows users to:

  • Create new partitions.
  • Delete existing partitions.
  • Change partition types.
  • View partition details.

fdisk works with disk devices such as /dev/sda, /dev/sdb, etc. Ensure that you identify the correct disk before making changes.

Checking Available Disks and Partitions

Before making any modifications, check the available disks and their partition layouts:

sudo fdisk -l

This command lists all the disks, partitions, and their details, including size, partition type, and file system.

Creating a New Partition

Step 1: Select the Disk

Run the following command to open fdisk for the target disk (e.g., /dev/sdb):

sudo fdisk /dev/sdb

Step 2: Create a New Partition

Once inside fdisk, follow these steps:

  1. Type n and press Enter to create a new partition.
  2. Choose p for a primary partition or e for an extended partition.
  3. Enter the partition number (default is the next available number).
  4. Specify the first sector (press Enter to accept the default).
  5. Specify the last sector or size (e.g., +20G for a 20GB partition).

Step 3: Write Changes to Disk

Once the partition is created, write the changes:

  1. Type w and press Enter to save changes and exit fdisk.
  2. Run sudo partprobe or sudo reboot to refresh the partition table.

Formatting the Partition

After creating a partition, format it with a suitable file system. For example, to format the new partition (/dev/sdb1) as ext4:

sudo mkfs.ext4 /dev/sdb1

For other file systems, use:

  • sudo mkfs.xfs /dev/sdb1 (XFS)
  • sudo mkfs.vfat /dev/sdb1 (FAT32)
  • sudo mkfs.ntfs /dev/sdb1 (NTFS)

Mounting the Partition

To use the newly created partition, mount it:

  1. Create a mount point:

    sudo mkdir /mnt/new_partition
    
  2. Mount the partition:

    sudo mount /dev/sdb1 /mnt/new_partition
    
  3. Verify the mount:

    df -h
    

Automounting the Partition

To ensure the partition is mounted automatically at boot, add it to /etc/fstab:

echo "/dev/sdb1 /mnt/new_partition ext4 defaults 0 2" | sudo tee -a /etc/fstab

Replace ext4 with the appropriate file system type.

Deleting a Partition

To delete a partition using fdisk:

  1. Open fdisk for the disk:

    sudo fdisk /dev/sdb
    
  2. Type d and press Enter.

  3. Choose the partition number to delete.

  4. Type w to save changes and exit.

Changing Partition Type

If you need to change a partition’s type (e.g., swap, LVM, etc.):

  1. Open fdisk:

    sudo fdisk /dev/sdb
    
  2. Type t and press Enter.

  3. Enter the partition number.

  4. Enter the type code (e.g., 82 for swap, 8e for LVM).

  5. Type w to save changes.

Resizing a Partition

If you need to resize a partition, you may have to use resize2fs (for ext4) along with fdisk. First, unmount the partition:

sudo umount /dev/sdb1

Then, delete and recreate the partition with the new size. Finally, use:

sudo resize2fs /dev/sdb1

Conclusion

Using fdisk to create and manage partitions in Debian 12 Bookworm is a straightforward process. By following these steps, you can efficiently handle disk partitions, ensuring optimal disk usage and system performance.