fdisk is a command-line utility for disk partitioning. Used to create, delete, and manage partitions on storage devices.
Basic Usage
- fdisk -l - List all partitions on all disks
- fdisk -l /dev/sda - List partitions on specific disk
- sudo fdisk /dev/sda - Open fdisk for disk (interactive mode)
Interactive Commands
Once in fdisk (type `m` for menu):
- p - Print partition table
- n - Create new partition
- d - Delete partition
- t - Change partition type
- w - Write changes and exit
- q - Quit without saving
- l - List known partition types
- v - Verify partition table
- u - Change units (sectors/cylinders)
- m - Print menu
- a - Toggle bootable flag
Common Operations
Create New Partition
- `sudo fdisk /dev/sda`
- `n` - New partition
- `p` - Primary partition (or `e` for extended)
- Choose partition number (1-4)
- Enter first sector (default is fine)
- Enter last sector or size (e.g., `+10G` for 10GB)
- `w` - Write changes
Delete Partition
- `sudo fdisk /dev/sda`
- `p` - View partitions
- `d` - Delete partition
- Select partition number
- `w` - Write changes
Change Partition Type
- `sudo fdisk /dev/sda`
- `t` - Change type
- Select partition
- Enter type code (e.g., `83` for Linux, `82` for swap)
- `w` - Write changes
Partition Type Codes
- 82 - Linux swap
- 83 - Linux filesystem
- 85 - Linux extended
- 8e - Linux LVM
- ef - EFI system partition
- Use `l` in fdisk to see full list
After Partitioning
After creating partitions, you may need to:
- `partprobe` - Inform OS of partition table changes
- `mkfs.ext4 /dev/sda1` - Format partition as ext4
- `mount /dev/sda1 /mnt` - Mount partition
- Add to `/etc/fstab` for permanent mounting
Related Commands
- `lsblk` - List block devices in tree format
- `blkid` - Show block device attributes
- `parted` - Alternative partitioning tool (GPT support)
- `cfdisk` - Curses-based disk partitioner
- `gdisk` - GPT fdisk (for GPT partition tables)
Configuration
Config File Location
- `/etc/fstab` - File system table (mount points)
- `/proc/partitions` - Current partition information
Tips
- Always use `-l` to list first before making changes
- Write changes (`w`) only when you're sure
- Use `q` to quit without saving if you make a mistake
- Backup important data before partitioning
- Use `partprobe` or reboot after partitioning
- For GPT disks, use `gdisk` instead of `fdisk`
- Be very careful - partitioning can destroy data
- Test changes on non-critical systems first
- Use `cfdisk` for easier interactive interface