swapon Command
The swapon command enables swap space on Linux systems. It activates swap partitions or swap files, making them available for the kernel to use as virtual memory when physical RAM is insufficient.
Syntax
Description
The swapon command is used to enable swap space on Linux systems. It can activate individual swap devices or all swap spaces defined in /etc/fstab. The command also provides functionality to display information about active swap spaces.
Key features:
- Enable specific swap partitions or files
- Enable all swap spaces from /etc/fstab
- Display active swap space information
- Set swap priorities for optimization
- Support for various swap device types
- Integration with system boot process
Common Options
| Option | Description |
|---|---|
-a, --all |
Enable all swap spaces listed in /etc/fstab |
-d, --discard[=policy] |
Enable discard/TRIM support for SSD swap |
-e, --ifexists |
Silently skip devices that do not exist |
-f, --fixpgsz |
Reinitialize swap space if page size has changed |
-o, --options opts |
Specify mount options |
-p, --priority priority |
Set swap priority (0-32767) |
-s, --summary |
Display summary of swap usage |
--show[=column] |
Display swap information in table format |
-v, --verbose |
Display verbose output |
-h, --help |
Display help message |
-V, --version |
Display version information |
Examples
Display active swap spaces
Check currently active swap spaces
Enable specific swap partition
Enable a specific swap partition
Enable specific swap file
Enable a swap file
Enable all swap spaces
Enable all swap spaces listed in /etc/fstab
Enable swap with priority
Enable swap with specific priority (higher numbers = higher priority)
Enable swap with verbose output
Enable swap with detailed output
Enable swap with discard support
Enable swap with TRIM/discard support for SSDs
Enable multiple swap devices
Enable multiple swap devices at once
Understanding swapon Output
Summary Output (-s option)
Summary showing swap devices, types, sizes, and usage
Detailed Output (--show option)
Detailed table format with human-readable sizes
Custom Column Display
Customize output to show specific information
Creating and Managing Swap
Creating Swap Partition
Complete swap partition setup
Complete process to create and enable a swap partition
Creating Swap File
Complete swap file setup
Complete process to create and enable a swap file
Swap Priorities
Understanding swap priorities
Manage swap priorities for optimal performance
Practical Use Cases
System Administration
Boot-time swap activation
Configure automatic swap activation at boot
Emergency swap addition
Add temporary swap space in emergency situations
Performance Optimization
Multi-device swap setup
Set up multiple swap devices with different priorities
SSD-optimized swap
Optimize swap for SSD drives with TRIM support
Monitoring and Troubleshooting
Swap usage monitoring
Monitor swap usage and identify processes using swap
Troubleshoot swap issues
Diagnose and troubleshoot swap-related issues
Advanced Configuration
Encrypted Swap
Set up encrypted swap
Set up encrypted swap for enhanced security
Swap on LVM
Create swap on logical volume
Create and manage swap on LVM logical volumes
Systemd Integration
Custom swap service
Create custom systemd service for swap management
Best Practices
swapon Command Best Practices
- Set Appropriate Priorities - Use higher priorities for faster storage devices
- Monitor Swap Usage - Regularly check swap usage to optimize system performance
- Use SSD Optimization - Enable discard/TRIM for SSD-based swap
- Plan Swap Size - Size swap appropriately based on RAM and workload
- Update fstab - Always update /etc/fstab for permanent swap configuration
- Test Configuration - Test swap configuration before rebooting