Fdisk Command - Linux Disk Partitioning Tool

Master the fdisk command for Linux disk partitioning. Learn to list, create, delete, and manage disk partitions effectively with practical examples.

Fdisk Command Guide

Understanding the Fdisk Command

The fdisk command is a powerful, text-based utility for manipulating disk partition tables in Linux and other Unix-like operating systems. It allows users to create, delete, modify, and manage partitions on hard drives, SSDs, and other block devices. Understanding fdisk is crucial for system administration, disk management, and troubleshooting storage issues.

Fdisk Command Syntax and Basic Usage

The primary function of fdisk is to interact with a specified disk device. Here's a breakdown of its common syntax and options:

# List partitions on a block device (e.g., /dev/sda)
fdisk -l [BLKDEV]

# General usage for changing partition table
fdisk [options] <disk>

# General usage for listing partition table(s)
fdisk [options] -l <disk>

# Get partition size(s) in blocks
fdisk -s <partition>

Common Fdisk Options

  • -b <size>: Sets the sector size (e.g., 512, 1024, 2048, 4096).
  • -c[=<mode>]: Enables compatible mode, either 'dos' or 'nondos' (default).
  • -h: Displays the help text.
  • -u[=<unit>]: Specifies the display units for sizes, either 'cylinders' or 'sectors' (default).
  • -v: Shows the program version.
  • -C <number>: Sets the number of cylinders.
  • -H <number>: Sets the number of heads.
  • -S <number>: Sets the number of sectors per track.

Interactive Fdisk Commands

Once you enter the interactive mode of fdisk (by running fdisk /dev/sdX where sdX is your disk), you can use several commands:

  • p: Displays the current partition table.
  • d: Deletes a partition. You will be prompted to enter the partition number.
  • l or L: Lists all known partition types.
  • n: Creates a new partition. You'll be guided through specifying the partition type, number, first sector, and last sector (or size).
  • t: Changes the type of a partition. You'll need to know the partition type code.
  • w: Writes the changes to the disk and exits. Use with caution!
  • q: Quits without saving changes.

Managing New and Existing Disks

When adding a new physical disk or extending an existing one (common in virtual machine environments), fdisk plays a key role. Here's a typical workflow:

Adding a New Physical Disk

  1. Identify the new disk: Use lsblk to see available block devices and identify the new disk (e.g., /dev/sdd).
  2. Start fdisk: Run fdisk /dev/sdd.
  3. Create a new partition: Use the n command to create a primary or extended partition. Follow the prompts to define its size and location.
  4. Set partition type (if necessary): Use the t command if you need to change the partition type.
  5. Write changes: Use the w command to save the partition table.
  6. Format the new partition: Use a filesystem command like mkfs.xfs /dev/sdd1 or mkfs.ext4 /dev/sdd1.
  7. Get the UUID: Use blkid /dev/sdd1 to find the partition's UUID.
  8. Update /etc/fstab: Add an entry to /etc/fstab to mount the new partition automatically on boot. For example: UUID=<blkid_output> /mnt/new_drive xfs defaults 0 0.
  9. Mount the partition: Use mount -a to mount all filesystems listed in /etc/fstab, or mount /mnt/new_drive.
  10. Verify: Use df -h /mnt/new_drive to check if it's mounted and showing the correct size.

Extending a Physical Disk (VM Example)

This process often involves resizing the virtual disk in your hypervisor, then using fdisk and filesystem tools to expand the partition:

  1. Unmount the drive: Comment out the relevant entry in /etc/fstab and unmount the partition (e.g., umount /dev/sdc1).
  2. Increase drive physical space: This is done at the hypervisor level (e.g., VMware, VirtualBox).
  3. Verify disk size: Use parted /dev/sdc -p to see the disk's reported size.
  4. Use fdisk to recreate the partition:
    • Run fdisk /dev/sdc.
    • Press p to print the current partition table. Note the start sector of the partition you want to extend.
    • Press d to delete the existing partition.
    • Press n to create a new partition. Choose the same partition number and ensure the start sector matches the old partition. For the end sector, accept the default to use the maximum available space.
    • Press w to write the changes.
  5. Reboot (sometimes necessary): A reboot might be required for the kernel to recognize the partition table changes.
  6. Check filesystem integrity: Run e2fsck -f /dev/sdc1 (replace sdc1 with your partition).
  7. Resize the filesystem: Use resize2fs /dev/sdc1.
  8. Mount the partition: Uncomment the entry in /etc/fstab and mount the partition (e.g., mount /dev/sdc1).
  9. Verify: Check the size with df -h /path/to/mountpoint.

External Resources