How to Enable Swap on a Virtual Machine Templates: A Step-by-Step Guide" class="internal-post-link">Virtual Machine Backup in 2 Steps: A Quick Guide" class="internal-post-link">Virtual Machine?

Swap is additional disk space that the operating system uses as virtual memory when real RAM is insufficient. A virtual machine may sometimes require swap for more efficient task execution. If you need to enable swap on a virtual machine, follow these steps.

Step 1: Checking for Swap on the Machine

First, you need to check if swap is already present on your virtual machine. To do this, execute the command:

sudo swapon --show

If the output of this command shows swap information, such as size and type, then swap is already enabled on your machine.

Step 2: Creating a Swap File

If swap is not present on your machine, proceed to the second step – creating a swap file. To do this, execute the following commands:

VPS Hosting

Virtual servers with guaranteed resources

Choose VPS

sudo fallocate -l 1G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile

After executing these commands, the swap file will be created and enabled on your virtual machine.

Step 3: Automatically Enabling Swap on Boot

To automatically enable swap each time the machine starts, add an entry for the swap file to the /etc/fstab file. To do this, perform the following actions:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Now swap will be activated automatically each time your virtual machine starts.

By following these simple steps, you can successfully enable swap on your virtual machine and provide it with additional space to work in environments with limited RAM.