Grid Guides

Explore How System On Grid Can Work For You

Grid Guide Topics

How To Add Swap Space on Debian 9

Table Of Contents


    ## Table of Contents

    – [Introduction](#introduction
    – [Prerequisites](#prerequisites
    – [Check the System for Swap Information](#check-the-system-for-swap-information
    – [Creating a Swap File](#creating-a-swap-file
    – [Adjusting the Swappiness Value](#adjusting-the-swappiness-value
    – [Removing a Swap File](#removing-a-swap-file
    – [Conclusion](#conclusion

    ## Introduction

    In this guide, I will explain to you how to add swap space on Debian 9.

    Swap is the area on a hard disk and a part of Virtual Memory, a combination of RAM and Swap space, of your computer. Swap space is used when your machine’s physical memory (RAM is full. When the RAM is full, the system needs more memory resources, all the inactive pages in the memory will be moved to Swap space.

    It is always recommended to have a dedicated swap partition for the swap space. But, the swap space can also be a swap file or a combination of swap partitions and swap files.

    Swap space gives your server the ability to store more amount of data in its working memory but with a few cautions. And, this space will only be used when the memory space in RAM is not sufficient for data.

    The information in the Swap space is comparatively slower than the information in the RAM. And, the operating system will use swap space to store the older data.

    Adding a swap space to your applications is the best and easiest way to increase the responsiveness of your server and protect against memory errors in the applications.

    ## Prerequisites

    You should have access to Debian 9 and created a non-root user account with sudo privileges by following our guide, [Initial server setup with Debian 9](https://systemongrid.com/support/guides/initial-server-setup-with-debian-9.

    ## Check the System for Swap Information

    If you want to know whether a swap is enabled or not by using below command.

    “`
    $ sudo swapon –show
    “`

    If you haven’t got any output then it means your system doesn’t have swap space.

    ## Creating a Swap File

    In this guide, we have added 1G of swap, If you want to create a bigger swap, then you can replace 1G with the size of the swap space you need.

    First of all, create a file which is used for the swap by using below command.

    “`
    $ sudo fallocate -l 1G /swapfile
    “`

    If the fallocate is not installed on your system or getting a message like fallocate failed: operation not supported then use the following command to create the swap file.

    “`
    $ sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
    “`

    You can able to read and write the swap file, if and only if you are a root user. Use the below command to set correct permissions.

    “`
    $ sudo chmod 600 /swapfile
    “`

    To set swap area on the swap file use the below command.

    “`
    $ sudo mkswap /swapfile
    “`

    Use the below command to activate the swap file.

    “`
    $ sudo swapon /swapfile
    “`

    Open the /etc/fstab file and make the change permanent.

    “`
    $ sudo vi /etc/fstab
    “`

    Enter the below line into the file.

    “`
    /swapfile swap swap defaults 0 0
    “`

    To verify whether the swap is active or not use the below command.

    “`
    $ sudo swapon –show
    “`

    ![debianswaponshow](https://grid.media/assets/images/debian-swapon-show-02132019.png

    “`
    $ sudo free -h
    “`

    ![debianfreespace](https://grid.media/assets/images/debian-swap-free-space-02132019.png

    ## Adjusting the Swappiness Value

    Swappiness means how often the system will use the swap space. It is a kernel property. Its value must be in between 0 to 100. A low swappiness value makes the kernel to avoid swapping whenever possible, while the higher value will make the kernel to use the swap space more aggressively. The default Swappiness value is 60. You can check your swappiness value by entering the below command.

    “`
    $ cat /proc/sys/vm/swappiness
    “`

    ![debiandefaultswapvalue](https://grid.media/assets/images/debian-default-swap-value-02132019.png

    If you want to set swappiness value to 10 then you can use the below command.

    “`
    $ sudo sysctl vm.swappiness=10
    “`

    If you want to make this parameter persistent across reboots then add the below line to the /etc/sysctl.conf file:

    “`
    $ vm.swappiness=10
    “`

    ## Removing a Swap File

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

    First of all, you have to deactivate the swap space by using the below command.

    “`
    $ sudo swapoff -v /swapfile
    “`

    Next, remove the swap file and /swapfile swap swap defaults 0 0 from the /etc/fstab file.

    Finally, delete the actual swap file by using below command.

    “`
    $ sudo rm /swapfile
    “`

    ## Conclusion

    In this guide, we have described how to add swap on Debian 9.