Swap is the space on the disk that is used when the physical RAM is full. When the Linux system runs out of RAM, the inactive pages are moved from the RAM to switching space.
Swap space can take the form of a private swap partition or swap file. In most cases, when running Linux on a virtual machine, the swap partition is not present, so our only option is to create a swap file.
We hope that you will open the Linux distribution console to follow the guide on this page so that you better understand how to create a Swap file in a Linux terminal.
We also recommend that you test any online tutorials or guides on a virtual machine (vmware or virtualbox) before deploying to a production server, so that you don’t mess with your running system when something goes wrong.
You can learn how to install VMware on Ubuntu, CentOS, and VirtualBox on Ubuntu, Fedora and CentOS.
This tutorial was tested on Linux systems with Ubuntu 18.04 and CentOS 7 but should work with other Linux distributions.
How to create the swap file
Follow these steps to add 1 GB of swap to the server. If you want to add 2 GB, replace it
- Create a file that we’ll use in Swap
sudo fallocate -l 1G /swapfile
fallocateNot installed or if you receive an error message stating
fallocate failed: Operation not supportedThen you can use the following command to create a swap file:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
- Set the permissions so that only the root can write and read the swap file. To set the correct permission type, use the command:
sudo chmod 600 /swapfile
- Set up the Linux swap region. Use the utilities
mkswapTo set the file as Linux swap region
sudo mkswap /swapfile
- Activate the swap file with the following command:
sudo swapon /swapfile
To make permanent changes, open the file
/etc/fstabAnd add the following line:
sudo nano /etc/fstab
/swapfile swap swap defaults 0 0
- Check the status of the swap. To verify that the swap is active, we can use
freeAs shown below:
sudo swapon --show
NAME TYPE SIZE USED PRIO /swapfile file 1024M 507.4M -1
sudo free -h
total used free shared buff/cache available Mem: 488M 158M 83M 2.3M 246M 217M Swap: 1.0G 506M 517M
Adjust the swap value
Swappiness is the Linux kernel property that determines how often the system uses swap space. A swap can have a value between 0 and 100.
A lower value will make the kernel try to avoid swapping whenever possible, while a higher value will make the kernel use the swap space more strongly.
The default swappiness value is 60. You can check the current swappiness value by typing the following command cat
While a swap value of 60 is sufficient for desktop computers, you may need to set a lower value for production servers.
For example, to set the swap value to 10, type:
sudo sysctl vm.swappiness=10
To make this parameter permanent, add the following line to the file
The optimal swap value depends on system workload and how memory is used. You will need to adjust these parameters little by little to find the optimum value that suits your needs.
How to delete swap files
If, for some reason, you want to disable and delete the swap file, follow these steps:
First, disable swap with the following command:
sudo swapoff -v /swapfile
Delete the swap file entry
/swapfile swap swap defaults 0 0 Of files
Finally, delete the swapfile with the command
sudo rm /swapfile
You learned how to create a swap file and enable and configure swap space on your Linux system.