Introduction
Managing disks and partitions is a core skill for any system administrator or power user. Whether preparing a fresh server, resizing filesystems on-the-fly, or recovering from a corrupted table, fdisk and parted are two of the most versatile command-line utilities available on Linux.
Understanding Disk Partitioning
Master Boot Record (MBR)
- Sector limit: Up to 2 TB per disk.
- Partition table: Located in the first 512 bytes.
- Max partitions: Four primary, or three primary one extended.
- Legacy compatibility: Well-supported by older BIOS systems.
GUID Partition Table (GPT)
- Sector limit: Virtually unlimited (up to 9.4 ZB).
- Partition table: Redundant headers at beginning and end.
- Max partitions: Typically 128 by default.
- UEFI requirement: Standard on modern systems for booting.
fdisk: A Traditional MBR Tool (and GPT-aware on Newer Releases)
Invoking fdisk
To examine all connected block devices:
sudo fdisk -l
To work on a specific disk:
sudo fdisk /dev/sdX
Common Interactive Commands
| Command | Description |
|---|---|
p |
Print current partition table |
n |
Add a new partition |
d |
Delete a partition |
a |
Toggle bootable flag |
t |
Change partition type |
w |
Write table to disk and exit |
q |
Quit without saving changes |
Non-Interactive Scripting
You can pipe commands via echo or use a here-document. Example:
sudo fdisk /dev/sdX ltltEOF
n
p
1
10G
w
EOF
parted: Flexible GPT and MBR Management
Setting a Partition Table Label
parted supports both msdos (MBR) and gpt:
sudo parted /dev/sdX mklabel gpt
Interactive Mode
sudo parted /dev/sdX
(parted) print
(parted) mkpart primary ext4 1MiB 20GiB
(parted) set 1 boot on
(parted) quit
Non-Interactive Usage amp Units
By default, parted uses MiB and GiB. To work in sectors, bytes or percentages:
sudo parted -s /dev/sdX unit s mkpart primary ext4 2048 409600
Resizing Partitions and Filesystems
- Unmount the filesystem:
sudo umount /dev/sdX1 - Resize partition:
parted /dev/sdX resizepart 1 50GiB - Grow the filesystem: for ext4,
sudo resize2fs /dev/sdX1
Aligning for Performance
Modern SSDs and RAID arrays benefit from proper alignment. Use MiB boundaries:
(parted) mkpart primary ext4 1MiB 100%
Best Practices and Caution
- Backup first: Always snapshot or image disks before modifying partitions.
- Double-check device names: Mixing up
/dev/sdaand/dev/sdbcan be disastrous. - Sync and refresh: Run
sudo partprobeor reboot to ensure the kernel sees changes. - Filesystem check: After resizing, run
fsckto verify integrity.
Securing Remote Disk Management
When administering disks on remote servers, encryption and secure tunnels are vital. Typical workflow:
- SSH into the server:
ssh user@host - Use
sudo fdiskorsudo partedas above. - Ensure your connection is protected by a VPN:
Conclusion
Mastering fdisk and parted gives you full control over disk layout, offering legacy compatibility and modern flexibility. By combining careful planning, best practices, and secure remote access (e.g., via a reputable VPN), you can confidently partition, resize, and maintain storage on any Linux system.
Leave a Reply