
Swap memory is a portion of storage used as virtual memory when your server's RAM is fully utilized. It helps prevent crashes by moving less-used processes from RAM to swap space, keeping the system stable under heavy load.
This article shows how to add swap memory on Ubuntu 20.04 using a swap file and Vultr Block Storage to expand your server’s available memory.
Before you begin:
Have an Ubuntu 20.04 server.
Have an existing block storage.
Access the server using SSH as a non-root user with sudo privileges.
Follow the steps below to check existing swap usage before adding more.
View the available server memory including RAM and Swap using the free utility.
Verify the Swap memory available on your server similar to the output below.
Based on the above output, the server includes a 2GB Swap memory volume.
Swap memory is storage space—either on your server's disk or a block storage volume—used as virtual memory. Utilize the section below to create swap space with a swap file or a dedicated block storage volume.
Create a new Swapfile with a specific size such as 2GB using the fallocate utility in your root directory /.
Modify the Swapfile permissions to allow only the root user to read and write changes on the file.
Format the file as swap using mkswap.
Verify the new swam memory size, label information and UUID similar to the following output.
List the storage devices available on your server using the lsblk utility. The new disk is attached as /dev/vdb and it has no partitions.
Output:
The Vultr Block Storage device is attached as vdb based on the above output with a 40GB size.
Initialize the volume using the GPT partition table.
Create a new partition using the entire block storage volume.
List the server storage devices again and verify that the new block storage partitiion is available.
Output:
Convert the new block storage partition to swap.
Output:
View your block devices information and verify that the new swap partition is available.
Output:
Note the swap partition UUID value to use when mounting the volume automatically at boot time.
Enable the Swapfile as swap memory using the swapon utility.
View the server swap memory and verify that the new Swapfile is active.
Output:
Run the following command to enable the Vultr Block Storage swap partition as swap memory. Replace /dev/vdb1 with your actual partition path.
View the server swap memory and verify that the swap partition is active.
Output:
The fstab file controls how file systems mount at boot. Follow the steps below to configure it for automatically mounting your swap memory at startup.
Backup the original fstab configuration on your server.
Open the fstab configuration file using a text editor such as nano.
Add the following Swapfile configuration at the end of the file.
Save and close the file.
The above configuration enables automatic mounting using the /swapfile.img file. Within the configuration:
swap: Enables the swap file system type.swap: Sets the swap mount point.0: Disables backups on the swap file system.0: Disables file system checks on the swap memory.View your block devices information and copy the UUID of new /dev/vdb1.
Output:
Add the block storage swap partition using its UUID value to enable automatic mounting on the volume.
Replace UUID=ca230c16-b5a4-44e6-b5a8-930bb1f33fcf with your server block device UUID.
You have enabled automatic mounting of swap memory on your server. The server automatically mounts and enables the swap memory at boot time.
The swappiness setting controls how aggressively your server uses swap memory compared to RAM. Lower values reduce swap usage, while higher values increase it. Below is a breakdown of how the values behave:
0: Avoids swapping unless absolutely necessary. 1–49: Uses swap sparingly; prefers RAM until nearly full. 50: Balanced use of RAM and swap. 51–99: More aggressive swapping from RAM to swap. 100: Prioritizes swap usage; RAM is freed more frequently.Follow the steps below to configure the swappiness value on your server.
Run the following command to modify the /etc/sysctl.conf with your swappiness value. Replace 50 with your desired swapping level.
Output:
Reload the sysctl configuration to apply the configuration changes.
View all available swap volumes on your server.
Output:
Run the following command to activate all inactive swap volumes in your /etc/fstab configuration.
View your server memory and verify the amount of swap memory actively in use.
Output:
Disable swap using the swapoff command followed by the swap file or partition path. For example, to disable the default swap file.
View the swap volumes on your server and verify that the default Swapfile is disabled.
Output:
You’ve successfully added swap memory on an Ubuntu 20.04 server, extending your system’s available memory. While swap is slower than RAM, it helps maintain performance by offloading less-used or resource-intensive processes from main memory.
0 Comments
Be the first to comment and share your perspective with the community.