
Swap memory is a designated area on a storage device that the operating system uses as virtual memory when the physical RAM is fully utilized. Instead of terminating processes when memory runs out, the system moves less active data from RAM to the swap space, keeping the server stable under high memory pressure.
This article explains how to add swap memory on an Ubuntu 26.04 server using a swap file, configure the system to mount it automatically at boot, and tune the swappiness value to control how aggressively the system uses swap.
Before you begin, you need to:
Ubuntu 26.04 servers may include a default swap allocation. Check the current memory status before adding new swap space.
View the available RAM and swap memory on the server.
The output displays the total, used, and free memory for both RAM and swap. Note the current swap size before proceeding.
A swap file is a regular file on the server's filesystem that functions as swap space. The following steps create a 2 GB swap file, set the correct permissions, and format it for use as swap.
Create a 2 GB swap file in the root directory. Adjust the size as needed for your workload.
Restrict the file permissions to allow only the root user to read and write.
Format the file as swap space.
The output displays the swap size and a UUID for the new swap space.
The swapon command activates swap space for immediate use. The following steps enable the swap file and verify that the system recognizes it.
Enable the swap file.
Verify that the new swap space is active.
The output lists all active swap entries including the new /swapfile with its size and type.
Confirm the updated memory allocation.
The swap total should reflect the additional space from the new swap file.
The File System Table (/etc/fstab) defines which filesystems are mounted automatically when the server starts. Without an fstab entry, the swap file is lost after a reboot.
Back up the current fstab configuration.
Append the swap file entry to the fstab configuration.
Within the configuration:
/swapfile: Specifies the path to the swap file.swap swap: Sets the filesystem type and mount point for swap.defaults: Applies the default mount options.0 0: Disables backup and filesystem check for the swap entry.The swappiness value controls how aggressively the kernel moves data from RAM to swap. The value ranges from 0 to 100:
0: The system avoids swapping unless absolutely necessary.10-30: Low swapping, suitable for servers with sufficient RAM.50: Balanced approach between RAM and swap usage.60: The default value on most Ubuntu systems.100: The system swaps aggressively, moving data to swap even when RAM is available.View the current swappiness value.
Set a custom swappiness value. For example, 10 for a server workload that should prioritize RAM.
Apply the configuration change.
The swapoff command deactivates swap space. The following steps disable the swap file and remove it from the system.
Disable the swap file.
Verify that the swap file is no longer active.
Remove the swap file entry from /etc/fstab to prevent mounting at boot.
Delete the /swapfile swap swap defaults 0 0 line. Save and close the file.
Delete the swap file from the filesystem.
You have added swap memory on an Ubuntu 26.04 server using a swap file, configured automatic mounting through fstab, and tuned the swappiness value. Swap memory provides a safety net against out-of-memory conditions, though it operates slower than physical RAM. For more options, run man swapon to view the swap management manual.
0 Comments
Be the first to comment and share your perspective with the community.