// ssd vps

How to Configure a Swap File on a Linux VPS: Practical Guide

August 08, 2026 · by CLIQHOST

How to Configure a Swap File on a Linux VPS: Practical Guide

Every server administrator has encountered the dreaded Out of Memory (OOM) error at least once. Processes crash, applications stop responding, and the server becomes unusable. The quickest and most effective solution in these situations is configuring a swap file.

In this guide, you'll learn exactly what swap is, when and why you need it, and how to set it up step by step on a VPS SSD or VPS NVMe running popular Linux distributions like Ubuntu or Debian.


What Is Swap and Why Does It Matter?

Swap is a storage area (either a dedicated partition or a file) that the operating system uses as an extension of physical RAM. When RAM is fully consumed, the Linux kernel moves the least-used memory pages to disk, freeing up space for active processes.

Key advantages:
- Prevents crashes caused by insufficient RAM
- Allows running demanding applications on a budget VPS plan
- Buys time for proper diagnosis and infrastructure scaling

Drawbacks to keep in mind:
- Disk storage (even SSD/NVMe) is slower than RAM
- Excessive swapping degrades application performance
- Swap is not a permanent replacement for insufficient RAM

Ground rule: Swap is a safety net, not a long-term solution. If your server constantly uses swap, it's time to upgrade your VPS plan.


Check if Swap Is Already Active

Before configuring anything, check the current memory and swap status:

free -h

Example output:

              total        used        free      shared  buff/cache   available
Mem:          1.9Gi       800Mi       300Mi        20Mi       850Mi       1.0Gi
Swap:           0B          0B          0B

If the Swap line shows 0B, no swap is configured. You can also run:

swapon --show

If it returns nothing, proceed with the steps below.


How Much Swap Do You Need?

General guidelines for modern systems:

Available RAM Recommended Swap
< 2 GB 2× RAM (e.g., 4 GB)
2–8 GB Equal to RAM (4–8 GB)
8–64 GB At least 4 GB, max 8 GB
> 64 GB Swap optional, situational

For a VPS NVMe with 2 GB of RAM, a 2–4 GB swap file offers a great balance between protection and performance.


Step 1 – Create the Swap File

Use fallocate to quickly allocate space (or dd if fallocate is unavailable):

sudo fallocate -l 2G /swapfile

Or with dd:

sudo dd if=/dev/zero of=/swapfile bs=1M count=2048

Verify the file was created:

ls -lh /swapfile

Step 2 – Set Correct Permissions

The swap file must be accessible only by root, otherwise Linux will refuse to activate it:

sudo chmod 600 /swapfile

Verify:

ls -lh /swapfile
# -rw------- 1 root root 2.0G ...

Step 3 – Format the File as Swap Space

sudo mkswap /swapfile

Expected output:

Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Step 4 – Enable the Swap File

sudo swapon /swapfile

Confirm it's active:

swapon --show
free -h

The Swap line should now display the configured size.


Step 5 – Make Swap Persistent Across Reboots

The activation above is temporary — swap disappears after a reboot. To make it permanent, add an entry to /etc/fstab:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Verify the entry was added:

cat /etc/fstab | grep swap

From now on, every time your Linux VPS restarts, swap will be activated automatically.


Step 6 – Tune the Swappiness Parameter

Swappiness controls how aggressively the Linux kernel uses swap versus RAM. The default value is 60, on a scale of 0 to 100.

  • 0 = kernel avoids swap as much as possible
  • 10–20 = recommended for web application servers (WordPress, Node.js, etc.)
  • 60 = Ubuntu/Debian default
  • 100 = kernel uses swap heavily

For a web server running on a VPS SSD, a value of 10 is recommended:

sudo sysctl vm.swappiness=10

To persist after reboot:

echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

Step 7 – Tune Cache Pressure

The vfs_cache_pressure parameter controls how quickly the kernel releases inode and dentry caches:

sudo sysctl vm.vfs_cache_pressure=50
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf

The default is 100. Lowering it to 50 keeps more data cached in memory — beneficial for busy web servers.


How to Monitor Swap Usage

Periodic monitoring is essential to understand your server's memory behaviour:

# General memory and swap usage
free -h

# Processes sorted by RAM consumption
ps aux --sort=-%mem | head -10

# Swap statistics
vmstat -s | grep -i swap

# Continuous monitoring (updated every 2 seconds)
watch -n 2 'free -h && swapon --show'

If swap is consistently used above 50%, it's a clear sign your VPS needs more RAM. Browse the available VPS NVMe plans for a seamless upgrade.


How to Disable or Remove Swap

If you've upgraded RAM or no longer need swap:

# Deactivate swap
sudo swapoff /swapfile

# Remove the entry from /etc/fstab (edit manually)
sudo nano /etc/fstab

# Delete the file
sudo rm /swapfile

Swap on SSD vs HDD: What You Need to Know

On a VPS with SSD or NVMe storage (like the VPS SSD and VPS NVMe plans at CLIQHOST), swap performs significantly faster than on a traditional HDD. The reduced latency of solid-state storage makes memory paging far less punishing for running applications.

However, SSD write cycles are finite. That's why:
- Keep swappiness low (10–20)
- Use swap as a safety net, not a primary memory resource
- Monitor SSD wear with smartctl if you have bare-metal access


Troubleshooting Common Issues

Error: "swapon: /swapfile: insecure permissions 0644"

sudo chmod 600 /swapfile

Error: "fallocate failed: Operation not supported"

Some filesystems (e.g., Btrfs) do not support fallocate for swap. Use dd instead:

sudo dd if=/dev/zero of=/swapfile bs=1M count=2048

Swap not activating after reboot

Check the syntax in /etc/fstab:

sudo mount -a
# If an error appears, correct the swap line in fstab

Conclusion

Configuring a swap file on a Linux VPS is one of the simplest and most effective ways to add a layer of stability to your server. With just a few commands, you protect your applications from OOM crashes and gain the breathing room needed to scale properly.

If you're running a production environment and notice swap being used frequently, the CLIQHOST team is available for consultation and a smooth migration to a higher-resource plan. Explore our VPS SSD and VPS NVMe plans — built for performance, reliability, and growth.

SHARE
// what clients say

What Our Clients Say

Real reviews from customers who trust CLIQHOST for performance, reliability and expert technical support.

★★★★★

"We moved our online shop from a foreign host and the difference is night and day — pages load instantly and support replies in minutes, in Romanian."

AM
Andrei M.
eCommerce owner · Chișinău
★★★★★

"Migrated 12 client sites to CLIQHOST. Free migration, zero downtime, and the cPanel setup is exactly what my team needed. Highly recommend."

EV
Elena V.
Web agency · Bălți
★★★★★

"Our NVMe VPS handles traffic spikes without a sweat. Full root, local datacenter, and billing in MDL — everything we wanted from a provider."

DC
Dmitri C.
SaaS founder · Chișinău