This tutorial dives deep into advanced fdisk skills for system administrators. You’ll learn how to manage disk partitions, optimize storage, and troubleshoot common issues using fdisk. By the end, you’ll be equipped to handle complex disk management tasks in real-world scenarios.
Step 1: Understanding fdisk Basics
Before diving into advanced skills, ensure you understand the basics of fdisk. It's a command-line utility for managing disk partitions on Linux systems. Key commands include:
- fdisk -l: List all partitions
- fdisk /dev/sdX: Start fdisk for a specific disk
- m: Display the help menu
- p: Print the partition table
- q: Quit without saving changes
- w: Write changes and exit
fdisk -lPractice Exercise
List all partitions on your system and identify the disk with the most free space. Write down its device name (e.g., /dev/sda).
Show Solution
fdisk -l
# Output will show all disks and partitions. Identify the disk with the most free space.
# Example: /dev/sda has the most free space.Step 2: Creating and Deleting Partitions
Learn how to create and delete partitions using fdisk.
- Use n to create a new partition.
- Specify the partition type (primary or extended).
- Set the start and end sectors.
- Use d to delete a partition.
- Always verify changes with p before writing them with w.
fdisk /dev/sdX
n
p
1
+10G
wPractice Exercise
Create a 5GB primary partition on /dev/sdb. Then, delete it and verify the changes.
Show Solution
fdisk /dev/sdb
n
p
1
+5G
w
# Verify with `fdisk -l /dev/sdb`
# Delete the partition:
fdisk /dev/sdb
d
1
w
# Verify again with `fdisk -l /dev/sdb`Step 3: Changing Partition Types
Partition types define how the partition is used (e.g., Linux filesystem, swap). Use t in fdisk to change the type.
- List available types with L.
- Set the type using its hex code (e.g., 83 for Linux, 82 for swap).
fdisk /dev/sdX
t
1
82
wPractice Exercise
Change the type of the first partition on /dev/sdc to a Linux swap partition (type 82). Verify the change.
Show Solution
fdisk /dev/sdc
t
1
82
w
# Verify with `fdisk -l /dev/sdc`
# The partition type should now show as 'Linux swap'.Step 4: Resizing Partitions
Resizing partitions is a critical skill for optimizing disk space. While fdisk doesn't directly resize partitions, you can delete and recreate them with new sizes. Always back up data before resizing.
- Delete the partition with d.
- Recreate it with n, specifying the new size.
- Use tools like resize2fs to resize the filesystem afterward.
fdisk /dev/sdX
d
1
n
p
1
+20G
w
resize2fs /dev/sdX1Practice Exercise
Resize the first partition on /dev/sdd from 10GB to 15GB. Assume the filesystem is ext4.
Show Solution
fdisk /dev/sdd
d
1
n
p
1
+15G
w
resize2fs /dev/sdd1
# Verify with `df -h`Step 5: Working with GPT Partitions
Modern systems often use GPT (GUID Partition Table) instead of MBR. Use gdisk or fdisk (on newer versions) to manage GPT partitions.
- Key differences:
- GPT supports more partitions.
- GPT uses unique identifiers (GUIDs).
- Use gdisk for advanced GPT features.
gdisk /dev/sdXPractice Exercise
Create a GPT partition table on /dev/sde and add a 10GB partition. Verify the changes.
Show Solution
gdisk /dev/sde
o
n
+10G
w
# Verify with `gdisk -l /dev/sde`Step 6: Recovering Lost Partitions
Accidentally deleted a partition? Use fdisk to recover it.
- Use p to view the partition table.
- Look for unused space.
- Recreate the partition with the same start and end sectors.
- Use tools like testdisk for advanced recovery.
fdisk /dev/sdX
n
p
1
wPractice Exercise
Simulate a partition deletion on /dev/sdf. Recreate it using the same start and end sectors.
Show Solution
fdisk /dev/sdf
d
1
n
p
1
w
# Verify with `fdisk -l /dev/sdf`Step 7: Automating fdisk with Scripts
Automate repetitive tasks with shell scripts. Use echo to pass commands to fdisk.
- Example: Create a script to partition a disk with a single command.
echo -e 'n
p
1
+10G
w' | fdisk /dev/sdXPractice Exercise
Write a script to create a 5GB primary partition and a 2GB swap partition on /dev/sdg.
Show Solution
#!/bin/bash
echo -e 'n
p
1
+5G
n
p
2
+2G
t
2
82
w' | fdisk /dev/sdg
# Run the script and verify with `fdisk -l /dev/sdg`Step 8: Troubleshooting Common Issues
Common issues include:
- Disk not recognized: Check kernel logs with dmesg.
- Partition table corruption: Use fsck or testdisk.
- Misaligned partitions: Ensure proper sector alignment for performance.
dmesg | grep sdX
fsck /dev/sdX1Practice Exercise
Simulate a corrupted partition table on /dev/sdh. Use fsck to check and repair the filesystem.
Show Solution
# Simulate corruption (optional):
dd if=/dev/zero of=/dev/sdh bs=512 count=1
# Check and repair:
fsck /dev/sdh1
# Verify with `fdisk -l /dev/sdh`Sign in to take Cornell notes on this lesson — they save automatically and stay with your account.