How to Create and Manage Swap Space on RHEL 7

Swap space is a designated area on disk that the Linux kernel uses as an overflow when physical RAM is exhausted. While swap is not a substitute for adequate RAM — disk I/O is orders of magnitude slower than memory access — it is a critical safety net that prevents the Out-Of-Memory (OOM) killer from terminating processes during memory pressure spikes. On RHEL 7, you may need to add or expand swap space after initial installation when workloads grow beyond the originally provisioned memory, when running memory-intensive applications like databases or Java processes, or when deploying on virtual machines with limited RAM. This tutorial covers every aspect of swap management on RHEL 7: checking existing swap, creating new swap, making it persistent, tuning swappiness, and safely removing swap.

Prerequisites

  • RHEL 7 system with root or sudo access
  • Sufficient free disk space for the swap file or partition
  • Basic familiarity with the command line and /etc/fstab

Step 1: Check Current Swap Space

Before creating new swap, check what is already configured on the system. Use free -h for a human-readable summary:

free -h

Example output:

              total        used        free      shared  buff/cache   available
Mem:           3.7G        1.2G        800M        100M        1.7G        2.2G
Swap:          2.0G        128M        1.9G

The Swap row shows total configured swap and how much is in use. A value of 0B for total means no swap is configured.

For more detail on swap devices and files, use swapon --show:

swapon --show

Example output:

NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition   2G 128M   -1

This shows the device or file, its type (partition or file), total size, current usage, and priority. If this command produces no output, no swap is currently active.

You can also check /proc/swaps directly:

cat /proc/swaps

Step 2: Determine How Much Swap You Need

Red Hat’s general guidance for swap sizing on RHEL 7 is:

  • Under 2 GB RAM: Swap = 2x RAM
  • 2–8 GB RAM: Swap = equal to RAM
  • 8–64 GB RAM: Swap = at least 4 GB
  • Over 64 GB RAM: Swap = at least 4 GB (often application-specific)
  • Hibernation support required: Swap must be at least equal to RAM size

For servers that do not hibernate and have sufficient RAM (8+ GB), a fixed 4–8 GB swap file is generally adequate as a safety net.

Step 3: Create a Swap File with dd or fallocate

You can create swap space as either a dedicated partition or a swap file. A swap file is more flexible as it does not require repartitioning. Two tools can create a swap file: dd (reliable, works on all filesystems) and fallocate (faster, but not recommended on Btrfs or XFS with extents).

Method A: Using dd (universally reliable)

Create a 2 GB swap file using dd. Note that on a busy system this may take a minute or two:

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

This writes 2048 blocks of 1 MB each, producing a 2 GB file of zeros.

Method B: Using fallocate (faster on ext4)

sudo fallocate -l 2G /swapfile

Verify the file was created:

ls -lh /swapfile

Expected output:

-rw-r--r-- 1 root root 2.0G May 17 10:00 /swapfile

Step 4: Set Correct Permissions on the Swap File

The swap file must be readable and writable only by root. Other permissions create a security risk (world-readable swap files can expose sensitive data held in memory):

sudo chmod 600 /swapfile
ls -lh /swapfile

Expected output:

-rw------- 1 root root 2.0G May 17 10:00 /swapfile

Step 5: Initialize the File as Swap with mkswap

Format the file with the swap signature:

sudo mkswap /swapfile

Example output:

Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=a1b2c3d4-e5f6-7890-abcd-ef1234567890

Note the UUID — this can be used in /etc/fstab as an alternative to the file path.

Step 6: Activate the Swap File

Enable the swap file immediately in the running system:

sudo swapon /swapfile

Verify it is active:

swapon --show
free -h

The swap should now appear in both outputs:

NAME      TYPE SIZE USED PRIO
/swapfile file   2G   0B   -2

Step 7: Make the Swap Persistent with /etc/fstab

The swap activated above will be lost on reboot. To make it persistent, add an entry to /etc/fstab:

sudo vi /etc/fstab

Add the following line at the end of the file:

/swapfile   swap   swap   defaults   0 0

The fields are: device/file path, mount point (swap), filesystem type (swap), mount options, dump flag, and fsck pass order. Both the dump and pass fields must be 0 for swap.

Verify the /etc/fstab entry is correct by testing the mount without rebooting:

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

The swapon -a command activates all swap entries in /etc/fstab. If the swap file reappears in swapon --show, the /etc/fstab entry is working correctly.

Step 8: Tune Swappiness with sysctl vm.swappiness

The vm.swappiness kernel parameter controls how aggressively the kernel moves memory pages to swap. The value ranges from 0 to 100:

  • 0: The kernel avoids swapping as much as possible, only using swap when absolutely necessary
  • 10: Recommended for database servers (PostgreSQL, MySQL) where keeping data in RAM is critical
  • 30: A conservative setting for general servers
  • 60: The RHEL 7 default — acceptable for workstations and general-purpose servers
  • 100: The kernel aggressively swaps pages even when RAM is available

Check the current value:

cat /proc/sys/vm/swappiness

Set a new value for the running system immediately:

sudo sysctl vm.swappiness=10

To make the setting persistent across reboots, add it to /etc/sysctl.conf or create a drop-in file in /etc/sysctl.d/:

echo "vm.swappiness=10" | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl -p /etc/sysctl.d/99-swappiness.conf

For database workloads, also consider tuning vm.vfs_cache_pressure to retain directory and inode caches:

echo "vm.vfs_cache_pressure=50" | sudo tee -a /etc/sysctl.d/99-swappiness.conf
sudo sysctl -p /etc/sysctl.d/99-swappiness.conf

Step 9: Remove Swap Space

If you need to remove a swap file (for example, after adding more RAM and deciding swap is no longer needed), follow these steps:

First, deactivate the swap file:

sudo swapoff /swapfile

Verify it is no longer active:

swapon --show

Remove the /etc/fstab entry. Open the file and delete the swap line:

sudo vi /etc/fstab

Delete the swap file itself:

sudo rm /swapfile

If you are removing a swap partition rather than a file, use swapoff /dev/sdXY and remove the corresponding /etc/fstab entry, but leave the partition itself (it can be reclaimed with a partitioning tool during a maintenance window).

Troubleshooting

swapon fails with “Invalid argument”

The file was not properly initialized with mkswap, or permissions are not 600. Re-run mkswap and check permissions.

Swap file on XFS not working with fallocate

XFS does not support fallocate-allocated swap files in some configurations. Use dd instead to create a fully allocated file.

Swap not activated after reboot

The /etc/fstab entry is missing or incorrect. Verify it has exactly four columns: path, swap, swap, defaults, and both trailing zeros.

Managing swap space on RHEL 7 is a fundamental skill for any system administrator. Whether you are adding swap to an underpowered VM, tuning swappiness for a database workload, or cleaning up legacy swap partitions after a hardware upgrade, the tools and techniques covered here — free, swapon, mkswap, /etc/fstab, and vm.swappiness — give you complete control over how your system handles memory pressure.