How to Fix “swapon failed: Operation not permitted” When Enabling Swap File on Linux Server


2 views

When trying to activate a swap file on a Linux server with:

sudo swapon /swapfile1

Many administrators encounter the frustrating error:

swapon: /swapfile1: swapon failed: Operation not permitted

This error typically occurs due to:

  • Incorrect file permissions (swap files need 600 permissions)
  • Missing nosuid and noexec mount options on the filesystem
  • Filesystem type limitations (some like tmpfs don't support swap)
  • Secure boot or SELinux restrictions

Here's the complete procedure to properly create and enable a swap file:

# Create 2GB swap file (adjust size as needed)
sudo fallocate -l 2G /swapfile

# Set correct permissions
sudo chmod 600 /swapfile

# Format as swap
sudo mkswap /swapfile

# Enable temporarily
sudo swapon /swapfile

# Make permanent by adding to fstab
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

If you still encounter issues, check these advanced solutions:

# Verify filesystem mount options
mount | grep 'on / '

# Check SELinux context
ls -Z /swapfile

# Verify file type
file /swapfile

# Test with secure boot temporarily disabled

For servers running GUI environments and multiple services like FreeNX, consider these optimizations:

  • Set vm.swappiness between 10-60 (sudo sysctl vm.swappiness=30)
  • Add multiple smaller swap files instead of one large file
  • Place swap on faster storage if available
  • Monitor swap usage with free -h and vmstat 1

When trying to activate a swap file using swapon /swapfile1, the "Operation not permitted" error typically occurs due to one of these common issues:

# Common root causes:
1. Incorrect file permissions
2. Missing nosuid and noexec mount options
3. Filesystem limitations (e.g., swap on NTFS/FAT)
4. SELinux/AppArmor restrictions
5. Invalid swap file signature

Here's the proper way to create and enable a swap file on modern Linux systems:

# Step 1: Create swap file (1GB example)
sudo fallocate -l 1G /swapfile1

# Step 2: Set secure permissions
sudo chmod 600 /swapfile1

# Step 3: Format as swap
sudo mkswap /swapfile1

# Step 4: Enable swap immediately
sudo swapon /swapfile1

# Step 5: Make persistent (add to /etc/fstab)
echo '/swapfile1 none swap sw 0 0' | sudo tee -a /etc/fstab

Case 1: Filesystem mount options conflict
If your filesystem is mounted with noexec or nosuid, remount it temporarily:

sudo mount -o remount,rw,exec,suid /

Case 2: SELinux contexts
For RHEL/CentOS systems, restore the proper context:

sudo restorecon -v /swapfile1

Case 3: Btrfs filesystem limitations
On Btrfs, you'll need special handling:

sudo truncate -s 0 /swapfile1
sudo chattr +C /swapfile1
sudo fallocate -l 1G /swapfile1

After successful activation, verify with these commands:

# Check active swap
sudo swapon --show

# Display memory summary
free -h

# Verify swap priority
cat /proc/swaps

For systems running GUI + applications + FreeNX (as mentioned in the original question), consider these optimizations:

# Adjust swappiness (30-60 recommended for desktop use)
echo 'vm.swappiness=40' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

# Set vfs_cache_pressure (reduces inode/dentry cache reclaim)
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf

Remember that while swap helps prevent OOM situations, upgrading RAM remains the best solution for memory-intensive workloads.