A cheatsheet for modifying swap on a linux machine

Cheatsheet

# See attached swaps
sudo swapon --show

# Create/Attach/Resize swap
sudo swapoff /swap.img
sudo fallocate -l 8G /swap.img
sudo chmod 600 /swap.img
sudo mkswap /swap.img
sudo swapon /swap.img

# Quick and dirty add swap to fstab
echo '/swap.img none swap sw 0 0' | sudo tee -a /etc/fstab

Swappiness

# See swappiness
cat /proc/sys/vm/swappiness

# Set swappiness=1
sudo sysctl vm.swappiness=1
# Before you tee, you should grep for it instead.
echo 'vm.swappiness=1' | sudo tee -a /etc/sysctl.conf