+(91)70149-37521Subscribe Now

How to Add Swap Space on Rocky Linux 9 and EL9

A swap file gives Linux extra virtual memory when physical RAM is under pressure. It is slower than RAM, but it can give a busy server time to recover instead of immediately losing a process. This guide explains how to add swap space on Rocky Linux 9, AlmaLinux 9, RHEL 9, and CentOS Stream 9. […]

How to Add Swap Space on CentOS

A swap file gives Linux extra virtual memory when physical RAM is under pressure. It is slower than RAM, but it can give a busy server time to recover instead of immediately losing a process. This guide explains how to add swap space on Rocky Linux 9, AlmaLinux 9, RHEL 9, and CentOS Stream 9.

The original version of this page covered CentOS 7. CentOS Linux 7 reached end of life on June 30, 2024 and no longer receives normal updates. Do not build a new public server on it. Migrate an old CentOS 7 machine to a supported operating system before treating swap as a long-term fix.

What Swap Can and Cannot Do

Swap stores less-active memory pages on disk. It can help during a short memory spike, but it does not replace enough RAM. Heavy, continuous swap use makes a server slow and may point to an undersized machine, an application limit that needs tuning, or a memory leak.

  • Use a swap file when you need flexibility without repartitioning a disk.
  • Check the application’s memory guidance before choosing a size.
  • Keep enough free disk space for logs, updates, and application data.
  • Back up /etc/fstab before editing it.

Check Existing Swap and Disk Space

Log in with a sudo-capable account and inspect the current memory, active swap, filesystem type, and available space:

free -h
swapon --show
findmnt -no FSTYPE /
df -h /

An empty result from swapon --show means no swap is active. If swap already exists, decide whether the real problem is capacity or unusually high memory use before adding another file.

Choose a Practical Swap Size

There is no correct one-size-fits-all number. Red Hat recommends sizing swap for the system’s workload, not simply copying the amount of installed RAM. A small cloud server may benefit from 1–2 GiB, while a database or application vendor may have its own rule. Hibernation has separate requirements and is uncommon on servers.

The example below creates a 2 GiB file. Replace 2G only after checking the available disk space.

Create a 2 GiB Swap File on EL9

On modern ext4 and XFS filesystems, Red Hat documents fallocate as a fast way to allocate the file:

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

The permission step matters because swap can contain pieces of sensitive application data. Only root should be able to read or write the file. Confirm both the permission and active swap:

ls -lh /swapfile
swapon --show
free -h

The file should show mode -rw-------, and swapon --show should list /swapfile.

Make the Swap File Permanent

A manual swapon does not survive a reboot unless the file is registered in /etc/fstab. Make a dated backup first:

sudo cp -a /etc/fstab /etc/fstab.backup
echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab
sudo systemctl daemon-reload

Check that the entry appears only once:

grep -n '/swapfile' /etc/fstab

Test the entry without rebooting. Do this during a quiet period and make sure the machine currently has enough free RAM:

sudo swapoff /swapfile
sudo swapon -a
swapon --show

If swapon -a reports an error, restore the backup or correct the line before rebooting. A mistake in /etc/fstab can cause startup trouble.

Adjust Swappiness Carefully

The kernel setting vm.swappiness influences how readily Linux swaps anonymous memory. Check the current value:

sysctl vm.swappiness

Do not copy a low value blindly. The best setting depends on the workload, storage speed, and memory pressure. First monitor the server. To test a value such as 10 until reboot, use:

sudo sysctl vm.swappiness=10

If testing proves that the change helps, store it in a separate sysctl file:

echo 'vm.swappiness = 10' | sudo tee /etc/sysctl.d/90-swappiness.conf
sudo sysctl --system

Watch real behavior with tools such as free, vmstat 1, and your application metrics. Constant swap-in and swap-out activity needs investigation, not just a different number.

Remove a Swap File Safely

Make sure enough RAM is available before disabling swap. Removing a busy swap file on a memory-starved server can kill processes.

free -h
sudo swapoff /swapfile

Delete the exact /swapfile line from /etc/fstab, reload systemd, and verify that it is no longer active. Only then remove the file:

sudo systemctl daemon-reload
swapon --show
sudo rm /swapfile

Troubleshooting

swapon Reports an Invalid Argument

Confirm the filesystem supports a swap file and the file was created correctly. On unusual copy-on-write or network filesystems, use the filesystem vendor’s documented method or create a swap partition/LVM volume instead.

Permission Warning Appears

Run sudo chmod 600 /swapfile, then check it with stat -c '%a %n' /swapfile. The expected mode is 600.

The Server Uses Swap All the Time

Check per-process memory, application limits, recent traffic, and logs. Swap usage by itself is not always a fault, but ongoing swap I/O and poor response time indicate real memory pressure.

Frequently Asked Questions

Does swap make a server faster?

No. Disk is slower than RAM. Swap is a safety buffer and memory-management tool, not a performance upgrade.

Can a Linux server have more than one swap area?

Yes. Linux can use multiple swap files, partitions, or logical volumes. Use swapon --show to see all active areas and their priorities.

Should a database server use swap?

Follow the database vendor’s memory and swap guidance. A small safety buffer may help, but active swapping can badly affect database latency.

Conclusion

You have created a secure swap file, enabled it at boot, and learned how to monitor or remove it. If the server regularly depends on swap, investigate the workload and add RAM or tune the application instead of treating disk space as permanent memory.

For the next security step, review our guides to Linux server hardening and Linux user and group management.


Reviewed and updated: August 2026. The procedure was checked against the official Red Hat Enterprise Linux 9 storage documentation. The operating-system notice follows the official CentOS end-of-life announcement.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to Our Newsletter

Get free how-to tutorials and over 700+ courses. Seo tips, create a wordpress, or learn a new skill.