Creating and Enabling Swap Space on Linux Cloud Servers

2019-12-10 By Aaron 5858 Views linux swap
59 reviews

Swap is an area on a hard disk that is used when the amount of physical RAM memory is full. The swap space on the hard disk will be used mainly when there is no longer sufficient space in RAM to hold the running application data on the server. Basically, it is designated as a place where the operating system can temporarily store data that can no longer hold in RAM.


swapdiagram


This guide outlines the steps to create and enable swap space in all Linux distributions.


  1. Check Swap Information.

    Before enabling a swap file, it can be checked if a server already has swap enabled by executing the below command.

    # swapon --show
    

    If no output is displayed, it means that the server is not enabled with swap.

    Available swap space can also be verified by using the free command as below:

    # free -m
    

    swap1


  1. Check Available Space on the Partition.

    A swap file can be created by making use of space already available from an existing partition. The current disk usage in the server can be found out by executing the below command.

    # df -h
    

    A general output for the above command will be as shown below:

    swap2

    In the above case, space from the partition /dev/mapper/cl-root (may differ in each server) can be used to create the swap space. Generally, a size equal to or double the size of RAM in the server can be made use to create the swap file.


  1. Create Swap File.

    The fallocate command can be used to create the swap file in the server. Execute the below command to create a swap file of 2GB in size.

    # fallocate --length 2G /lsswap
    

    The swap file with the name lsswap will be created in the root directory of the server.

    If fallocate is not installed or if you get an error message as fallocate failed: Operation not supported, then the following command can be used to create the swap file:

    # dd if=/dev/zero of=/lsswap bs=1024 count=2146304
    

    Using the ls command, the created swap file can be verified.

    # ls -lh /lsswap
    

    swap3


  1. Set the correct permissions.

    Set appropriate permissions for the swap file, making it readable only by the root user.

    # chmod 600 /lsswap
    


  1. Set up Linux swap area.

    The mkswap utility can be used to set up the created swap file as the swap space in the server. For this, execute the below command.

    # mkswap /lsswap
    

    The output for the above command will be displayed as below.

    swap4


  1. Enable the swap file.

    Enable the swap file and add it to the system as a swap file by running the below command.

    # swapon /lsswap
    

    To make the changes permanent, open the /etc/fstab file using any editor and add the following line in it.

    # /lsswap swap swap defaults 0 0
    


  1. Verify the swap file.

    The newly set up swap space can be verified by making use of either of the below two commands.

    # swapon -s
    (or)
    # free -h
    

    swap5


  1. Adjust swappiness.

    Swappiness is the server kernel parameter that indicates how much and how often the RAM contents are copied to swap. It has the default value of 60 and can range between 0 to 100.

    The existing value of swappiness in the server can be found out by making use of the below command.

    # cat /proc/sys/vm/swappiness
    

    swap6

    The swappiness can be adjusted using the sysctl command.

    # sysctl vm.swappiness=10
    

    The swappiness value will be set to 10 after the command is executed.

    To set this value permanently in the server, add the below line at the bottom of the /etc/sysctl.conf file.

    # vm.swappiness=10
    

    swap7


  1. Remove a swap file.

    The swap file in the server can be removed by making use of the swapoff command. The below steps can be followed for this. The swap file name being used in the below commands is lsswap.

    # swapoff -a -v
    

    Remove the originally created swap file.

    # rm /lsswap
    

    Remove the entry for the swap space from the /etc/fstab file.

    # sed -i '/\/lsswap/d' /etc/fstab
    

    Check if the swap has been removed by making use of the free command.

    swap8

What do you think about this article?

Rate this article
LayerStack Promotion
Need assistance?

Try this guide to receive free bundled services at signup on a new free account.

Sign Up

Your Feedback Is Important

We hope you’ll give the new products and updates a try. If you have an idea for improving our products or want to vote on other user ideas so they get prioritized, please submit your feedback on our Community platform. And if you have any questions, please feel free to ask in the Community or contact our Technical Support team.