-
-
Notifications
You must be signed in to change notification settings - Fork 2
Linux swap Guide
Mattscreative edited this page Dec 5, 2025
·
2 revisions
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.
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
Using fdisk:
# Create partition
sudo fdisk /dev/sda
# Create partition type 82 (Linux swap)
# Then format
sudo mkswap /dev/sda2Format partition:
# Format as swap
sudo mkswap /dev/sda2
# With label
sudo mkswap -L "swap" /dev/sda2Activate swap:
# Enable swap
sudo swapon /dev/sda2
# Check status
swapon --showCreate file:
# Create 2GB swap file
sudo fallocate -l 2G /swapfile
# Or use dd
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048Format:
# Set permissions
sudo chmod 600 /swapfile
# Format as swap
sudo mkswap /swapfileActivate:
# Enable swap file
sudo swapon /swapfile
# Check status
swapon --showAuto-mount swap:
# Edit fstab
sudo vim /etc/fstabAdd:
# Swap partition
UUID=xxxx-xxxx none swap defaults 0 0
# Or swap file
/swapfile none swap defaults 0 0
Set priority:
# In fstab
/swapfile none swap defaults,pri=10 0 0Check status:
# Check swap
swapon --show
# Check usage
free -h
# Enable swap
sudo swapon -aFix issues:
# Check swap file
sudo swapon --show
# Recreate if needed
sudo swapoff /swapfile
sudo mkswap /swapfile
sudo swapon /swapfileThis guide covered swap setup, swap files, and swap configuration for Arch Linux, CachyOS, and other distributions.
- 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.