Add swap space on Rocky Linux 9 by creating a root-only 2 GiB swap file and enabling it for the current session and future boots. The setup takes about five to ten minutes and gives a small VPS a disk-backed buffer during short memory spikes.
Prerequisites
- Rocky Linux 9 on a VPS or dedicated server, with at least 2.5 GiB free on the filesystem that holds
/swapfile; the extra room leaves space for system files and logs. - SSH access as
root, or a regular user with workingsudoprivileges. - At least 2 GiB of RAM is useful for a general-purpose server, but swap size should follow the workload rather than replace physical memory.
- The
/swapfilepath must be unused. Do not overwrite an existing application file or active swap area. - A VPSLake Linux VPS if you need a Rocky Linux server for this procedure.
Swap is slower than RAM. It can absorb a temporary shortage, but frequent swapping means the server needs more memory or the application needs tighter limits.
Step 1: Inspect Rocky Linux 9 and the storage state
Check the release, root filesystem, available disk, and current swap before writing a file so you do not duplicate swap or fill the system disk.
cat /etc/rocky-release
findmnt -no FSTYPE /
df -h /
free -h
swapon --show
test -e /swapfile && ls -lh /swapfile || echo "/swapfile is available"
A new Rocky Linux 9 VPS commonly reports XFS or ext4, no active swap rows, and an available target path:
Rocky Linux release 9.x (Blue Onyx)
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 420Mi 1.0Gi 12Mi 580Mi 1.4Gi
Swap: 0B 0B 0B
/swapfile is available
The exact Rocky minor release, device name, and memory values will differ. If swapon --show already lists a swap partition or file, keep it unless you have a specific reason to add more. If /swapfile exists, choose another unused name such as /swapfile-2g instead of replacing it.
Step 2: Create and protect the swap file
Allocate the file on the local filesystem, lock it to root, and then write the swap signature so other users cannot read memory pages that the kernel stores there.
On the usual XFS or ext4 root filesystem, run:
sudo fallocate -l 2G /swapfile
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
mkswap should print 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
Rocky Linux 9 follows the RHEL 9 storage procedure, which recommends fallocate on modern filesystems such as XFS and ext4. The RHEL 9 swap documentation also documents the mkswap, fstab, and swapon sequence.
If fallocate is missing or the later activation check rejects the file, first confirm that /swapfile is not active. Then recreate the inactive file with a fully written fallback:
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
The dd command writes 2,048 MiB and can take longer because it touches every block. Do not run both creation sequences against the same active file.
Step 3: Enable swap for this boot
Creating the swap signature prepares the file but does not make the running kernel use it; swapon activates it immediately without a reboot.
sudo swapon /swapfile
sudo swapon --show
The active table should contain a row similar to this:
NAME TYPE SIZE USED PRIO
/swapfile file 2G 0B -2
The priority can vary when another swap device is already enabled. The important results are that /swapfile appears and its size is close to 2 GiB.
Step 4: Make the file persistent across reboots
Add one guarded /etc/fstab entry so Rocky Linux activates the same file during startup, then reload systemd’s generated configuration.
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 final command should show one active line:
/swapfile none swap defaults 0 0
The guard prevents a duplicate if the entry is already present. swapon from Step 3 handles this boot; the fstab record is what restores swap after a restart. You do not need to reboot just to activate it now.
Step 5: Review swappiness before changing it
vm.swappiness influences how readily the kernel moves inactive pages to disk, so inspect the current value and tune it only for a workload you are monitoring.
sysctl vm.swappiness
To test a value that prefers keeping active data in RAM, apply 10 for the current boot:
sudo sysctl vm.swappiness=10
sysctl vm.swappiness
If monitoring shows that value is appropriate, save it in a dedicated file rather than editing a vendor configuration:
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 merely to eliminate disk activity. A little flexibility can help during a sudden memory shortage, while constant swap use is evidence that the workload needs attention.
Verify the persistent Rocky Linux 9 swap setup
Check activation, capacity, permissions, and the boot entry together. This confirms that the kernel can use the file now and that Rocky Linux has the information needed after a restart.
sudo swapon --show
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, root-only permissions, and one matching fstab line:
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
swapon: /swapfile: swapon failed: Invalid argument
The file may be sparse, partly allocated, or on a filesystem that does not support this swap-file layout. Run findmnt -no FSTYPE / and confirm the file is not active with sudo swapon --show; on XFS or ext4, recreate the inactive file with the dd fallback in Step 2. Do not remove a path that appears in the active swap table.
fallocate: fallocate failed: No space left on device
The filesystem cannot provide 2 GiB. Check df -h /, preserve room for Rocky 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 fstab path remains 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. Inspect sudo swapon --show and ls -l /swapfile; if it is already listed, keep that file and remove any duplicate /etc/fstab line rather than recreating it.
swapon: /swapfile: swapon failed: Operation not permitted
Some container-based VPS platforms do not allow a guest to manage its own swap. This is a virtualization policy, not a missing Rocky Linux package, so ask the host for managed swap or use a virtualization plan that permits guest-created swap files.
FAQ
Is a 2 GiB swap file enough for Rocky Linux 9?
It is a practical starting point for a small VPS, not a universal formula. Size it around short memory spikes, leave room for the operating system and application data, and watch swap activity before making it larger.
Does Rocky Linux use swap instead of RAM?
The kernel uses RAM first and may move less-active pages to disk under memory pressure. Swap is far slower than RAM, so frequent reads and writes indicate that the server needs more memory or the workload needs adjustment.
Can I use a swap file on Rocky Linux 9’s default XFS filesystem?
Yes. XFS is a common Rocky Linux layout, and fallocate is appropriate for a normal local XFS swap file. The file still needs mode 0600, a swap signature, an active swapon entry, and a persistent /etc/fstab record.
How do I resize the swap file later?
Create or activate a second swap file first if the server is under memory pressure. Otherwise, run swapoff on the old file, remove only its fstab line, create the replacement at the new size, and verify the new active path before deleting the old file.