Swap is disk space that Linux uses when RAM runs low. The kernel moves inactive memory pages to swap, freeing RAM for active work. Swap can prevent crashes under memory pressure, but it is much slower than RAM. This guide explains how swap works and how to create a swap partition or a swap file.

How swap works
When memory fills up, the kernel picks pages that have not been used recently and writes them to swap. If a program needs them again, the kernel reads them back, which causes a delay. A little swap use is normal. Constant heavy swapping, however, means the system needs more RAM.
Swap also supports hibernation, where the whole RAM content is saved to disk before power-off.
How much swap do you need?
There is no single rule. Common guidance:
| RAM | Suggested swap (no hibernation) | With hibernation |
|---|---|---|
| Up to 2 GB | 2 × RAM | 3 × RAM |
| 2 to 8 GB | Equal to RAM | 2 × RAM |
| 8 to 64 GB | At least 4 GB | 1.5 × RAM |
| Over 64 GB | At least 4 GB | Not usually recommended |
These figures follow typical distribution guidance, such as Red Hat’s. Servers that must never swap may use little or none, but a small amount still provides a safety margin.
Check current swap
swapon --show
free -hOption 1: create a swap partition
In this example, /dev/sdb2 is an empty partition. Warning: check the device name with lsblk, because this erases data.
- Set the partition type to Linux swap in
fdiskorparted, if needed. - Format it as swap:
sudo mkswap /dev/sdb2- Enable it:
sudo swapon /dev/sdb2- Make it permanent by adding the UUID to
/etc/fstab. First get the UUID withblkid /dev/sdb2, then add:
UUID=your-uuid-here none swap sw 0 0Option 2: create a swap file
A swap file is easier to add and resize, and it works well on cloud servers.
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabIf fallocate is not supported on your file system, use dd if=/dev/zero of=/swapfile bs=1M count=2048 instead. On Btrfs, follow the distribution’s special instructions for swap files.
Tune swappiness
The vm.swappiness setting controls how readily the kernel swaps. The default is often 60. Many servers lower it to keep more data in RAM:
cat /proc/sys/vm/swappiness
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --systemzram: compressed swap in RAM
Some distributions, such as Fedora, use zram, which compresses pages and keeps them in RAM instead of on disk. It is faster than disk swap and reduces SSD writes, which suits laptops and small servers.
Remove swap
sudo swapoff /swapfile
sudo rm /swapfileThen delete the matching line from /etc/fstab.
Related guides
Sources
Running Linux servers in production? Our Linux server hardening & security audit checks configuration, patching, SSH and logging. You can also use our free Linux server hardening checklist, or contact us.

Pingback: What is LVM Partition and how to create it on Linux - Youngster Company
Pingback: Linux Interview Questions - Youngster Company