Setting up swap space on a Linux server

Step-by-step guide to creating Linux swap, determining the right size, configuring swappiness, and fixing common errors. A practical solution to prevent server crashes during RAM shortage.

6 min Updated 22 Aug 2026

Why Do You Need Swap Space?

When your server's RAM is full, the Linux kernel has two choices: either kill processes with an Out Of Memory error, or move some less-used data to disk. Linux swap space is designed exactly for this scenario. This space acts as auxiliary memory and allows the system to maintain its performance instead of crashing suddenly when actual RAM runs out.

The important point is that swap is not a replacement for RAM. Read/write speed on disk (even NVMe) is several times slower than RAM. However, in critical situations, having swap can be the difference between a live server and a dead one. Especially for services like MySQL, Redis, or web servers that suddenly face heavy traffic, having swap is a vital insurance policy.

When Is Creating Swap Necessary?

Not all servers need swap, but in these cases you should definitely enable it:

  • Servers with low RAM (less than 2GB): If your server has limited memory, swap is essential to prevent crashes.
  • Resource-intensive applications: Databases, virtualization environments, and analytical software usually need more memory than expected.
  • Shared servers: If you run multiple services on one server, RAM consumption fluctuations are inevitable.
  • Critical systems: For servers where uptime is critical, swap adds an extra protective layer.

On the other hand, if your server has more than 16GB of RAM and your memory usage is typically below 70%, you probably don't need much swap. In this case, enabling swap can even reduce performance, because the kernel might move less-used data to disk and later waste time bringing it back.

Determining the Right Size for Swap

The swap size depends on two main factors: the amount of RAM and the type of workload. An old rule said swap should be twice the RAM, but this rule is outdated for modern systems. Today's common recommendation is as follows:

  • RAM less than 2GB: swap equal to 2 times RAM (e.g., for 1GB RAM, 2GB swap)
  • RAM between 2 and 8GB: swap equal to 1 to 1.5 times RAM
  • RAM between 8 and 64GB: swap equal to 0.5 to 1 times RAM
  • RAM above 64GB: swap equal to 0.25 to 0.5 times RAM

For example, a server with 4GB RAM running MySQL and Nginx needs 4 to 6GB of swap. But a server with 32GB RAM serving only a lightweight web application is perfectly fine with 8GB of swap.

Important Note About Hibernation

If you use the Hibernation feature, swap must be at least the size of RAM so it can store the entire memory contents. However, on servers this feature is usually disabled, so only consider this on desktops.

Methods for Creating Swap in Linux

There are two main methods for creating swap: using a dedicated partition or a swap file. The file method is simpler and more flexible, and it doesn't require repartitioning the disk. In this tutorial, we'll use the file method, which is the best option for most virtual servers.

Step 1: Check Current Status

Before anything else, check the current swap status:

sudo swapon --show
free -h

If the output of swapon --show is empty, swap is not active. The free -h command also shows complete memory information.

Step 2: Create the Swap File

To create a 4GB swap file, use the fallocate command:

sudo fallocate -l 4G /swapfile

If fallocate doesn't work on your system (e.g., on some older filesystems), use the dd command:

sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress

The second command writes 4096 1-megabyte blocks, totaling 4GB. Make sure to create the swap file on a disk with sufficient space. Check available space with the df -h command.

Step 3: Set Permissions

The swap file contains sensitive data, so you should restrict access:

sudo chmod 600 /swapfile

This command only allows root to read and write the file.

Step 4: Format and Activate

Format the file as swap and then activate it:

sudo mkswap /swapfile
sudo swapon /swapfile

The output of mkswap should look something like this:

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

Step 5: Enable Permanently

To make swap activate automatically after a server reboot, edit the /etc/fstab file:

sudo nano /etc/fstab

And add this line to the end of the file:

/swapfile none swap sw 0 0

Check the status again with sudo swapon --show and free -h to make sure swap is properly activated.

Configuring swappiness: The Key to Performance Optimization

The swappiness parameter determines how much the Linux kernel tends to use swap. Its value ranges from 0 to 100:

  • 0: Almost never uses swap (only in critical situations)
  • 10: Recommended value for database servers and latency-sensitive applications
  • 60: Default value in most distributions (suitable for desktops)
  • 100: Aggressive use of swap (suitable for systems with very little RAM)

To view the current value:

cat /proc/sys/vm/swappiness

To change it temporarily (until the next reboot):

sudo sysctl vm.swappiness=10

To change it permanently, edit the /etc/sysctl.conf file:

sudo nano /etc/sysctl.conf

And add this line:

vm.swappiness=10

Then apply the changes:

sudo sysctl -p

The Right swappiness Value for Your Server

If your server runs a database (MySQL, PostgreSQL), the value 10 is the best choice. This value makes the kernel use RAM as much as possible and only resort to swap when necessary. For general web servers, a value of 20 to 30 is a good balance. If you have very little RAM (less than 1GB), a value of 60 to 80 can prevent crashes, but it will come with reduced speed.

Common Errors and How to Fix Them

"swapon failed: Invalid argument" Error

This error usually occurs due to incorrect file formatting or using unsupported filesystems. Make sure you've run mkswap and that the file is on an ext4 or xfs filesystem. Filesystems like FAT32 and NTFS don't support swap.

"swapon failed: No space left on device" Error

This error indicates that your disk doesn't have enough space. Check available space with df -h and either create a smaller swap file or free up space.

Issue: Swap is Disabled After Reboot

If swap is not active after a reboot, you probably wrote the /etc/fstab line incorrectly. Make sure the file path is exactly correct and you're using the right syntax. Check the contents with the cat /etc/fstab command.

Important Note About SSD and Flash Storage

Heavy use of swap on an SSD can reduce the disk's lifespan, because each write to memory cells wears them out. If your server has an SSD, try to keep swappiness low and make sure swap is really only used when necessary. To monitor swap usage, you can use the vmstat 5 command and watch the si and so columns.

Summary

Creating Linux swap space is a simple but critical operation that can save your server from sudden crashes. By following the steps in this tutorial, you can enable swap on your server in less than 5 minutes. Remember that the right size and swappiness configuration are just as important as creating the swap itself. If your server has sufficient memory and a light workload, you might not need swap at all. But for most production servers, having swap is a smart preventive measure.

If you're looking for cloud infrastructure with optimal configuration for Linux servers, ServerNet's virtual and dedicated server services can be a suitable option. But in any case, you can implement this tutorial on any Linux server you have.

Was this page helpful?