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

swapon [OPTIONS] [DEVICE...] swapon -a swapon -s

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
Note: Swap devices must be properly formatted with mkswap before they can be enabled with swapon.

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

# Show swap summary swapon -s # Show detailed swap information swapon --show # Alternative method cat /proc/swaps

Check currently active swap spaces

Enable specific swap partition

sudo swapon /dev/sdb1

Enable a specific swap partition

Enable specific swap file

sudo swapon /swapfile

Enable a swap file

Enable all swap spaces

sudo swapon -a

Enable all swap spaces listed in /etc/fstab

Enable swap with priority

sudo swapon -p 10 /dev/sdb1

Enable swap with specific priority (higher numbers = higher priority)

Enable swap with verbose output

sudo swapon -v /dev/sdb1

Enable swap with detailed output

Enable swap with discard support

sudo swapon -d /dev/sdb1

Enable swap with TRIM/discard support for SSDs

Enable multiple swap devices

sudo swapon /dev/sdb1 /dev/sdc1

Enable multiple swap devices at once

Understanding swapon Output

Summary Output (-s option)

$ swapon -s Filename Type Size Used Priority /dev/sdb1 partition 2097148 0 -2 /swapfile file 1048572 0 -3

Summary showing swap devices, types, sizes, and usage

Detailed Output (--show option)

$ swapon --show NAME TYPE SIZE USED PRIO /dev/sdb1 partition 2G 0B -2 /swapfile file 1024M 0B -3

Detailed table format with human-readable sizes

Custom Column Display

# Show specific columns $ swapon --show=NAME,SIZE,USED NAME SIZE USED /dev/sdb1 2G 0B /swapfile 1024M 0B # Show all available columns $ swapon --show=NAME,TYPE,SIZE,USED,PRIO,UUID

Customize output to show specific information

Creating and Managing Swap

Creating Swap Partition

Complete swap partition setup

# 1. Create partition (using fdisk) sudo fdisk /dev/sdb # Create new partition, set type to 82 (Linux swap) # 2. Format as swap sudo mkswap /dev/sdb1 # 3. Enable swap sudo swapon /dev/sdb1 # 4. Add to /etc/fstab for permanent use echo '/dev/sdb1 none swap sw 0 0' | sudo tee -a /etc/fstab # 5. Verify swapon --show

Complete process to create and enable a swap partition

Creating Swap File

Complete swap file setup

# 1. Create swap file (1GB example) sudo fallocate -l 1G /swapfile # or: sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576 # 2. Set proper permissions sudo chmod 600 /swapfile # 3. Format as swap sudo mkswap /swapfile # 4. Enable swap sudo swapon /swapfile # 5. Add to /etc/fstab echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab # 6. Verify swapon --show

Complete process to create and enable a swap file

Swap Priorities

Understanding swap priorities

# Enable swap with different priorities sudo swapon -p 10 /dev/sdb1 # Higher priority sudo swapon -p 5 /swapfile # Lower priority # Check priorities swapon --show # In /etc/fstab, set priorities: # /dev/sdb1 none swap sw,pri=10 0 0 # /swapfile none swap sw,pri=5 0 0

Manage swap priorities for optimal performance

Practical Use Cases

System Administration

Boot-time swap activation

# /etc/fstab entries for automatic swap activation /dev/sdb1 none swap sw 0 0 /swapfile none swap sw,pri=5 0 0 # Test fstab entries sudo swapon -a # Verify all swap is active swapon --show

Configure automatic swap activation at boot

Emergency swap addition

# Quickly add temporary swap file sudo fallocate -l 2G /tmp/emergency_swap sudo chmod 600 /tmp/emergency_swap sudo mkswap /tmp/emergency_swap sudo swapon /tmp/emergency_swap # Check memory situation free -h # Remove when no longer needed sudo swapoff /tmp/emergency_swap sudo rm /tmp/emergency_swap

Add temporary swap space in emergency situations

Performance Optimization

Multi-device swap setup

# Enable multiple swap devices with priorities sudo swapon -p 15 /dev/sdb1 # Fast SSD sudo swapon -p 10 /dev/sdc1 # Slower HDD sudo swapon -p 5 /swapfile # File-based swap # Check configuration swapon --show # Monitor usage watch -n 1 'swapon --show && echo && free -h'

Set up multiple swap devices with different priorities

SSD-optimized swap

# Enable swap with discard support for SSD sudo swapon -d /dev/sdb1 # Or with specific discard policy sudo swapon -d=once /dev/sdb1 # In /etc/fstab for permanent use: # /dev/sdb1 none swap sw,discard 0 0 # Verify discard is enabled cat /proc/swaps

Optimize swap for SSD drives with TRIM support

Monitoring and Troubleshooting

Swap usage monitoring

# Monitor swap usage watch -n 2 'swapon --show && echo && free -h' # Check swap usage by process for file in /proc/*/status; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file done | grep -v " 0" | sort -k2 -nr # Historical swap usage sar -S 1 10

Monitor swap usage and identify processes using swap

Troubleshoot swap issues

# Check if swap device exists ls -la /dev/sdb1 # Verify swap signature sudo file -s /dev/sdb1 # Check for errors in system logs sudo dmesg | grep -i swap sudo journalctl | grep -i swap # Test swap device sudo mkswap -c /dev/sdb1 # Check for bad blocks

Diagnose and troubleshoot swap-related issues

Advanced Configuration

Encrypted Swap

Set up encrypted swap

# Create encrypted swap with cryptsetup sudo cryptsetup luksFormat /dev/sdb1 sudo cryptsetup luksOpen /dev/sdb1 encrypted_swap # Format and enable sudo mkswap /dev/mapper/encrypted_swap sudo swapon /dev/mapper/encrypted_swap # Add to /etc/crypttab and /etc/fstab echo 'encrypted_swap /dev/sdb1 none luks' | sudo tee -a /etc/crypttab echo '/dev/mapper/encrypted_swap none swap sw 0 0' | sudo tee -a /etc/fstab

Set up encrypted swap for enhanced security

Swap on LVM

Create swap on logical volume

# Create logical volume for swap sudo lvcreate -L 2G -n swap_lv vg0 # Format and enable sudo mkswap /dev/vg0/swap_lv sudo swapon /dev/vg0/swap_lv # Add to /etc/fstab echo '/dev/vg0/swap_lv none swap sw 0 0' | sudo tee -a /etc/fstab # Verify swapon --show

Create and manage swap on LVM logical volumes

Systemd Integration

Custom swap service

# Create custom swap service: /etc/systemd/system/custom-swap.service [Unit] Description=Enable Custom Swap After=local-fs.target [Service] Type=oneshot ExecStart=/usr/sbin/swapon -p 15 /dev/sdb1 ExecStop=/usr/sbin/swapoff /dev/sdb1 RemainAfterExit=yes [Install] WantedBy=multi-user.target # Enable the service sudo systemctl enable custom-swap.service sudo systemctl start 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

See also