A swap file lets the kernel move idle pages out of RAM onto disk. It’s handy on small-memory hosts (VPS, VMs) that have no dedicated swap partition, and unlike a partition it can be resized or removed without repartitioning.
Create the swap file
| |
Make it persistent
Add an entry to /etc/fstab so the swap file is mounted on every boot. The
grep guard keeps this idempotent — running it twice won’t add a duplicate line.
| |
Tune swappiness
vm.swappiness (0–100, default 60) controls how aggressively the kernel swaps;
a lower value keeps more data in RAM. 10 is a common choice for servers.
Write it to a drop-in file under /etc/sysctl.d/ rather than editing
/etc/sysctl.conf directly with >, which would overwrite the whole file and
wipe any existing settings.
| |
Verify
| |