Add swap space on Rocky Linux 10 by creating a protected 2 GiB swap file and enabling it now and at every future boot. The setup usually takes five to ten minutes and gives a small VPS temporary disk-backed headroom when RAM is under pressure.
Prerequisites
- Rocky Linux 10, with at least 2.5 GiB free on the filesystem mounted at
/for this example. - SSH access as
root, or an administrative user that can run commands withsudo. - A shell prompt and permission to create
/swapfile; do not use that path if it already belongs to a file or active swap area. - A VPSLake Linux VPS if you need a Rocky Linux server for this procedure.
Swap is slower than RAM, so treat it as a buffer for short memory spikes rather than a substitute for choosing enough memory. The commands below use 2 GiB; use a smaller file if your disk has less free space.
Step 1: Inspect Rocky Linux 10 and the current swap state
Check the release, root filesystem, free capacity, and existing swap before writing anything so you do not overwrite a useful configuration or fill the system disk.
grep -E '^(PRETTY_NAME|VERSION_ID)=' /etc/os-release
findmnt -no FSTYPE /
df -h /
free -h
swapon --show
if [ -e /swapfile ]; then ls -lh /swapfile; else echo "/swapfile is available"; fi
On an unused Rocky Linux 10 VPS, the release starts with version 10, the root filesystem is commonly xfs or ext4, Swap is 0B, and the last line confirms that the target path is free:
PRETTY_NAME="Rocky Linux 10.x (Red Quartz)"
VERSION_ID="10.x"
xfs
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 6.8G 33G 18% /
total used free shared buff/cache available
Mem: 2.0Gi 520Mi 760Mi 18Mi 720Mi 1.3Gi
Swap: 0B 0B 0B
/swapfile is available
The minor release, device name, memory totals, and filesystem type can differ. Rocky Linux 10 uses DNF 5, but no package installation is required for this procedure. If swapon --show lists swap already, keep it unless you have a specific reason to add another area. If /swapfile exists, stop here and choose another unused filename.
Step 2: Create and protect the swap file
Allocate the file, lock it to root, and add the swap signature; the permission step prevents local users from reading memory pages that the kernel writes into the file.
On the normal XFS or ext4 root filesystem, create a 2 GiB file with fallocate, which the RHEL 10 storage documentation recommends for modern filesystems:
sudo fallocate -l 2G /swapfile
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
mkswap should report a swap area close to 2 GiB and print a generated UUID. The UUID is informational here because the persistent configuration will use the stable /swapfile path.
Setting up swapspace version 1, size = 2 GiB
no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
For the file format and Rocky Linux 10’s Red Hat-compatible storage workflow, see the official RHEL 10 swap documentation. Do not repeat this step if /swapfile is already active.
Step 3: Activate swap for the running system
Formatting the file only prepares it; swapon makes the kernel use it immediately without requiring a reboot.
sudo swapon /swapfile
sudo swapon --show
The output should include /swapfile with a capacity close to 2 GiB:
NAME TYPE SIZE USED PRIO
/swapfile file 2G 0B -2
The priority can be different when another swap device is already enabled. The important checks are that the file is listed and its size is correct.
Step 4: Persist swap across reboots
Add one guarded /etc/fstab entry so Rocky Linux enables the same file during boot, while avoiding duplicate lines if the command is run again.
if ! sudo grep -qE '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab; then
echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab
fi
sudo systemctl daemon-reload
sudo grep -nE '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab
The final command should show one active configuration line:
/swapfile none swap defaults 0 0
systemctl daemon-reload refreshes systemd’s generated view of the changed file. Swap is already active from Step 3; the fstab record is what restores it after the next restart.
Step 5: Review swappiness before changing it
The vm.swappiness value influences how readily Linux moves inactive memory pages to disk, so inspect it before choosing a workload-specific setting.
sysctl vm.swappiness
If the server should prefer RAM and use swap mostly during pressure, you can test 10 for the current boot:
sudo sysctl vm.swappiness=10
If monitoring shows that value suits the workload, save it in a separate file so package updates do not overwrite the setting:
printf '%s\n' 'vm.swappiness = 10' | sudo tee /etc/sysctl.d/60-swap.conf
sudo sysctl --load=/etc/sysctl.d/60-swap.conf
sysctl vm.swappiness
Expected output is:
vm.swappiness = 10
Do not set swappiness to zero simply to eliminate disk activity. Continuous swapping usually means the VPS needs more RAM or the application needs tighter memory limits.
Verify the persistent Rocky Linux 10 swap setup
Run these checks together to prove that the kernel sees the file, the file is protected, and the boot configuration contains exactly 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 contains a 2 GiB active file, a nonzero swap total, root-only permissions, and one fstab entry:
NAME TYPE SIZE USED PRIO
/swapfile file 2G 0B -2
Swap: 2.0Gi 0B 2.0Gi
-rw------- root:root /swapfile
/swapfile none swap defaults 0 0
Troubleshooting
fallocate: fallocate failed: No space left on device
The root filesystem cannot provide the requested 2 GiB. Confirm the available capacity with df -h /, clean up files you recognize, or recreate the inactive file at a smaller size such as 1 GiB. Never remove the file until sudo swapon --show confirms that /swapfile is not active.
sudo swapon --show
sudo rm /swapfile
sudo fallocate -l 1G /swapfile
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
The /etc/fstab path does not include the size, so the existing entry can remain unchanged.
swapon: /swapfile: swapon failed: Invalid argument
The file may be sparse, incomplete, or on a filesystem with swap-file restrictions. Check findmnt -no FSTYPE / and confirm that mkswap /swapfile completed; on XFS or ext4, recreate the inactive file with fallocate, then run mkswap and swapon again. If the kernel log identifies a filesystem or virtualization restriction, use a swap partition or ask the host to provide swap instead.
swapon: /swapfile: swapon failed: Device or resource busy
The path is already active or another process has it open. Run sudo swapon --show and ls -l /swapfile; if it is already listed, keep that file and remove only duplicate /swapfile lines from /etc/fstab.
swapon: /swapfile: swapon failed: Operation not permitted
Some container-based VPS plans do not allow a guest to manage its own swap. This is a host policy, not a missing Rocky Linux package, so request host-managed swap or move the workload to a virtualization plan that permits guest swap files.
FAQ
How much swap should Rocky Linux 10 use?
There is no fixed RAM-to-swap formula. Two GiB is a practical starting point for a small VPS when disk space is available, but size the file around short memory spikes and keep enough storage for updates, logs, and application data.
Does swap add physical RAM?
No. Swap uses disk storage as slower virtual memory. It can delay an out-of-memory failure during a brief spike, but it cannot provide the performance of additional RAM.
Do I need to reboot after enabling swap?
No. swapon /swapfile activates the file immediately, and the /etc/fstab entry handles later boots. A reboot is only needed if you want to test the complete boot path during a maintenance window.
Can I remove the swap file later?
Confirm its exact path with sudo swapon --show, run sudo swapoff /swapfile, and remove only its line from /etc/fstab. After checking that it is inactive, delete the file with sudo rm /swapfile; never remove active swap first.