Read in 3 minutes
Create a Linux Swap File
Swap is a space on a disk that is used when the amount of physical RAM memory is full. When a Linux system runs out of RAM, inactive pages are moved from the RAM to the swap space.
Swap space can take the form of either a dedicated swap partition or a swap file. In most cases when running Linux on a virtual machine a swap partition is not present so our only option is to create a swap file.
This tutorial was tested on a Linux systems with Ubuntu 18.04 and CentOS 7 but it should work with any other Linux distribution.
How to add Swap File
Follow this steps to add 1GB of swap to your server. If you want to add 2GB instead of 1 GB, replace
Create a file which will be used for swap.
sudo fallocate -l 1G /swapfile
faillocateis not installed or if you get an error message saying
fallocate failed: Operation not supportedthen you can you use the following command to create the swap file:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
Set the correct permissions.
Only the root user should be able to write and read the swap file. To set the right permissions type:
sudo chmod 600 /swapfile
Format the file to swap.
mkswaputility for format the file to swap:
sudo mkswap /swapfile
Enable the swap.
Activate the swap with the following command:
sudo swapon /swapfile
To make the change permanent open the
/etc/fstabfile and append the following line:/etc/fstab
/swapfile swap swap defaults 0 0
Verify the swap status.
To verify that the swap is active we can use either the
freecommand as shown bellow:
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
How to adjust the swappiness value
Swappiness is a Linux kernel property that defines how often the system will use the swap space. Swappiness can have a value between 0 and 100. A low value will make the kernel to try to avoid swapping whenever possible while a higher value will make the kernel to use the swap space more aggressively.
The default swappiness value is 60. You can check the current swappiness value by typing the following command:
While the swappiness value of 60 is OK for Desktops, for production servers you may need to set a lower value.
For example, to set the swappiness value to 10, type:
sudo sysctl vm.swappiness=10
To make this parameter persistent across reboots append the following line to the
The optimal swappiness value depends on your system workload and how the memory is being used. You should adjust this parameter in small increments to find an optimal value.
How to remove Swap File
If for any reason you want to deactivate and remove the swap file, follow these steps:
First deactivate the swap using the following command:
sudo swapoff -v /swapfile
Remove the swap file entry
/swapfile swap swap defaults 0 0from the
Finally delete the actual swapfile file:
sudo rm /swapfile
You have learned how to create a swap file and activate and configure swap space on your Linux system.
If you hit a problem or have a feedback, leave a comment below.