How to Add Swap Space on AlmaLinux 8

Add swap space on AlmaLinux 8 with a protected 2 GB swap file, persistent boot setup, safe tuning, verification, and practical troubleshooting steps.
A glowing swap file represented by a storage block beside a midnight-blue Linux server.

Add swap space on AlmaLinux 8 by creating a protected 2 GB swap file and enabling it immediately and at boot. You can complete the setup in about five to ten minutes, giving a small VPS extra disk-backed memory for short periods of RAM pressure.

Prerequisites

  • An AlmaLinux 8 server, preferably updated to the latest AlmaLinux 8 minor release, with at least 2 GB of free space on / for this example.
  • SSH access as root, or a normal account with working sudo privileges.
  • The path /swapfile must not already be an active swap file or a regular file you need to keep.
  • A Linux VPS from VPSLake if you need an AlmaLinux server for this procedure.

Swap is slower than RAM, so it is an emergency buffer rather than a substitute for adding memory. The commands below use a 2 GB file; reduce that size if your disk is small.

Step 1: Inspect memory, disk, and current swap

Check the server first so you do not create duplicate swap or consume the last free space on the root filesystem.

cat /etc/almalinux-release
free -h
swapon --show
df -h /

On a server with no active swap, swapon --show returns no rows. The other checks should look similar to this abbreviated example:

AlmaLinux release 8.10 (Cerulean Leopard)
               total        used        free      shared  buff/cache   available
Mem:            2.0Gi       410Mi       1.0Gi       12Mi       590Mi       1.4Gi
Swap:              0B          0B          0B
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        40G  6.5G   34G  17% /

If any swap device is already listed, you may not need another one. If /swapfile already exists, inspect it before continuing; do not overwrite a file that belongs to an application.

Step 2: Create and protect the swap file

Write a fully allocated file, then restrict it before Linux accepts it as swap; this avoids sparse-file problems and prevents ordinary users from reading data written to disk.

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

The copy operation should finish with a summary showing 2,147,483,648 bytes copied. Set up the file as swap space:

sudo mkswap /swapfile

The command creates a new swap signature and reports its size and UUID:

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

If dd stops with a space error, remove the incomplete file only after confirming that it is not active, then choose a smaller size based on the available space shown in Step 1.

Step 3: Activate swap for the current session

mkswap prepares the file but does not use it; swapon hands it to the kernel without requiring a reboot.

sudo swapon /swapfile
sudo swapon --show

The active table should include the new file:

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

The priority may differ if the server already has another swap area. That is normal as long as /swapfile appears as active.

Step 4: Make the configuration survive reboot

Add one /etc/fstab entry so AlmaLinux activates the file during every boot, then ask systemd to reload the generated mount configuration.

sudo nano /etc/fstab

Add this line on its own line, save the file, and exit the editor:

/swapfile none swap defaults 0 0

Reload systemd and validate the entry without restarting the server:

sudo systemctl daemon-reload
sudo swapon --show

Do not add the line twice. A duplicate entry can cause confusing boot messages even though the first entry is valid.

Step 5: Tune swappiness only if needed

The vm.swappiness value influences how readily the kernel moves inactive memory to swap; the AlmaLinux default is a sensible starting point for most workloads.

See the current value:

sysctl vm.swappiness

If this VPS should prefer RAM and use the file mainly during sustained pressure, try 10 for the current boot:

sudo sysctl vm.swappiness=10

If monitoring shows that this is appropriate, persist it in a dedicated file instead of editing the main sysctl configuration:

sudo nano /etc/sysctl.d/99-swap.conf

Add:

vm.swappiness = 10

Apply the file and check the effective value:

sudo sysctl --system
sysctl vm.swappiness

Expected output ends with:

vm.swappiness = 10

Do not set swappiness to zero just to avoid disk I/O; doing so can leave less room for the kernel to handle a sudden memory shortage.

Verify the persistent swap setup

Confirm the kernel sees the file, the system reports its capacity, and the boot entry exists.

sudo swapon --show
cat /proc/swaps
free -h
sudo grep -E '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab

Successful output contains a 2 GB /swapfile, a matching /proc/swaps row, and this configuration line:

/swapfile file    2097148      0        -2
Swap:          2.0Gi          0B     2.0Gi
/swapfile none swap defaults 0 0

For the AlmaLinux 8-compatible storage procedure, see Red Hat’s RHEL 8 swap documentation.

Troubleshooting

swapon: /swapfile: swapon failed: Invalid argument

The file may be sparse, partially allocated, or on a filesystem that rejects its current layout. Check that it is not active with swapon --show, remove the inactive file, and recreate it with the dd command in Step 2 rather than fallocate or truncate.

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

The root filesystem cannot hold a 2 GB file. Check df -h /, remove only data you recognize as unnecessary, or use a smaller file such as 1G with bs=1M count=1024; run mkswap again after recreating it.

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

The path is already active or another process has the file open. Run swapon --show and ls -l /swapfile; if it is already listed, keep it and remove any duplicate /etc/fstab line instead of creating a second file.

swapon: ... Operation not permitted

The VPS virtualization layer may forbid guest-managed swap. This is a host policy, not a missing AlmaLinux package; ask the provider to enable host swap or move the workload to a plan that permits swap files.

FAQ

How much swap should a small AlmaLinux 8 VPS have?

There is no universal ratio. A 2 GB file is a practical starting point for a small server, but choose based on available disk, workload peaks, and whether an application has its own memory requirements. Keep enough free space for updates and logs.

Will swap increase the server’s available RAM?

No. It adds slower disk-backed virtual memory and can prevent a short memory spike from immediately killing a process. Continuous swap activity usually means the workload needs more RAM or tighter memory limits.

Can I use a swap partition instead of a file?

Yes. A dedicated partition can be formatted with mkswap and added to /etc/fstab by UUID, but a file is easier to resize on a VPS and does not require repartitioning the disk.

How can I remove this swap file later?

First confirm the path with swapon --show, then run sudo swapoff /swapfile, remove only its line from /etc/fstab, reload systemd, and delete the file with sudo rm /swapfile. Do not remove it while it is active.

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

How to Add Swap Space on AlmaLinux 10 (Step-by-Step)

Next Article

What Is a 10Gbps Dedicated Server?

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 ✨