Swap space gives the Linux kernel an overflow area on disk when physical RAM is fully utilized, preventing out-of-memory crashes on memory-constrained servers. On RHEL 8, swap can be implemented as a dedicated partition or as a swap file — the latter being more flexible since it can be created, resized, and removed without repartitioning the disk. This guide covers checking existing swap, creating a swap file, making it persistent across reboots with /etc/fstab, tuning swappiness, and optionally adding a swap partition with fdisk.
Prerequisites
- A running RHEL 8 server with root or sudo access
- Sufficient free disk space for the desired swap size (a minimum of 1–2 GB is typical)
- Basic familiarity with disk management and
/etc/fstab
Step 1 — Check Current Swap Usage
Before adding swap, inspect what is currently configured. The two most useful commands are free and swapon:
# Human-readable overview of RAM and swap
free -h
# Detailed list of active swap devices and files
swapon --show
free -h shows total, used, and available swap in the Swap: row. If swapon --show produces no output, no swap is currently active. Also check /proc/swaps for a kernel-level view:
cat /proc/swaps
Step 2 — Create a Swap File
Use fallocate to pre-allocate a contiguous 2 GB file. Contiguous allocation is important for swap performance — fallocate is preferred over dd because it is significantly faster on modern filesystems:
sudo fallocate -l 2G /swapfile
Note: On filesystems that do not support fallocate (e.g., XFS with certain configurations or Btrfs), use dd instead:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
Restrict permissions on the swap file so only root can read or write it — this is a security requirement:
sudo chmod 600 /swapfile
ls -lh /swapfile
Step 3 — Format and Activate the Swap File
Format the file as swap space, then activate it for immediate use in the current session:
# Write swap signature to the file
sudo mkswap /swapfile
# Enable the swap file now (active until next reboot)
sudo swapon /swapfile
Verify it is active:
swapon --show
free -h
The swapon --show output should list /swapfile with type file, the configured size, and current usage.
Step 4 — Make Swap Persistent with /etc/fstab
The swapon command activates swap only until the next reboot. To make it permanent, add an entry to /etc/fstab. Open the file:
sudo vi /etc/fstab
Add the following line at the end of the file:
/swapfile none swap sw 0 0
Verify the fstab entry is syntactically correct without rebooting:
sudo mount -a
If this command returns no errors, the fstab entry is valid. The swap entry will not cause errors here because mount -a skips swap entries — but the command confirms there are no parse errors in the file.
Step 5 — Tune Swappiness
The vm.swappiness kernel parameter controls how aggressively the kernel moves memory pages to swap. The default value is 30 on RHEL 8 (previously 60 on older systems). A lower value keeps more data in RAM; a higher value moves data to swap earlier.
# Check current swappiness
cat /proc/sys/vm/swappiness
# Set swappiness temporarily (lost on reboot)
sudo sysctl vm.swappiness=10
# Make the change permanent
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
For database servers (PostgreSQL, MariaDB), a swappiness of 1 to 10 is commonly recommended to avoid thrashing. For general-purpose servers, the default of 30 is typically appropriate.
Step 6 — Add a Swap Partition with fdisk (Alternative Method)
If you prefer a dedicated swap partition on an additional disk (e.g., /dev/sdb), use fdisk to create and format it. This method is common when provisioning a new disk specifically for swap:
# Start fdisk on the target disk (replace /dev/sdb with your disk)
sudo fdisk /dev/sdb
Inside fdisk, use these commands: n (new partition), accept defaults for size, then t (change type) and enter 82 (Linux swap), then w (write changes). After exiting:
sudo mkswap /dev/sdb1
sudo swapon /dev/sdb1
swapon --show
Add the partition to /etc/fstab using its UUID for reliability:
sudo blkid /dev/sdb1
# Add to /etc/fstab (replace UUID with the value from blkid output):
UUID= none swap sw 0 0
Conclusion
You have checked existing swap, created and activated a 2 GB swap file on RHEL 8, made it persistent via /etc/fstab, and tuned vm.swappiness via /etc/sysctl.conf. You also have an alternative path using a dedicated swap partition with fdisk. Swap is a safety net — if your server is regularly using it heavily, that is a signal to add more RAM rather than rely on swap for sustained workloads.
Next steps: How to Monitor System Resources with htop, top and vmstat on RHEL 8, How to Configure Automatic Security Updates on RHEL 8, and How to Manage Disk Partitions with fdisk and parted on RHEL 8.