<span>fdisk</span> is a commonly used command-line tool in Linux for disk partition management. It can create, delete, view, and modify disk partition tables, suitable for MBR (Master Boot Record) formatted disks. For GPT formatted disks, it is recommended to use <span>gdisk</span> or <span>parted</span>.
🔧 Basic Usage
sudo fdisk [options] [device]
For example:
sudo fdisk /dev/sda
⚠️ Note: Modifying disk partitions is a high-risk operation, please ensure to back up important data before proceeding!
📌 Common Command Descriptions
After entering the <span>fdisk</span> interactive interface, input the following commands to perform operations:
| Command | Description |
|---|---|
<span>m</span> |
Display help menu |
<span>p</span> |
Print the current disk’s partition table |
<span>n</span> |
Create a new partition (primary/extended/logical) |
<span>d</span> |
Delete a partition |
<span>t</span> |
Change partition type (e.g., Linux, swap, LVM, etc.) |
<span>w</span> |
Write changes to disk and exit |
<span>q</span> |
Exit without saving changes |
<span>v</span> |
Check for free space or errors on the disk |
🧱 Partition Type Descriptions
- Primary Partition: Supports up to 4.
- Extended Partition: Can contain multiple logical partitions, only one can exist.
- Logical Partition: Must be contained within an extended partition.
✅ Example: Create a New Partition
sudo fdisk /dev/sda
After entering the interactive interface, input the following in sequence:
<span>p</span>to view existing partitions.<span>n</span>to create a new partition:
- Select
<span>p</span>(primary) or<span>l</span>(logical). - Input the partition number (e.g., 1~4).
- Set the starting sector (usually press enter to use the default).
- Set the ending sector (can input +10G to allocate 10GB).
<span>t</span> (optional) to change the partition type (e.g., 82 for swap).<span>w</span> to save and exit.📁 Notes
- After completing the operation, use
<span>partprobe</span>or reboot to make the partition effective:
sudo partprobe
- Use
<span>mkfs</span>to create a filesystem:
sudo mkfs.ext4 /dev/sda5