• Post author:Bhaskar Soni
  • Post published:
  • Post category:Linux
  • Post comments:2 Comments
  • Reading time:3 mins read

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.

swap partition

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:

RAMSuggested swap (no hibernation)With hibernation
Up to 2 GB2 × RAM3 × RAM
2 to 8 GBEqual to RAM2 × RAM
8 to 64 GBAt least 4 GB1.5 × RAM
Over 64 GBAt least 4 GBNot 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 -h

Option 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.

  1. Set the partition type to Linux swap in fdisk or parted, if needed.
  2. Format it as swap:
sudo mkswap /dev/sdb2
  1. Enable it:
sudo swapon /dev/sdb2
  1. Make it permanent by adding the UUID to /etc/fstab. First get the UUID with blkid /dev/sdb2, then add:
UUID=your-uuid-here  none  swap  sw  0 0

Option 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/fstab

If 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 --system

zram: 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 /swapfile

Then 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.

Post author avatar

Bhaskar Soni

Bhaskar Soni is a Security Engineer and the founder of Youngster Company, based in Ahmedabad, Gujarat. He works hands-on with Linux server hardening, vulnerability assessment and penetration testing (VAPT), ISO 27001 compliance and OSINT investigations. He writes practical, tested guides for IT professionals and Indian businesses, and every article on this site is written or reviewed by him.

This Post Has 2 Comments

Leave a Reply