
Swap is a reserved space on a hard drive where a operating system can temporarily store data and it will not hold in RAM. When there is no longer sufficient space in RAM to hold in-use application data then Swap space will be used. This tutorial will help you to adding a swap space on Debian 10 Buster.
The information written to disk will be significantly slower than information kept in RAM. Overall, having a swap space can be good when your system’s RAM is depleted and throw out-of-memory exceptions on systems.
Before You Start
You should check that your Debian installation enabled swap or not. You can check it by running below command:
sudo swapon --show
If the output of above command is empty, it means your system don’t have swap space. If you will get something output like below then you already have swap enabled on your Debian system.
NAME TYPE SIZE USED PRIO /dev/sda5 partition 3.9G 0B -2
You can activate the swap by log in from a root or sudo enabled user account.
Creating a Swap File
Let’s create a swap of 1G
in this example. You can change the swap size as per your requirement and system. Perform the following steps to add swap space on Debian 10 system:
At first, create a file using below command which will used for swap :
sudo fallocate -l 1G /swapfile
By above command, if you get error message like fallocate failed: Operation not supported
, it means your system have not installed fallocate
. If you don’t want to install then you can use alternate command as following:
sudo dd if=/dev/zero of=/swapfile bs=2048 count=1048576
You should set permission to read and write only for root user. Execute the below command to set proper permission:
sudo chmod 600 /swapfile
Run the below command to set up a Linux swap area on the file:
sudo mkswap /swapfile
After that activate the swap file:
sudo swapon /swapfile
Edit the /etc/fstab
file to make changes:
sudo nano /etc/fstab
Add the below lines to that file then save and close:
/swapfile swap swap defaults 0 0
You can verify the swap is activated or not using either the swapon
or free
command as given below:
sudo swapon --show
NAME TYPE SIZE USED PRIO /swapfile file 1024M 0B -2
sudo free -h
total used free shared buff/cache available Mem: 1.5G 493M 533M 5.1M 473M 862M Swap: 1.0G 0B 1.0G
Adjust the Swappiness Property
The swappiness parameter configures how often your system swaps data out of RAM to the swap space. This is a value between 0 and 100 that represents a percentage. Values that are closer to 100 will try to put more data into swap in an effort to keep more RAM space free. While a low value will make the kernel to try to avoid swapping whenever possible. By default, for a desktop swappiness value is 60 and for a server, you might want to move it closer to 0. You can check the current swappiness value using the cat command:
cat /proc/sys/vm/swappiness
60
If you would like to change swappiness
to a different value, you can do it using sysctl
command.
sudo sysctl vm.swappiness=10
This setting will persist until the next reboot. We can set this value automatically at restart by adding the line to our /etc/sysctl.conf
file:
sudo sysctl vm.swappiness=10
vm.swappiness=10
Save and close the file when you are finished.
Removing a Swap File
Follow the below steps to deactivate and remove the swap file:
Execute the below command to deactivate swap space:
sudo swapoff -v /swapfile
After that, Open the /etc/fstab
file with your text editor and remove the swap file entry /swapfile
swap swap defaults 0 0.
At last, delete the actual swapfile
file:
sudo rm /swapfile
Conclusion
You have learned how to create a swap file, activate and configure swap space and remove swap file on your Debian 10 system.
Leave a Reply