Swap is disk space that Linux uses when the memory (RAM) is full. It does not make the server faster, but it keeps programs from being killed when memory runs out for a moment, for example during a large update or an unusual peak. The steps were tested on Ubuntu 26.04 LTS; run the commands as root.


1. Check What You Have

swapon --show
free -h

Servers installed from the Client Area usually already have a swap partition, for example /dev/md3 partition 4G on a server with RAID. If swapon --show prints nothing, there is no swap. If the Swap line of free -h shows that the swap is nearly full, add more.

2. Create a Swap File

A swap file is the simplest way to add swap. This creates one of 2 GB:

fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

swapon --show now lists /swapfile file 2G next to the partition.

3. Keep It After a Restart

Add this line at the end of /etc/fstab:

/swapfile none swap sw 0 0

Then let the system read the changed file:

systemctl daemon-reload

4. How Eagerly Linux Uses Swap

The value swappiness (0 to 100, default 60) says how early Linux moves memory to the disk. For a server where most memory is used by programs, a lower value such as 10 keeps more in RAM:

sysctl -w vm.swappiness=10

That lasts until the next restart. To keep it, create the file /etc/sysctl.d/99-swappiness.conf with the line:

vm.swappiness=10

5. Remove the Swap File

swapoff /swapfile
rm /swapfile

Then delete the /swapfile line from /etc/fstab. Never delete the file while it is still in use: swapon --show must not list it any more.

ℹ Swap is not more memory

If the swap is used heavily all the time, the server is short of memory, and everything becomes slow. Find out which program uses the memory (What Fills the Disk and What Loads the Server), or choose a server with more RAM.


Related Guides

View all Linux guides

Was this answer helpful? 0 Users Found This Useful (0 Votes)