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

Bash Scripting Best Practices Every Beginner Should Know

Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…

7 hours ago

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

8 hours ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

12 hours ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

13 hours ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

14 hours ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

15 hours ago