How To Add Swap Space on Debian 10 Linux

Содержание

How to Add Swap Space on Debian 10 Linux

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:

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.

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 :

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:

You should set permission to read and write only for root user. Execute the below command to set proper permission:

Run the below command to set up a Linux swap area on the file:

After that activate the swap file:

Edit the /etc/fstab file to make changes:

Add the below lines to that file then save and close:

You can verify the swap is activated or not using either the swapon or free command as given below:

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:

If you would like to change swappiness to a different value, you can do it using sysctl command.

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:

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:

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:

Conclusion#

You have learned how to create a swap file, activate and configure swap space and remove swap file on your Debian 10 system.


Источник: tecnstuff.net