How to Add Swap Space on Debian 10 Linux

Published on

4 min read

Create Swap File on Debian Linux

Swap is a space on a disk that is used when the amount of physical 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. Usually, when running a Debian virtual machine a swap partition is not present so the only option is to create a swap file.

This tutorial will guide you through the steps of adding a swap file on Debian 10 Buster.

Before You Begin

Although possible, it is not common to have multiple swap spaces on a single machine. To check whether your Debian installation already has swap enabled, run the following command:

sudo swapon --show

If the output is empty, it means that the system doesn’t have swap space.

Otherwise, if you get something like below, you already have swap enabled on your Debian system.

NAME      TYPE      SIZE USED PRIO
/dev/sda2 partition   4G   0B   -1

To activate swap, the user running the commands must have sudo privileges .

Creating a Swap File

In this example, we will create and activate 1G of swap. To create a bigger swap, replace 1G with the size of the desired swap space.

The steps below show how to add swap space on Debian 10.

  1. First create a file which will be used for swap:

    sudo fallocate -l 1G /swapfile

    If fallocate is not installed or you get an error message saying fallocate failed: Operation not supported you can use the following command to create the swap file:

    sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
  2. Only the root user should be able to read and write to the swap file. Issue the command below to set the correct permissions :

    sudo chmod 600 /swapfile
  3. Use the mkswap tool to set up a Linux swap area on the file:

    sudo mkswap /swapfile
  4. Activate the swap file:

    sudo swapon /swapfile

    To make the change permanent open the /etc/fstab file:

    sudo nano /etc/fstab

    and paste the following line:

    /etc/fstab
    /swapfile swap swap defaults 0 0
  5. Verify whether the swap is active using either the swapon or free command as 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

Adjusting 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 using the cat command:

cat /proc/sys/vm/swappiness
60

While the swappiness value of 60 is OK for most Linux systems, for production servers you should 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 /etc/sysctl.conf file:

/etc/sysctl.conf
vm.swappiness=10

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.

Removing a Swap File

To deactivate and remove the swap file, perform the steps below:

  1. Deactivate the swap space by running:

    sudo swapoff -v /swapfile
  2. Open the /etc/fstab file with your text editor and remove the swap file entry /swapfile swap swap defaults 0 0.

  3. Finally, delete the actual swapfile file with the rm command:

    sudo rm /swapfile

Conclusion

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

If you hit a problem or have feedback, leave a comment below.