Add swap space on AlmaLinux 10 by creating a protected 2 GB swap file and enabling it at boot. The process takes about five to ten minutes and gives a memory-constrained VPS a disk-backed buffer when RAM is under pressure.
Prerequisites
- An AlmaLinux 10 VPS with at least 2 GB of free disk space for the example file; choose a smaller file if your disk is nearly full.
- An SSH connection as
root, or a normal user that can run commands withsudo. - The
/swapfilepath must not already be in use as a regular file or active swap device. - A fresh Linux VPS from VPSLake if you need an AlmaLinux server to work on.
Swap is not a replacement for RAM. It is slower than memory, but it can provide breathing room during short-lived memory spikes and may help prevent an immediate out-of-memory failure.
Step 1: Check RAM, disk space, and existing swap
Check the current state first so you do not create a duplicate swap area or fill the root filesystem.
cat /etc/almalinux-release
free -h
swapon --show
df -h /
Typical output on a server without active swap looks like this:
AlmaLinux release 10.x (release name may vary)
total used free shared buff/cache available
Mem: 2.0Gi 420Mi 1.1Gi 12Mi 480Mi 1.4Gi
Swap: 0B 0B 0B
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 6.2G 34G 16% /
An empty result from swapon --show means no swap is active. If it already lists /swapfile, stop here and use the existing file; recreating it could interrupt processes that are using it. Any existing partition or logical volume listed as swap can remain enabled while you add a separate file.
Step 2: Create and secure a 2 GB swap file
Allocate the file on the local filesystem, then restrict access before marking it as swap so ordinary users cannot read memory pages written to disk.
sudo fallocate -l 2G /swapfile
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
The mkswap command prints a new UUID. The UUID is informational for this path-based setup; the /etc/fstab entry in the next step will refer to /swapfile.
Expected output includes:
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
If fallocate is unavailable or the filesystem rejects the allocated file, create a fully written file instead. This performs more disk I/O but avoids sparse-file problems on filesystems that require swap files to have allocated blocks.
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
Only run the fallback after the first attempt has failed and /swapfile is not active. The dd command replaces the file at that path.
Step 3: Enable swap now and after every reboot
Add the swap file to /etc/fstab for persistence, reload systemd’s view of that file, and activate the file in the current session.
sudo nano /etc/fstab
Add this line on its own line, then save and exit:
/swapfile none swap defaults 0 0
Now register the updated configuration and turn the file on:
sudo systemctl daemon-reload
sudo swapon /swapfile
sudo swapon --show
You should see the new file in the active swap table:
NAME TYPE SIZE USED PRIO
/swapfile file 2G 0B -2
The defaults entry makes the configuration available at boot, while swapon activates it without requiring a restart. Reloading systemd avoids stale mount-unit information after changing /etc/fstab.
Step 4: Optionally adjust swappiness
vm.swappiness controls how readily the kernel reclaims memory into swap. The default is usually suitable, so change it only when your workload and disk performance justify the choice; setting it to zero can increase the risk of the OOM killer terminating a process.
Inspect the current value before changing it:
sysctl vm.swappiness
For a server where you want the kernel to prefer RAM and use swap mainly during pressure, test a value of 10 for the current boot:
sudo sysctl vm.swappiness=10
If the result is appropriate for your workload, persist it in a dedicated configuration file:
sudo nano /etc/sysctl.d/99-swap.conf
Add:
vm.swappiness = 10
Apply and check the saved setting:
sudo sysctl -p /etc/sysctl.d/99-swap.conf
sysctl vm.swappiness
Expected output is:
vm.swappiness = 10
For the parameter behavior and persistence options, see the RHEL 10 kernel parameter documentation.
Verify the swap file and boot configuration
Confirm both halves of the setup: the kernel must report /swapfile as active, and /etc/fstab must contain the persistent entry.
sudo swapon --show
cat /proc/swaps
free -h
sudo grep -E '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab
Successful output includes a 2 GB /swapfile, zero or low current usage, and the matching fstab line:
NAME TYPE SIZE USED PRIO
/swapfile file 2G 0B -2
Filename Type Size Used Priority
/swapfile file 2097148 0 -2
Swap: 2.0Gi 0B 2.0Gi
/swapfile none swap defaults 0 0
For the AlmaLinux 10-compatible procedure and verification commands, refer to Red Hat’s RHEL 10 swap documentation.
Troubleshooting
swapon: /swapfile: swapon failed: Invalid argument
The file may be sparse, have unallocated blocks, or be stored on a filesystem that does not support swap files in its current configuration. Confirm that it is not active, remove the failed file, then recreate it with the dd fallback from Step 2; do not delete it if swapon --show lists it.
fallocate: fallocate failed: No space left on device
The root filesystem does not have enough free space for a 2 GB file. Check df -h /, free disk space safely, or choose a smaller swap size such as 1 GB and repeat the creation step with matching dd block counts if needed.
swapon: ... Operation not permitted
Some container-based VPS environments do not allow the guest to manage swap. You cannot fix that with a Linux command inside the VPS; ask the hosting provider to enable host-level swap or move the workload to a virtualization plan that permits guest swap files.
swapon: /swapfile: swapon failed: Device or resource busy
The path is already active or another process is using it. Run swapon --show and ls -l /swapfile; if it is already listed, remove any duplicate fstab entry rather than creating another swap area.
FAQ
How large should swap be on AlmaLinux 10?
There is no single size that fits every server. A 2 GB file is a practical starting point for a small VPS, but memory-heavy services may need more disk-backed headroom; measure RAM pressure and keep enough free disk for the operating system and applications.
Does swap make a VPS faster?
No. Disk-based swap is much slower than RAM, so heavy swap activity usually indicates that the VPS needs more memory or a workload adjustment. Swap is best treated as an emergency buffer, not a performance upgrade.
Do I need to reboot after adding the swap file?
No. swapon /swapfile enables it immediately, and the fstab entry makes it available on future boots. The systemctl daemon-reload command is enough to make systemd recognize the updated configuration.
How do I remove the swap file later?
Run sudo swapoff /swapfile, remove only its line from /etc/fstab, run sudo systemctl daemon-reload, and then delete the file with sudo rm /swapfile. Check swapon --show first and do not remove a file that is still active.