Grid Guides

Explore How System On Grid Can Work For You

Grid Guide Topics

How To Add Swap on CentOS 7

Table Of Contents


    ## Table of Contents

    – [Introduction](#introduction
    – [Prerequisites](#prerequisites
    – [Check the System for Swap Information](#check-the-system-for-swap-information
    – [Check the Hard Disk for Available Space](#check-the-hard-disk-for-available-space
    – [How Much Swap Space Do I Need](#how-much-swap-space-do-i-need
    – [Create a Swap File](#create-a-swap-file
    – [Enabling the Swap File](#enabling-the-swap-file
    – [Making the Swap File Permanent](#making-the-swap-file-permanent
    – [Change your Swap Settings](#change-your-swap-settings
    – [Conclusion](#conclusion

    ## Introduction
    In this guide, I will explain to you how to add swap space on CentOS 7

    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 possess regular user with sudo privileges configured on the server. To do so, follow our guide, [Initial Server Setup with CentOS 7](https://systemongrid.com/support/guides/initial-server-setup-with-centos-7.

    ## Check the System for Swap Information

    Make sure to check whether your system has swap space already or not. You can check it by using the following command.

    “`
    $ sudo swapon -s
    “`

    If you get the header of the table, as shown above, it means you do not have any swap space in your system.

    And, another way to check space is by using the free utility, which shows the memory usage of your system. We can know the swap usage and current memory by using the below command.

    “`
    $ sudo free -m
    “`

    ![centosswapusage](https://grid.media/assets/images/centos7-swap-usage-02132019.png

    Now, you can see the above output. Your swap space in your system is 0.

    ## Check the Hard Disk for Available Space

    Also, check your hard disk for the current disk usage. You can use the below command to know that.

    “`
    $ sudo df -h
    “`

    ![swapcentosavailablespace](https://grid.media/assets/images/swap-centos7-hard-disk-available-space-02132019.png

    Now, you can see the first line in the above table that the hard disk partition has 1GB of available memory. It means we have an adequate amount of space to work with, however, your actual usage might be different.

    ## How Much Swap Space Do I Need

    The appropriate size of your swap space depends on the requirement of your applications and your personal choice. But, it is recommended to have the size of your swap space equal to or double the space of RAM on your system.

    For example, your system has 8 GB of RAM. Now, it is recommended to have either 8GB or 16GB of swap space.

    ## Create a Swap File

    Now, as you know the amount of available space in your hard disk, you can create a swap file within your file system.

    You must create a file, swapfile, in your root directory. This file must be allocated the amount of space that you wish to. This can be done using the following method.

    The swap file can be created by using fallocate programme. This command generates a file with a specified size.

    For example, a server has 1G of RAM, we will create a 1G file in this guide.

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

    We can verify the amount of space reserved by the following command.

    “`
    $ sudo ls -lh /swapfile
    “`

    ![centosswapfile](https://grid.media/assets/images/centos7-swap-file-02132019.png

    Now the file is created with the required amount of space.

    ## Enabling the Swap File

    Before enabling the swap file, you need to change the settings so that no one can read the file except the root user. We can restrict other users from reading or editing the swap file by using the following command.

    “`
    $ sudo chmod 600 /swapfile
    “`

    To verify the permissions whether they are changed or not, use the following command.

    “`
    $ sudo ls -lh /swapfile
    “`

    ![verifyswapfilepermissions](https://grid.media/assets/images/verify-swap-file-permissions-02132019.png

    Now you can see the root user has read and write flags enabled. Now we can mark the file as swap space by entering the below command.

    “`
    $ sudo mkswap /swapfile
    “`

    ![fileasswapspace](https://grid.media/assets/images/marking-the-file-as-swap-space-02132019.png

    We can enable the swap file by allowing the system to start utilizing it, after marking the file.

    “`
    $ sudo swapon /swapfile
    “`

    Enter the following command to verify swap is available or not.

    “`
    $ sudo swapon -s
    “`

    You can verify the output of free utility by entering the following command.

    “`
    $ sudo free -m
    “`

    ![swapfreeutility](https://grid.media/assets/images/swap-centos7-free-utility-02132019.png

    Successfully set up swap and our operating system will begin to use it if required.

    ## Making the Swap File Permanent

    Now your swap file is enabled. When you reboot, the server will not automatically enable the file for use. You can modify the fstab file, which is a table that manages filesystems and partitions.

    You have to edit the file with sudo privileges.

    “`
    sudo vi /etc/fstab
    “`

    You have to add the below line that will tell OS to automatically use the swap file that you created.

    “`
    /swapfile swap swap sw 0 0
    “`

    Now you can save and close the file. Now the server will check this file when each time reboots, So the swap file will be ready to use.

    ## Change your Swap Settings

    The swappiness parameter, a value between 0 and 100 represents the percentage, configures how frequently your system swaps the data out of the RAM to the swap space.

    If the value is closer to 0, it means the kernel will not swap the data to the hard disk unless it is required.

    If the value is closer to 100, it means the kernel will put more data into swap to make more RAM space free.

    The ideal for a server is close to 0. So, set your swappiness value to 10 by using the below command.

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

    ![centosswappiness](https://grid.media/assets/images/centos7-swappiness-02132019.png

    Make this swap file permanent by entering the following command.

    “`
    $ sudo vi /etc/sysctl.conf
    “`

    Then, add the next line at the end of the file.

    “`
    $ sudo vm.swappiness=10
    “`

    ## Conclusion

    In this guide, we have described how to add swap on CentOS 7.