How to Add Swap Space on Rocky Linux 8 (Step-by-Step)

Add swap space on Rocky Linux 8 with a secure 2 GiB file, persistent boot setup, careful tuning, verification checks, and fixes for common errors.
A glowing swap storage stack connected to a server and memory module on a midnight-blue background.

Add swap space on Rocky Linux 8 by creating a protected 2 GiB file and enabling it for the current session and future boots. You can finish the setup in about five to ten minutes, giving a small VPS a disk-backed buffer for short memory spikes.

Prerequisites

  • Rocky Linux 8, preferably updated to the latest available 8.x minor release, with at least 2.5 GiB free on the filesystem mounted at /.
  • SSH access as root, or a regular account with working sudo privileges.
  • Permission to create /swapfile; that path must not contain an application file or an active swap area.
  • A VPSLake Linux VPS if you need a Rocky Linux server for this procedure.

Swap is much slower than RAM. The 2 GiB example below can absorb a temporary shortage, but frequent swap activity is a sign that the workload needs more memory or tighter limits.

Step 1: Inspect the server before creating swap

Check the Rocky Linux release, filesystem capacity, and existing swap first so you do not overwrite a useful file or fill the root volume.

cat /etc/rocky-release
findmnt -no FSTYPE /
df -h /
free -h
swapon --show
if [ -e /swapfile ]; then ls -lh /swapfile; else echo "/swapfile is unused"; fi

On a new server, the final commands normally show no active swap rows and an unused target path:

Rocky Linux release 8.x (Green Obsidian)
xfs
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        40G  7.0G   33G  18% /
               total        used        free      shared  buff/cache   available
Mem:            2.0Gi       430Mi       1.0Gi        12Mi       570Mi       1.4Gi
Swap:              0B          0B          0B
/swapfile is unused

The device name, memory totals, and filesystem type can differ. If swapon --show already lists a swap partition or file, keep it unless you have a specific reason to add another. If /swapfile exists, stop and choose another unused path such as /swapfile-2g.

Step 2: Create and secure a 2 GiB swap file

Write the file fully, set root-only permissions, and add a swap signature; this avoids sparse-file surprises and keeps users without administrative access from reading pages written to disk.

The dd method is deliberately compatible with common Rocky Linux 8 XFS and ext4 layouts:

sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile
sudo mkswap /swapfile

dd writes 2,048 MiB, so it may take a little longer than an allocation-only command. mkswap should report a new UUID and a size close to 2 GiB:

Setting up swapspace version 1, size = 2 GiB
no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

For the compatible Red Hat Enterprise Linux 8 procedure behind Rocky Linux 8, see the official RHEL 8 swap documentation. Do not rerun the creation command against a path that is already active.

Step 3: Turn on swap for this boot

Formatting prepares the file but does not make the kernel use it; swapon activates it immediately without a restart.

sudo swapon /swapfile
sudo swapon --show

You should see a row similar to this:

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

The priority can differ if another swap device is present. The important checks are that /swapfile appears and its capacity is close to the size you created.

Step 4: Persist the file across reboots

Add one /etc/fstab record so Rocky Linux enables the same file during startup, while the guard prevents duplicate entries if you run the command again.

if ! sudo grep -qE '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab; then
  echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab >/dev/null
fi
sudo systemctl daemon-reload
sudo grep -nE '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab

The last command should show one matching line:

/swapfile none swap defaults 0 0

Swap is already active from Step 3. The /etc/fstab entry is what restores it after a reboot, so you do not need to restart just to use the new space. If you had to choose a different filename in Step 1, substitute that exact path in both commands and in later checks.

Step 5: Review swappiness and make an optional adjustment

The vm.swappiness value influences how readily Linux moves inactive pages from RAM to disk, so inspect the current setting before changing it for a particular workload.

sysctl vm.swappiness

If the server should keep active data in RAM and use swap mainly during pressure, test a value of 10 for the current boot:

sudo sysctl vm.swappiness=10
sysctl vm.swappiness

If monitoring shows that this setting fits the workload, save it in a dedicated file and reload that file:

printf '%s\n' 'vm.swappiness = 10' | sudo tee /etc/sysctl.d/90-swap.conf
sudo sysctl -p /etc/sysctl.d/90-swap.conf

Expected output is:

vm.swappiness = 10

Do not set swappiness to zero simply to hide disk activity. Constant swapping usually means the VPS needs more RAM or the application needs a lower memory footprint.

Verify the Rocky Linux 8 swap setup

Run these checks together to confirm that the kernel sees the file, the file is protected, and the boot configuration contains one matching entry.

sudo swapon --show
cat /proc/swaps
free -h
sudo stat -c '%A %U:%G %n' /swapfile
sudo grep -E '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab

Successful output includes an active 2 GiB file, a nonzero swap total, root-only permissions, and the persistent record:

NAME      TYPE  SIZE USED PRIO
/swapfile file    2G   0B   -2
/swapfile file 2097148      0   -2
Swap:          2.0Gi          0B     2.0Gi
-rw------- root:root /swapfile
/swapfile none swap defaults 0 0

The exact block counts and memory figures vary by kernel and server. If you want to test the boot path, schedule a maintenance window and reboot; do not deactivate the only swap area on a memory-stressed server just for a test.

Troubleshooting

swapon: /swapfile: swapon failed: Invalid argument

The file may be sparse, incomplete, or stored on a filesystem layout that rejects it as swap. Confirm it is not active with sudo swapon --show, remove the inactive file, and recreate it with the dd sequence in Step 2. Do not delete a path that appears in the active swap table.

dd: error writing '/swapfile': No space left on device

The root filesystem does not have 2 GiB available. Check df -h /, keep enough room for updates and logs, and use a smaller 1 GiB file only after confirming the failed file is inactive:

sudo swapon --show
sudo rm /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024 status=progress
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

The /etc/fstab path stays valid when the file size changes. Never run the rm command while /swapfile is listed by swapon --show.

swapon: /swapfile: swapon failed: Device or resource busy

The file is already active or another process has it open. Run sudo swapon --show and ls -l /swapfile; if it is already listed, keep it and remove only duplicate /swapfile lines from /etc/fstab.

swapon: /swapfile: swapon failed: Operation not permitted

Some container-based VPS environments prohibit guest-managed swap. This is a virtualization policy rather than a missing Rocky Linux command, so request host-managed swap or use a virtualization plan that allows swap files.

Next steps

  • Watch free -h and vmstat 5 during the workload that prompted you to add swap; sustained swap-in and swap-out activity needs attention.
  • Leave free disk space for Rocky Linux updates, logs, and application data instead of filling the volume with swap.
  • If the server regularly approaches its memory limit, resize the VPS or move the workload rather than treating swap as a RAM replacement.

FAQ

How much swap does a Rocky Linux 8 VPS need?

There is no universal RAM-to-swap formula. Two GiB is a practical starting point for a small VPS when the disk has room, but size it around short-lived memory peaks and preserve space for the operating system and applications.

Does swap increase physical RAM?

No. Swap uses slower disk storage as virtual memory and can postpone an out-of-memory failure during a brief spike. It cannot deliver the performance or capacity of additional RAM.

Is a swap partition better than a swap file?

A partition can work, but a file is usually easier on a VPS because you can resize it without repartitioning the disk. Both require a swap signature, restrictive permissions where applicable, activation with swapon, and a persistent boot configuration.

Can I remove or resize the swap file later?

First identify the exact path with swapon --show, then move the workload away from memory pressure and deactivate that path with swapoff. Remove only its /etc/fstab entry, create the replacement, and verify the new area before deleting the old file.

Samrat Ghosh
Written by

Samrat Ghosh

Founder & Infrastructure Engineer

Samrat Ghosh is the founder of VPSLake, where he builds and runs the remote desktop and VPS hosting infrastructure the company is built on. He writes hands-on guides about RDP, Windows Server, VPS management and secure remote access - the practical documentation he wishes had…

Previous Article

MQL5 Virtual Hosting vs VPS for MT5 Traders

Next Article

How to Change Hostname in AlmaLinux 10 (Step-by-Step)

Write a Comment

Leave a Comment

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

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨