This tutorial builds on basic fdisk knowledge to teach intermediate skills for managing disk partitions. You’ll learn advanced partitioning techniques, troubleshooting, and real-world scenarios for system administrators.

Intermediate30 minutes

Step 1: Review Basic fdisk Commands

Before diving into advanced topics, let's review the basics of fdisk. It's a command-line utility for managing disk partitions on Linux systems. Key commands include:

- p: Print the partition table

- n: Create a new partition

- d: Delete a partition

- w: Write changes to disk

- q: Quit without saving changes

sudo fdisk /dev/sda

Practice Exercise

Use fdisk to list the partition table of /dev/sdb. Identify the number of partitions and their sizes.

Show Solution
sudo fdisk -l /dev/sdb
# The output will display the partition table, including partition numbers, sizes, and types.

Step 2: Create a New Partition with Specific Size

Learn how to create a partition with a specific size. For example, you might need to allocate 10GB for a new data partition.

  • Start fdisk on the target disk.
  • Use the n command to create a new partition.
  • Specify the partition type (primary or extended).
  • Set the start and end sectors or specify the size directly (e.g., +10G).
sudo fdisk /dev/sda
n
p
1

+10G
w

Practice Exercise

Create a 5GB primary partition on /dev/sdc. Verify the partition was created successfully.

Show Solution
sudo fdisk /dev/sdc
n
p
1

+5G
w
# Verify with:
sudo fdisk -l /dev/sdc
# Look for the new 5GB partition in the output.

Step 3: Change Partition Types

Sometimes, you need to change a partition's type (e.g., from Linux to swap). Use the t command in fdisk to modify the partition type.

  • List the partition table to identify the partition number.
  • Use the t command and specify the partition number.
  • Enter the hex code for the desired partition type (e.g., 82 for swap).
sudo fdisk /dev/sda
t
1
82
w

Practice Exercise

Change the type of the second partition on /dev/sdb to swap (hex code 82). Verify the change.

Show Solution
sudo fdisk /dev/sdb
t
2
82
w
# Verify with:
sudo fdisk -l /dev/sdb
# Check the 'Type' column for the second partition.

Step 4: Resize Partitions Safely

Resizing partitions can be risky, but fdisk allows you to delete and recreate partitions with new sizes. Always back up data before proceeding.

  • Delete the partition you want to resize.
  • Create a new partition with the desired size.
  • Ensure the new partition starts at the same sector to avoid data loss.
sudo fdisk /dev/sda
d
1
n
p
1

+20G
w

Practice Exercise

Resize the first partition on /dev/sdc from 5GB to 8GB. Ensure no data is lost by starting at the same sector.

Show Solution
sudo fdisk /dev/sdc
d
1
n
p
1

+8G
w
# Replace  with the original start sector from the partition table.

Step 5: Recover Deleted Partitions

Accidentally deleted a partition? Use fdisk to recreate it with the exact same parameters (start sector, size, and type).

  • Note the details of the deleted partition from the partition table.
  • Recreate the partition with the same start sector and size.
  • Verify the partition is restored correctly.
sudo fdisk /dev/sda
n
p
1


t
1

w

Practice Exercise

Simulate a partition deletion on /dev/sdd and recover it using the original parameters.

Show Solution
# Simulate deletion:
sudo fdisk /dev/sdd
d
1
w
# Recover:
sudo fdisk /dev/sdd
n
p
1


t
1

w
# Replace , , and  with the original values.

Step 6: Advanced: Align Partitions for Performance

Proper partition alignment is crucial for performance, especially on SSDs. Use fdisk to align partitions to 1MB boundaries.

  • Start fdisk and create a new partition.
  • Set the start sector to a multiple of 2048 (1MB alignment).
  • Verify alignment using parted or fdisk.
sudo fdisk /dev/sda
n
p
1
2048
+10G
w

Practice Exercise

Create a 10GB partition on /dev/sde aligned to a 1MB boundary. Verify the alignment.

Show Solution
sudo fdisk /dev/sde
n
p
1
2048
+10G
w
# Verify alignment:
sudo parted /dev/sde align-check optimal 1
# Output should indicate proper alignment.

Step 7: Troubleshoot Partition Issues

Learn to diagnose and fix common partition issues, such as corrupted partition tables or misaligned partitions.

  • Use fdisk -l to inspect the partition table.
  • Check for overlapping partitions or incorrect sizes.
  • Recreate partitions if necessary.
sudo fdisk -l /dev/sda

Practice Exercise

Inspect /dev/sdf for partition issues. Identify any overlapping partitions and fix them.

Show Solution
sudo fdisk -l /dev/sdf
# Look for overlapping partitions in the output.
# If found, delete and recreate the affected partitions with correct parameters.

Step 8: Automate Partitioning with Scripts

Automate repetitive partitioning tasks using shell scripts. For example, create a script to partition a new disk with a standard layout.

  • Write a script that uses fdisk commands.
  • Use echo to pass input to fdisk.
  • Test the script on a non-critical disk.
echo -e 'n\np\n1\n\n+10G\nw' | sudo fdisk /dev/sda

Practice Exercise

Write a script to create a 5GB primary partition and a 10GB extended partition on /dev/sdg.

Show Solution
#!/bin/bash
echo -e 'n\np\n1\n\n+5G\nn\ne\n2\n\n+10G\nw' | sudo fdisk /dev/sdg
# Run the script and verify the partitions with `fdisk -l /dev/sdg`.

Sign in to take Cornell notes on this lesson — they save automatically and stay with your account.

Sign in

Click to access the login or register cheese