How to edit partitions → fdisk

How to Edit Partitions Using fdisk: A Comprehensive Guide Table of Contents 1. [Introduction](#introduction) 2. [Prerequisites](#prerequisites) 3. [Understanding fdisk](#understanding-fdisk) 4. [Getting Started with fdisk](#getting-started-with-fdisk) 5. [Viewing Partition Information](#viewing-partition-information) 6. [Creating New Partitions](#creating-new-partitions) 7. [Deleting Partitions](#deleting-partitions) 8. [Modifying Partition Types](#modifying-partition-types) 9. [Advanced fdisk Operations](#advanced-fdisk-operations) 10. [Practical Examples](#practical-examples) 11. [Common Issues and Troubleshooting](#common-issues-and-troubleshooting) 12. [Best Practices and Safety Tips](#best-practices-and-safety-tips) 13. [Alternative Tools](#alternative-tools) 14. [Conclusion](#conclusion) Introduction The `fdisk` (fixed disk) utility is one of the most powerful and widely-used command-line tools for managing disk partitions in Linux and Unix-like operating systems. This comprehensive guide will teach you how to effectively use fdisk to view, create, modify, and delete disk partitions safely and efficiently. Whether you're a system administrator managing server storage, a developer setting up development environments, or a Linux enthusiast learning system administration, mastering fdisk is essential for effective disk management. This article covers everything from basic partition viewing to advanced partition manipulation techniques. By the end of this guide, you'll understand how to confidently use fdisk to manage your disk partitions, avoid common pitfalls, and implement best practices for safe partition management. Prerequisites Before diving into fdisk operations, ensure you have: System Requirements - A Linux or Unix-like operating system - Root or sudo privileges - Basic command-line knowledge - Understanding of disk and partition concepts Essential Knowledge - Partition Types: Primary, extended, and logical partitions - File Systems: ext4, NTFS, FAT32, swap, etc. - Disk Naming: Understanding `/dev/sda`, `/dev/sdb`, etc. - Backup Importance: Always backup critical data before partition operations Safety Preparations ```bash Always backup important data before proceeding sudo fdisk -l # List all disks to identify target disk lsblk # Display block devices in tree format df -h # Show mounted filesystems and usage ``` Understanding fdisk What is fdisk? fdisk is a command-line utility that manipulates disk partition tables. It supports various partition table formats, including: - MBR (Master Boot Record): Traditional partition table format - GPT (GUID Partition Table): Modern partition table format for UEFI systems Key Capabilities fdisk allows you to: - View existing partition tables - Create new partitions - Delete existing partitions - Change partition types - Set bootable flags - Verify partition table integrity Limitations Important limitations to consider: - Destructive Operations: Changes can result in data loss - MBR Limitations: Maximum 4 primary partitions, 2TB disk size limit - No Undo Function: Changes are permanent once written - Requires Unmounted Partitions: Cannot modify mounted filesystems Getting Started with fdisk Basic Syntax The basic fdisk syntax follows this pattern: ```bash fdisk [options] device ``` Common Options | Option | Description | |--------|-------------| | `-l` | List partition tables for all devices | | `-u` | Display partition sizes in sectors | | `-s` | Display partition size in blocks | | `-v` | Display fdisk version | Accessing fdisk To start fdisk on a specific disk: ```bash sudo fdisk /dev/sda ``` Interactive Mode Commands Once in fdisk interactive mode, use these commands: | Command | Description | |---------|-------------| | `m` | Display help menu | | `p` | Print partition table | | `n` | Create new partition | | `d` | Delete partition | | `t` | Change partition type | | `a` | Toggle bootable flag | | `w` | Write changes and exit | | `q` | Quit without saving | Viewing Partition Information Listing All Disks and Partitions To view all available disks and their partitions: ```bash sudo fdisk -l ``` Example output: ``` Disk /dev/sda: 500.1 GB, 500107862016 bytes 255 heads, 63 sectors/track, 60801 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes Device Boot Start End Blocks Id System /dev/sda1 * 1 2611 20971520 83 Linux /dev/sda2 2611 5222 20971520 83 Linux /dev/sda3 5222 60801 446676480 83 Linux ``` Viewing Specific Disk Information To examine a specific disk: ```bash sudo fdisk -l /dev/sda ``` Using Alternative Display Methods For a tree-like view of block devices: ```bash lsblk ``` For detailed partition information: ```bash sudo parted -l ``` Creating New Partitions Step-by-Step Partition Creation 1. Start fdisk on target disk: ```bash sudo fdisk /dev/sdb ``` 2. Display current partition table: ``` Command (m for help): p ``` 3. Create new partition: ``` Command (m for help): n ``` 4. Choose partition type: ``` Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p ``` 5. Select partition number: ``` Partition number (1-4, default 1): 1 ``` 6. Set starting sector (accept default or specify): ``` First sector (2048-1048575, default 2048): [Enter] ``` 7. Set ending sector or size: ``` Last sector, +sectors or +size{K,M,G} (2048-1048575, default 1048575): +10G ``` Creating Different Partition Types Primary Partition ```bash In fdisk interactive mode n # New partition p # Primary partition 1 # Partition number [Enter] # Accept default first sector +20G # Set size to 20GB ``` Extended Partition ```bash In fdisk interactive mode n # New partition e # Extended partition 3 # Partition number [Enter] # Accept default first sector [Enter] # Use remaining space ``` Logical Partition ```bash After creating extended partition n # New partition l # Logical partition (automatically selected) [Enter] # Accept default first sector +5G # Set size to 5GB ``` Deleting Partitions Safe Deletion Process 1. Unmount the partition (if mounted): ```bash sudo umount /dev/sdb1 ``` 2. Start fdisk: ```bash sudo fdisk /dev/sdb ``` 3. Delete partition: ``` Command (m for help): d Partition number (1-4): 1 ``` 4. Verify deletion: ``` Command (m for help): p ``` 5. Write changes: ``` Command (m for help): w ``` Deleting Multiple Partitions To delete multiple partitions: ```bash In fdisk interactive mode d # Delete partition 1 # Delete partition 1 d # Delete partition 2 # Delete partition 2 p # Verify changes w # Write changes ``` Modifying Partition Types Understanding Partition Type Codes Common partition type codes: | Code | Type | |------|------| | 83 | Linux | | 82 | Linux swap | | 7 | HPFS/NTFS/exFAT | | b | W95 FAT32 | | c | W95 FAT32 (LBA) | | ef | EFI (FAT-12/16/32) | Changing Partition Type 1. Start fdisk: ```bash sudo fdisk /dev/sdb ``` 2. Change partition type: ``` Command (m for help): t Partition number (1-4): 1 Hex code (type L to list all codes): 82 ``` 3. Verify change: ``` Command (m for help): p ``` Setting Bootable Flag To make a partition bootable: ```bash In fdisk interactive mode a # Toggle bootable flag 1 # Partition number p # Verify bootable flag (*) w # Write changes ``` Advanced fdisk Operations Working with GPT Partition Tables For GPT disks, use `gdisk` or `fdisk` with GPT support: ```bash sudo fdisk /dev/sdb ``` Create GPT partition table: ``` Command (m for help): g ``` Sector-Level Operations Displaying in Sectors ```bash In fdisk interactive mode u # Toggle units between cylinders and sectors p # Display partition table in sectors ``` Manual Sector Specification ```bash When creating partition First sector: 2048 Last sector: 4196352 ``` Expert Mode Operations Access expert mode for advanced operations: ```bash In fdisk interactive mode x # Enter expert mode ``` Expert mode commands: - `r`: Return to main menu - `h`: Change number of heads - `s`: Change number of sectors per track - `c`: Change number of cylinders Practical Examples Example 1: Setting Up a Development Server Scenario: Configure a 100GB disk with separate partitions for system, home, and swap. ```bash Start fdisk sudo fdisk /dev/sdb Create system partition (30GB) n p 1 [Enter] +30G Create home partition (60GB) n p 2 [Enter] +60G Create swap partition (remaining space) n p 3 [Enter] [Enter] Change swap partition type t 3 82 Make system partition bootable a 1 Write changes w ``` Example 2: Preparing USB Drive with Multiple Partitions Scenario: Create a USB drive with a FAT32 partition for compatibility and an ext4 partition for Linux files. ```bash Start fdisk on USB drive sudo fdisk /dev/sdc Create FAT32 partition (4GB) n p 1 [Enter] +4G Create ext4 partition (remaining space) n p 2 [Enter] [Enter] Set FAT32 partition type t 1 b Write changes w Format partitions sudo mkfs.vfat /dev/sdc1 sudo mkfs.ext4 /dev/sdc2 ``` Example 3: Expanding Partition Layout Scenario: Add a new partition to existing disk layout without affecting current partitions. ```bash Check current layout sudo fdisk -l /dev/sda Start fdisk sudo fdisk /dev/sda Create new partition in free space n p 4 [Enter] +50G Verify layout p Write changes w ``` Common Issues and Troubleshooting Issue 1: "Device or resource busy" Error Problem: Cannot modify partition on mounted device. Solution: ```bash Check what's using the device sudo lsof /dev/sdb1 Unmount the partition sudo umount /dev/sdb1 If unmount fails, force unmount sudo umount -l /dev/sdb1 ``` Issue 2: Partition Table Corruption Problem: Partition table appears corrupted or unreadable. Solution: ```bash Try to repair with fdisk sudo fdisk /dev/sdb Use 'v' command to verify partition table Use 'x' for expert mode if needed Alternative: Use testdisk for recovery sudo apt-get install testdisk sudo testdisk ``` Issue 3: Cannot Create More Than 4 Primary Partitions Problem: MBR limitation prevents creating additional primary partitions. Solution: ```bash Convert one primary to extended partition Delete existing partition 4 d 4 Create extended partition n e 4 [Enter] [Enter] Create logical partitions within extended n l [Enter] +10G ``` Issue 4: Changes Not Reflected After fdisk Problem: Kernel doesn't recognize partition changes. Solution: ```bash Force kernel to re-read partition table sudo partprobe /dev/sdb Alternative method sudo hdparm -z /dev/sdb If still not working, reboot may be required sudo reboot ``` Issue 5: GPT vs MBR Conflicts Problem: Mixing GPT and MBR partition tables. Solution: ```bash Check current partition table type sudo parted /dev/sdb print Convert to GPT if needed sudo fdisk /dev/sdb g # Create new GPT partition table w # Write changes ``` Best Practices and Safety Tips Pre-Operation Safety Measures 1. Always Backup Data: ```bash Create full disk backup sudo dd if=/dev/sdb of=/backup/disk_backup.img bs=4M Create partition table backup sudo sfdisk -d /dev/sdb > /backup/partition_table.txt ``` 2. Verify Target Disk: ```bash Double-check disk identification lsblk sudo fdisk -l ``` 3. Unmount All Partitions: ```bash Check mounted partitions mount | grep /dev/sdb Unmount all partitions on target disk sudo umount /dev/sdb* ``` Operational Best Practices 1. Use Consistent Naming: Follow logical partition numbering schemes 2. Plan Partition Layout: Design layout before implementation 3. Leave Free Space: Reserve space for future expansion 4. Document Changes: Keep records of partition modifications Post-Operation Verification 1. Verify Partition Table: ```bash sudo fdisk -l /dev/sdb sudo parted /dev/sdb print ``` 2. Test File System Creation: ```bash Test formatting new partitions sudo mkfs.ext4 /dev/sdb1 sudo mkfs.ext4 /dev/sdb2 ``` 3. Mount and Test: ```bash Create mount points sudo mkdir -p /mnt/test1 /mnt/test2 Mount partitions sudo mount /dev/sdb1 /mnt/test1 sudo mount /dev/sdb2 /mnt/test2 Test write operations sudo touch /mnt/test1/testfile ls -la /mnt/test1/ ``` Security Considerations 1. Secure Data Wiping: ```bash Securely wipe partition before deletion sudo shred -vfz -n 3 /dev/sdb1 ``` 2. Verify Permissions: ```bash Ensure proper permissions on new partitions sudo chmod 755 /mnt/newpartition sudo chown user:group /mnt/newpartition ``` Alternative Tools Modern Alternatives to fdisk 1. parted: More user-friendly with GPT support ```bash sudo parted /dev/sdb (parted) print (parted) mkpart primary ext4 1MiB 10GiB ``` 2. gdisk: Specialized for GPT partitions ```bash sudo gdisk /dev/sdb ``` 3. cfdisk: Text-based interface with menus ```bash sudo cfdisk /dev/sdb ``` Graphical Tools - GParted: GUI partition manager - KDE Partition Manager: KDE-based partition tool - GNOME Disks: Simple disk management utility When to Use Each Tool | Tool | Best For | |------|----------| | fdisk | Traditional MBR partitions, scripting | | parted | GPT partitions, precise sizing | | gdisk | GPT-specific operations | | cfdisk | Interactive menu-driven operations | | GParted | Visual partition management | Conclusion Mastering fdisk is essential for effective Linux system administration and disk management. This comprehensive guide has covered everything from basic partition viewing to advanced manipulation techniques, providing you with the knowledge and confidence to manage disk partitions safely and efficiently. Key Takeaways 1. Safety First: Always backup data before partition operations 2. Understand Limitations: Know MBR vs GPT constraints 3. Verify Operations: Double-check changes before writing 4. Use Appropriate Tools: Choose the right tool for your specific needs 5. Practice Safely: Test on non-critical systems first Next Steps To continue improving your disk management skills: 1. Practice: Set up virtual machines for safe experimentation 2. Learn Related Tools: Explore parted, gdisk, and LVM 3. Understand File Systems: Study different file system types and their use cases 4. Automation: Learn to script partition operations for consistency 5. Advanced Topics: Investigate RAID, LVM, and advanced storage solutions Final Recommendations - Keep this guide as a reference for future partition operations - Always test procedures in safe environments before production use - Stay updated with latest partitioning tools and best practices - Consider modern alternatives like LVM for complex storage requirements Remember that partition management is a critical system administration skill that requires careful attention to detail and thorough understanding of the underlying concepts. With the knowledge gained from this guide, you're well-equipped to handle most partition management scenarios confidently and safely.