Introduction
Swap space is a part of a computer's virtual memory that is used for storing data that is not being used right now. When a computer's RAM is full, the operating system moves some data from the RAM to the swap space. This is called “swapping”. This helps the computer to work better by optimizing the use of RAM.
Furthermore, swap space can also be used to store data that is not being used currently, which can come in handy during times of unexpected system load.
A dedicated swap partition or a swap file can both be used as swap space. The only option when operating a Debian virtual machine is to build a swap file because a swap partition is generally not available.
In this tutorial, you will see how to add a swap space in Debian 10 Linux.
Before You Begin
Although it is conceivable, having several swap spaces on a single system is uncommon. Run the following command to see if swap is already enabled in your Debian installation:
sudo swapon --show
If the output is empty, the system does not have swap space.
If you see something similar to the output below, your Debian system already has swap enabled.
Output
NAME TYPE SIZE USED PRIO
/dev/sda2 partition 4G 0B -1
The user running the commands must have sudo privileges in order to activate swap.
Creating a Swap File
We will create and activate 1G
of swap in this example. Replace 1G
with the required swap space size to generate a larger swap.
Following are the instructions for adding swap space in Debian 10.
1) Firstly, create a file that will be used for swap:
sudo fallocate -l 1G /swapfile
In case fallocate
is not installed, or you receive a message stating that fallocate failed: Operation not supported
You can create a swap file with the command:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
2) The swap file should only be accessible to the root user for reading and writing. To set the proper permissions, enter the command below:
sudo chmod 600 /swapfile
3) To create a Linux swap area on the file, use the mkswap
tool:
sudo mkswap /swapfile
4) Activate the swap file:
sudo swapon /swapfile
Open the /etc/fstab
file in order to make the modification permanent:
sudo nano /etc/fstab
and paste the line that follows:
/swapfile swap swap defaults 0 0
5) Using either the swapon
or free
command, as seen below, check to see if the swap is active:
sudo swapon --show
Output
NAME TYPE SIZE USED PRIO
/swapfile file 1024M 507.4M -1
sudo free -h
Output
total used free shared buff/cache available
Mem: 488M 158M 83M 2.3M 246M 217M
Swap: 1.0G 506M 517M
Adjusting the Swappiness Value
The Linux kernel property known as “swappiness” determines how frequently the system will use the swap space. Swappiness can range from 0 to 100 in value. While a greater number will force the kernel to use the swap space, a low value will cause it to try to avoid swapping whenever possible.
The swappiness value by default is 60. The cat
command can be used to check the current swappiness value:
cat /proc/sys/vm/swappiness
Output
60
While the swappiness value of 60 is acceptable for the majority of Linux systems, you should use a lower value for production servers.
For instance, to adjust the swappiness value to 10, enter:
sudo sysctl vm.swappiness=10
Add the following line to the /etc/sysctl.conf
file to make this setting persistent across reboots:
vm.swappiness=10
Removing a Swap File
Follow these instructions to disable and delete the swap file:
1) Run the following command to deactivate the swap space:
sudo swapoff -v /swapfile
2) With your text editor, open the /etc/fstab
file, and delete the entry for the swap file, /swapfile swap swap defaults 0 0
.
3) Finally, use the rm
command to remove the actual swapfile:
sudo rm /swapfile
Conclusion
You now know how to create a swap file, activate swap space, and configure swap space on your Debian 10 machine.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.