Linux swap Guide - ryzendew/Linux-Tips-and-Tricks GitHub Wiki
Linux Swap Guide
Complete beginner-friendly guide to swap on Linux, covering Arch Linux, CachyOS, and other distributions including swap files, swap partitions, swap configuration, and memory management.
Table of Contents
Understanding Swap
What is Swap?
Swap is virtual memory on disk.
Uses:
- Memory overflow: When RAM is full
- Hibernation: Save system state
- Memory management: Free up RAM
- System stability: Prevent OOM errors
When to use:
- Low RAM: Systems with limited memory
- Hibernation: Need to hibernate
- Memory-intensive: Running large applications
Swap Partition
Create Swap Partition
Using fdisk:
# Create partition
sudo fdisk /dev/sda
# Create partition type 82 (Linux swap)
# Then format
sudo mkswap /dev/sda2
Format Swap
Format partition:
# Format as swap
sudo mkswap /dev/sda2
# With label
sudo mkswap -L "swap" /dev/sda2
Enable Swap
Activate swap:
# Enable swap
sudo swapon /dev/sda2
# Check status
swapon --show
Swap File
Create Swap File
Create file:
# Create 2GB swap file
sudo fallocate -l 2G /swapfile
# Or use dd
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
Format Swap File
Format:
# Set permissions
sudo chmod 600 /swapfile
# Format as swap
sudo mkswap /swapfile
Enable Swap File
Activate:
# Enable swap file
sudo swapon /swapfile
# Check status
swapon --show
Swap Configuration
fstab Entry
Auto-mount swap:
# Edit fstab
sudo vim /etc/fstab
Add:
# Swap partition
UUID=xxxx-xxxx none swap defaults 0 0
# Or swap file
/swapfile none swap defaults 0 0
Swap Priority
Set priority:
# In fstab
/swapfile none swap defaults,pri=10 0 0
Troubleshooting
Swap Not Working
Check status:
# Check swap
swapon --show
# Check usage
free -h
# Enable swap
sudo swapon -a
Swap Errors
Fix issues:
# Check swap file
sudo swapon --show
# Recreate if needed
sudo swapoff /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
Summary
This guide covered swap setup, swap files, and swap configuration for Arch Linux, CachyOS, and other distributions.
Next Steps
- Filesystem Management - Filesystem setup
- fdisk Guide - Partitioning
- System Configuration - System setup
- swap Documentation:
man swapon
This guide covers Arch Linux, CachyOS, and other Linux distributions. For distribution-specific details, refer to your distribution's documentation.