How to Create and Manage Partitions Using `fdisk` in Debian 12 Bookworm
fdisk
in Debian 12 Bookworm.Categories:
3 minute read
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:
- Type
n
and press Enter to create a new partition. - Choose
p
for a primary partition ore
for an extended partition. - Enter the partition number (default is the next available number).
- Specify the first sector (press Enter to accept the default).
- 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:
- Type
w
and press Enter to save changes and exitfdisk
. - Run
sudo partprobe
orsudo 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:
Create a mount point:
sudo mkdir /mnt/new_partition
Mount the partition:
sudo mount /dev/sdb1 /mnt/new_partition
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
:
Open
fdisk
for the disk:sudo fdisk /dev/sdb
Type
d
and press Enter.Choose the partition number to delete.
Type
w
to save changes and exit.
Changing Partition Type
If you need to change a partition’s type (e.g., swap, LVM, etc.):
Open
fdisk
:sudo fdisk /dev/sdb
Type
t
and press Enter.Enter the partition number.
Enter the type code (e.g.,
82
for swap,8e
for LVM).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.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.