Quick Linux Tip #94:
Question: My server is low on RAM but has no swap partition. How do I add swap?
Try:
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
Info: fallocate preallocates space instantly without writing zeros. chmod 600 secures swap permissions, mkswap formats the file, and swapon activates it immediately.
Examples:
- $
sudo swapoff /swapfile && sudo rm /swapfile# Safely remove active swap file - $
sudo sysctl vm.swappiness=10# Reduce swap usage aggressiveness - $
free -h# Verify total and active swap allocation
Note: chmod 600 is critical because swap memory can store unencrypted secrets. Add /swapfile none swap sw 0 0 to /etc/fstab to persist across reboots.
Leave a Reply