How To

How to Add Ubuntu Swap Space for Better System Stability

Ubuntu Swap Space helps Linux systems stay responsive when physical RAM starts running low. Instead of crashing applications or freezing the system, Ubuntu temporarily moves inactive memory data to disk-based swap storage.

This feature is especially useful on VPS servers, cloud instances, and low-memory systems. In many Ubuntu installations, swap may not be configured by default, making it important to create it manually for improved stability.

In this guide, you will learn how to create, enable, and optimize swap space on Ubuntu systems safely.

Why Ubuntu Swap Space Matters

Swap acts as backup memory for your Linux system. When RAM usage becomes too high, Ubuntu transfers less-used processes to swap storage. Although swap is slower than RAM, it prevents unexpected crashes and improves multitasking performance.

Systems running virtual machines, databases, Docker containers, or heavy browser workloads often benefit from additional swap memory.

Before creating swap, check whether your system already has active swap configured:

sudo swapon --show

You can also monitor memory usage with:

free -h

If swap usage displays 0B, your system likely has no configured swap space.

Create Ubuntu Swap Space on Linux

First, verify available disk storage:

df -h

Next, create a 4GB swap file:

sudo fallocate -l 4G /swap.img

If fallocate does not work on your filesystem, use:

sudo dd if=/dev/zero of=/swap.img bs=1024 count=4194304

Set secure permissions for the swap file:

sudo chmod 600 /swap.img

Now format the file as swap memory:

sudo mkswap /swap.img

Enable the swap file immediately:

sudo swapon /swap.img

To make the configuration permanent after reboot, add the swap entry to /etc/fstab:

/swap.img swap swap defaults 0 0

Finally, confirm the swap is active:

sudo swapon --show

Optimize Ubuntu Swap Space Performance

Ubuntu uses a kernel setting called swappiness to control how aggressively swap memory is used.

Check the current value:

cat /proc/sys/vm/swappiness

Most Ubuntu systems use a default value of 60. For servers and performance-focused workloads, many administrators prefer a lower value such as 10.

Temporarily change swappiness:

sudo sysctl vm.swappiness=10

To keep the setting after reboot, add this line to /etc/sysctl.conf:

vm.swappiness=10

Lower swappiness values reduce unnecessary disk swapping and improve responsiveness on systems with enough RAM.

Common Ubuntu Swap Space Issues

Some systems may show errors during swap creation. If Ubuntu reports insecure permissions, reapply the correct permissions using:

sudo chmod 600 /swap.img

If swap does not activate after reboot, verify the /etc/fstab entry carefully and check it using:

sudo findmnt --verify

Insufficient disk space can also prevent swap creation, so always confirm available storage before allocating large swap files.

Conclusion

Setting up Ubuntu Swap Space is one of the easiest ways to improve Linux system reliability and memory management. Whether you run Ubuntu on a cloud server, VPS, or desktop machine, properly configured swap helps prevent crashes during high memory usage.

By creating a swap file, enabling it permanently, and tuning swappiness values, you can keep Ubuntu running smoothly even under heavy workloads.

Cyber Defence

Recent Posts

Install Apache on Ubuntu 20.04: Setup and Virtual Host Guide

Apache is one of the most widely used open-source web servers in the world. It is…

11 hours ago

Add Swap Space on Ubuntu 20.04: Create, Enable, and Tune

Swap space is an area on disk that Linux uses when it runs out of physical…

11 hours ago

Install Zoom on Ubuntu 20.04: Download, Setup, and Remove

Zoom is one of the most widely used video conferencing platforms. Zoom works on Windows, macOS,…

11 hours ago

Install Webmin on Ubuntu 20.04: Complete Setup and Login Guide

Webmin is an open-source web-based control panel for Linux servers. It gives you a browser interface…

11 hours ago

Install MariaDB on Ubuntu 20.04: Setup and Admin Access

MariaDB is an open-source relational database management system. It was created by the original MySQL developers…

11 hours ago

Best OSINT Tools for Investigating Corruption 2026: Public Records and Link Analysis

Corruption investigations need accuracy, patience, and strong evidence. In 2026, OSINT tools can help researchers,…

12 hours ago