swapon Command
Intermediate Disk & Storage man(8)Enable and manage swap space
📅 Updated: Mar 16, 2026
SYNTAX
swapon [OPTIONS] [DEVICE|FILE]
What Does swapon Do?
The swapon command enables swap space on a device or file, making it available for the kernel's virtual memory system. Swap provides overflow space when physical RAM is full — the kernel moves less-used memory pages to swap, freeing RAM for active processes.
Swap is critical for system stability. Without swap, when RAM is exhausted, the OOM (Out-Of-Memory) killer terminates processes to free memory, which can crash important services. With swap, the system degrades gracefully — slowing down instead of killing processes.
swapon and its counterpart swapoff manage which swap devices and files are active. System administrators use these commands to add swap space on running systems, adjust swap priority, create swap files (preferred over swap partitions on modern systems), and troubleshoot memory pressure situations.
Modern Linux systems often use swap files instead of dedicated swap partitions, as they are easier to resize and manage. Additionally, zram (compressed swap in RAM) and zswap (compressed swap cache) complement traditional swap for better performance.
Swap is critical for system stability. Without swap, when RAM is exhausted, the OOM (Out-Of-Memory) killer terminates processes to free memory, which can crash important services. With swap, the system degrades gracefully — slowing down instead of killing processes.
swapon and its counterpart swapoff manage which swap devices and files are active. System administrators use these commands to add swap space on running systems, adjust swap priority, create swap files (preferred over swap partitions on modern systems), and troubleshoot memory pressure situations.
Modern Linux systems often use swap files instead of dedicated swap partitions, as they are easier to resize and manage. Additionally, zram (compressed swap in RAM) and zswap (compressed swap cache) complement traditional swap for better performance.
Options & Flags
| Option | Description | Example |
|---|---|---|
| -s | Display swap usage summary | swapon -s |
| --show | Show swap devices in columns format | swapon --show |
| -a | Enable all swaps from /etc/fstab | sudo swapon -a |
| -p PRIORITY | Set swap priority (higher = used first) | sudo swapon -p 10 /swapfile |
| DEVICE | Enable swap on a specific device or file | sudo swapon /swapfile |
| --verbose | Verbose output showing details | sudo swapon --verbose /swapfile |
Practical Examples
#1 Show active swap
Display all active swap devices/files with size, type, and usage.
$ swapon --show
Output:
NAME TYPE SIZE USED PRIO\n/swapfile file 4G 256M -2
#2 Create and enable a swap file
Create a 4GB swap file: allocate space, set secure permissions, format as swap, and activate.
$ sudo fallocate -l 4G /swapfile && sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile#3 Make swap permanent
Add swap file to /etc/fstab so it activates on boot.
$ echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab#4 Disable swap temporarily
Disable a swap file. All data in swap is moved back to RAM — ensure enough free RAM first.
$ sudo swapoff /swapfile#5 Resize swap file
Resize swap from 4GB to 8GB. Must disable first, recreate, then re-enable.
$ sudo swapoff /swapfile && sudo fallocate -l 8G /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile#6 Check swap usage with free
Quick check of total, used, and free swap space.
$ free -h | grep -i swap
Output:
Swap: 4.0Gi 256Mi 3.7Gi
#7 Set swap priority
Set higher priority for fast NVMe swap, lower for file-based swap. Higher priority swap is used first.
$ sudo swapon -p 100 /dev/nvme0n1p3 && sudo swapon -p 10 /swapfileTips & Best Practices
Recommended swap size: Servers: equal to RAM for hibernation, or 1-2GB minimum for stability. If RAM >16GB and no hibernation, 2-4GB swap is usually sufficient.
swapoff needs free RAM: swapoff moves ALL swap data back to RAM. If there is not enough free RAM, swapoff will hang or fail. Check free -h before running swapoff.
Swappiness: Control how aggressively Linux uses swap: sysctl vm.swappiness=10 (10 = prefer RAM, 60 = default, 100 = swap aggressively). For databases, use 10.
Swap file vs partition: Swap files are easier to resize and manage than swap partitions. Performance is identical on ext4/XFS. Use swap files unless you have a specific reason for partitions.
Frequently Asked Questions
How do I add swap space in Linux?
Create a swap file: sudo fallocate -l 4G /swapfile && sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile. Add to /etc/fstab for persistence.
How much swap do I need?
Minimum 1-2GB for system stability. For hibernation: equal to RAM. For servers with 16GB+ RAM without hibernation: 2-4GB is usually sufficient. Monitor actual usage with swapon --show.
How do I check current swap usage?
swapon --show or free -h. For per-process swap: grep VmSwap /proc/PID/status or use smem tool for detailed per-process breakdown.
Is swap bad for SSDs?
Modern SSDs handle swap well. Swap usage is typically minimal with enough RAM. For heavy swap, consider zram (compressed RAM) which reduces SSD writes: sudo modprobe zram.
Related Commands
More Disk & Storage Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →