HostSlim

How do I add swap space to my Linux server?

Swap is disk space the server can use when its RAM is full. It prevents out-of-memory crashes, but is much slower than RAM. sudo fallocate -l 2G /swapfile…

  • VPS

Swap is disk space the server can use when its RAM is full. It prevents out-of-memory crashes, but is much slower than RAM.

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
free -h

A swap file of 1–2× your RAM (up to a few GB) is enough for most servers. If your server uses swap all the time, it needs more RAM – consider upgrading your VPS or moving to a dedicated server.

More from the knowledge base

Need something that is not on the list?

Custom builds, private VLANs, BGP sessions, multi-rack deployments. Tell us what you are running and an engineer replies, usually within the hour.