Candid photograph of a Linux server terminal with a soft olive-green glow against a dark slate background, conveying a calm technical atmosphere.

Step-by-step guides for system administrators — covering command-line basics, web server setup, and preparation material for technical interviews.

Browse Tutorials

How to Add a Swap File on RHEL 9 for Linux Administrators

Swap space acts as a safety valve when physical memory fills up, allowing the Linux kernel to move less-active pages from RAM to disk until they are needed again. On modern servers with abundant memory this rarely matters, but smaller instances and workloads with bursty demand still benefit from having some configured.

RHEL 9 follows the broader trend of Red Hat-derived distributions toward tighter defaults around partitioning and memory management, and many installation templates either omit swap entirely or leave it disabled when LVM auto-partitioning is chosen. That makes manual configuration a routine task for anyone managing a freshly deployed system.

In Australia, plenty of local providers such as Macquarie Telecom, Servers Australia and the AWS Sydney region ship base images with swap turned off, so administrators in Brisbane or Melbourne offices frequently inherit cloud VMs that need this small but important fix before they are production-ready. Knowing how to add it yourself avoids waiting on a support ticket during business hours, which on the east coast aligns with AEST.

This walkthrough covers the steps needed to create, activate and persist a swap file on a RHEL 9 host, along with tuning and verification commands you can run immediately afterwards.

Understanding Swap Space and Its Role

Swap is essentially designated disk space that the kernel treats as an extension of RAM. When active pages exceed available memory, the system writes older or less-used pages to this space and frees physical memory for new requests. The process is transparent to applications, although it does come with a latency cost compared with RAM access.

For most modern workloads running on NVMe-backed storage in data centres around Sydney or Perth, the performance penalty is modest and rarely noticeable. Swap is still useful for catching short spikes, supporting suspend states, and protecting the out-of-memory killer from terminating critical services when a workload briefly exceeds its memory allowance.

It is worth noting that swap is not a replacement for adequate RAM. Heavy reliance on swap typically signals that an instance is undersized, and the right response is usually to resize it rather than to grow the swap file indefinitely. For learning environments and small VMs, however, a few gigabytes of swap is a sensible safety net.

Checking Current Memory and Swap Status

Before adding anything, confirm the current state of memory and swap using free -h and swapon --show. These commands display total, used and available RAM, plus any active swap devices or files. On a default RHEL 9 cloud image you will usually see a swap row with no entries, indicating nothing is configured.

It is also worth checking available disk space with df -h on the target filesystem. Swap files consume real storage, and on a 20 GB root volume common with Australian budget VPS providers there is rarely room for more than a couple of gigabytes without trimming other directories. Choosing a size between one and two times the installed RAM is a common starting point for small servers, while larger hosts may only need a fixed 2 to 4 GB buffer.

Creating the Swap File on RHEL 9

The fastest method is to use fallocate to reserve the space immediately, then lock the file down with strict permissions. As root, run fallocate -l 2G /swapfile to create a 2 GB file, followed by chmod 600 /swapfile so that only root can read or write it. The permissions step matters: a world-readable swap file is treated as a security weakness by most compliance scanners.

With the file in place, format it as swap using mkswap /swapfile and label the resulting area. RHEL 9 defaults to XFS on most installations, which works fine for this purpose, although Btrfs requires a different workflow and is rarely used as the root filesystem on RHEL anyway. If your filesystem does not support fallocate, the older dd if=/dev/zero of=/swapfile bs=1M count=2048 achieves the same result more slowly.

For administrators in Canberra or Adelaide government environments who manage many similar hosts, scripting these steps into a small bash snippet or an Ansible role saves a lot of repetitive typing and keeps the file size consistent across the fleet.

Enabling and Making the Swap File Persistent

Activate the swap immediately with swapon /swapfile, then make the change survive reboots by adding an entry to /etc/fstab. The recommended line uses the file path rather than a UUID, since UUIDs are not generated for swap files in the same way they are for block devices:

/swapfile none swap defaults 0 0

After saving the file, test the entry with swapon -a or simply reboot to confirm the system picks it up automatically. If you ever need to disable the swap file, run swapoff /swapfile and remove the corresponding line from /etc/fstab.

Some teams prefer to manage related text-editor configuration through emacs-based tooling; if you want to read about that side of system administration, you will find useful material at alternative editor guides.

Tuning Swappiness and System Behaviour

The vm.swappiness sysctl controls how aggressively the kernel moves pages from RAM to swap, with values ranging from 0 to 200 on RHEL 9. A lower value keeps more data in physical RAM, which suits database servers and other latency-sensitive workloads, while a higher value favours freeing RAM for caches.

To set a value for the current session, use sysctl vm.swappiness=10. To make it permanent, drop a file such as /etc/sysctl.d/90-swappiness.conf containing vm.swappiness = 10 and reload with sysctl --system. Most general-purpose servers in Australia running mixed workloads are well served by a value between 10 and 30.

Verifying the Configuration Safely

After applying the changes, confirm everything is in place with free -h, swapon --show and cat /proc/swaps. These all report slightly different views, so cross-checking them catches inconsistencies. Watching the si and so columns in vmstat 1 for a few minutes shows whether the system is actually using the new space.

Before rolling the same change out across production systems, consider testing it inside an isolated environment so that any mistakes can be caught without affecting live services. A useful walkthrough on chroot testing walks through setting up a chroot for safe experimentation on CentOS-based hosts, which translates directly to RHEL 9.

A swap file is one of the smallest but most reliable pieces of insurance on a Linux host. Adding one on RHEL 9 takes only a handful of commands once you have a clear picture of the available disk space and the workload's memory profile, and the result is a system that handles short memory spikes far more gracefully than one without any swap configured.