Table of Contents
-
Understanding Linux Partitioning Basics
- 1.1 What is a Partition?
- 1.2 Partition Tables: MBR vs. GPT
- 1.3 Key Partition Types and File Systems
- 1.4 Mount Points Explained
-
Planning Your Partition Layout
- 2.1 Factors to Consider
- 2.2 Recommended Partition Schemes (By Use Case)
- 2.3 Swap Space: How Much Do You Need?
-
Step-by-Step Implementation: Command-Line Tools
- 4.1 Identifying Disks with
lsblkandfdisk - 4.2 Choosing MBR or GPT
- 4.3 Creating Partitions with
fdisk(MBR) - 4.4 Creating Partitions with
parted(GPT) - 4.5 Formatting Partitions with
mkfs - 4.6 Setting Up Swap
- 4.7 Mounting Partitions and Updating
fstab
- 4.1 Identifying Disks with
1. Understanding Linux Partitioning Basics
1.1 What is a Partition?
A partition is a logical division of a physical disk. Think of a disk as a large book; partitions are like chapters, each with its own content and purpose. Without partitioning, the entire disk would act as a single “chapter,” making it harder to manage data, reinstall the OS, or isolate failures.
1.2 Partition Tables: MBR vs. GPT
Before creating partitions, you must choose a partition table (also called a “disk label”), which defines how partitions are organized on the disk. The two most common types are:
-
MBR (Master Boot Record):
- Older standard, limited to disks ≤ 2 TiB.
- Supports up to 4 primary partitions (or 3 primary + 1 extended partition, which can hold multiple logical partitions).
- Uses BIOS firmware.
-
GPT (GUID Partition Table):
- Modern standard, supports disks > 2 TiB (up to 8 ZiB).
- Supports up to 128 primary partitions (no extended/logical distinction).
- More robust (includes backup partition table) and required for UEFI firmware.
Recommendation: Use GPT for new systems, especially if your disk is >2 TiB or you need more than 4 partitions.
1.3 Key Partition Types and File Systems
Partitions are formatted with a file system—a structure that organizes data (e.g., files, directories). Common Linux file systems include:
- ext4: Default for most Linux distros (stable, compatible, supports large files).
- XFS: Optimized for large files and high throughput (common on servers).
- Btrfs: Advanced (supports snapshots, RAID, compression) but less mature than ext4.
- Swap: Not a “file system,” but a dedicated partition for virtual memory (acts as overflow for RAM).
1.4 Mount Points Explained
In Linux, partitions are “mounted” to mount points—directories that act as entry points to the partition’s data. For example:
/(root): The top-level directory, contains all other directories (mandatory)./home: Stores user data (photos, documents, etc.)./boot: Contains bootloader files (e.g., GRUB) and kernel images (optional but recommended for encryption/LVM)./var: Stores variable data (logs, databases, spool files).
2. Planning Your Partition Layout
2.1 Factors to Consider
- Disk Size: Larger disks may benefit from more partitions (e.g., separate
/varfor logs). - Use Case: Desktop vs. server vs. dual-boot (with Windows/macOS).
- Data Importance: Separate
/hometo preserve user data during OS reinstalls. - Encryption: If encrypting the disk, you may need a separate
/boot(unencrypted for UEFI/BIOS to boot).
2.2 Recommended Partition Schemes
Scenario 1: Basic Desktop (100 GB–1 TB Disk)
| Partition | Mount Point | Size | File System | Purpose |
|---|---|---|---|---|
/ | / | 30–50 GB | ext4/XFS | OS and system files |
/home | /home | Remainder | ext4/XFS | User data |
swap | N/A | 2–8 GB | swap | Virtual memory |
Scenario 2: Server (1 TB+ Disk)
| Partition | Mount Point | Size | File System | Purpose |
|---|---|---|---|---|
/boot | /boot | 512 MB–1 GB | ext4 | Boot files (critical for encryption) |
/ | / | 50–100 GB | ext4/XFS | OS files |
/home | /home | 100–200 GB | ext4/XFS | User data |
/var | /var | 50–100 GB | XFS | Logs/databases (high I/O) |
/tmp | /tmp | 20–50 GB | tmpfs | Temporary files (RAM-based for speed) |
swap | N/A | 4–16 GB | swap | Virtual memory |
Scenario 3: Dual-Boot with Windows
- Shrink the Windows partition first (via Windows Disk Management).
- Allocate remaining space to Linux partitions (use GPT for UEFI systems).
2.3 Swap Space: How Much Do You Need?
Swap acts as “emergency RAM” when physical memory is full. General guidelines:
- < 2 GB RAM: 2x RAM (e.g., 1 GB RAM → 2 GB swap).
- 2–16 GB RAM: Equal to RAM (e.g., 8 GB RAM → 8 GB swap).
- > 16 GB RAM: 4–8 GB (unless hibernating, which requires swap ≥ RAM).
3. Pre-Implementation Checklist
- Backup Data: Partitioning erases data! Use tools like
rsync,dd, or cloud storage to back up critical files. - Identify Target Disk: Use
lsblkorfdisk -lto list disks (e.g.,/dev/sda,/dev/nvme0n1for NVMe drives). - Boot from Live USB: For partitioning an empty disk (e.g., new system), boot a Linux live environment (e.g., Ubuntu Live).
4. Step-by-Step Implementation: Command-Line Tools
4.1 Identify Target Disk
First, list all disks to avoid partitioning the wrong one:
lsblk # Lists all disks and partitions (e.g., /dev/sda, /dev/sdb)
fdisk -l /dev/sda # Detailed info about disk /dev/sda
Example Output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 465.8G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
└─sda2 8:2 0 465.3G 0 part /
4.2 Choose Partition Table (MBR/GPT)
Use parted to set the partition table. Replace /dev/sda with your disk:
-
For GPT:
parted /dev/sda mklabel gpt -
For MBR:
parted /dev/sda mklabel msdos # "msdos" = MBR
4.3 Create Partitions with fdisk (MBR)
fdisk is a simple tool for MBR partitioning. Run:
fdisk /dev/sda # Replace /dev/sda with your disk
Steps in fdisk:
- Type
nto create a new partition. - Choose
p(primary) ore(extended, for logical partitions). - Enter partition number (1–4 for primary).
- Set start/end size (e.g.,
+50Gfor a 50 GB partition). - Type
wto write changes and exit.
4.4 Create Partitions with parted (GPT)
parted supports GPT and is more flexible for large disks. Example: create a 50 GB root partition on /dev/sda:
parted /dev/sda # Launch parted for /dev/sda
(parted) mkpart # Start creating a partition
Partition name? []? root # Optional label
File system type? [ext2]? ext4 # Anticipated file system
Start? 1MiB # Start at 1 MiB (avoids alignment issues)
End? 50GB # End at 50 GB
(parted) print # Verify partition was created
(parted) quit # Exit
4.5 Format Partitions with mkfs
After creating partitions, format them with a file system. Use:
mkfs.ext4for ext4,mkfs.xfsfor XFS, etc.
Example: Format /dev/sda1 as ext4 (root partition):
mkfs.ext4 /dev/sda1 # Replace /dev/sda1 with your partition
Example: Format /dev/sda2 as XFS (home partition):
mkfs.xfs /dev/sda2
4.6 Set Up Swap
Create and enable a swap partition:
mkswap /dev/sda3 # Format /dev/sda3 as swap
swapon /dev/sda3 # Enable swap temporarily
To make swap permanent, add it to /etc/fstab (see Section 4.7).
4.7 Mount Partitions and Update fstab
To use partitions permanently, mount them and update /etc/fstab (the file system table).
Step 1: Mount Temporarily
mkdir /mnt/root # Create mount point
mount /dev/sda1 /mnt/root # Mount root partition
mkdir /mnt/root/home # Create /home mount point
mount /dev/sda2 /mnt/root/home # Mount home partition
Step 2: Update fstab
fstab defines how partitions mount at boot. Use blkid to get the partition’s UUID (unique identifier):
blkid /dev/sda1 # Output: /dev/sda1: UUID="abc123..." TYPE="ext4"
Edit /etc/fstab (or /mnt/root/etc/fstab for a new system) and add:
UUID=abc123... / ext4 defaults 0 1
UUID=def456... /home xfs defaults 0 2
UUID=ghi789... none swap sw 0 0
defaults: Common options (rw, suid, dev, exec, auto, nouser, async).0 1: Dump/fsck flags (1 for root, 2 for others, 0 to skip).
5. Step-by-Step Implementation: Graphical Tool (GParted)
For users who prefer a GUI, GParted (GNOME Partition Editor) simplifies partitioning.
Steps:
-
Install GParted:
sudo apt install gparted # Debian/Ubuntu sudo dnf install gparted # Fedora/RHEL -
Launch GParted and select the target disk (top-right dropdown).
-
Create Partition Table:
- Click Device → Create Partition Table.
- Choose GPT or MBR, then Apply.
-
Create Partitions:
- Click New (toolbar).
- Set size (e.g., 50 GB), file system (ext4), and label (e.g., “root”).
- Click Add, then Apply (checkmark icon) to commit changes.
-
Format and Mount:
GParted formats partitions automatically. Use the file manager to mount them temporarily, or update/etc/fstabas above.
6. Troubleshooting Common Partitioning Issues
- “Partition Not Mounting”: Check
/etc/fstabfor typos (UUIDs, mount points). Usemount -ato test fstab. - “File System Errors”: Run
fsck /dev/sda1(unmount first) to repair corruption. - “Disk Full”: Use
df -hto check space, ordu -sh /home/*to find large files. - “GPT/MBR Conflict”: If switching from MBR to GPT, use
gdiskto convert (backup data first!).
7. Best Practices for Linux Partitioning
- Separate
/home: Preserves user data during OS reinstalls. - Use GPT: For disks >2 TiB or UEFI systems.
- Limit Partition Count: Too many partitions complicate management.
- Encrypt Sensitive Partitions: Use
cryptsetup(LUKS) for/homeor/. - Regular Backups: Even with partitions, back up data to external storage.
8. Conclusion
Linux partitioning is a critical skill that balances organization, performance, and data safety. By planning your layout, choosing the right tools (command-line or GUI), and following best practices, you can create a robust system tailored to your needs. Always back up data first, and don’t hesitate to experiment in a virtual machine (e.g., VirtualBox) before partitioning physical disks.