Home Linux How To Create Swap Memory on RHEL/Centos & Debian/Ubuntu KVM Server

    How To Create Swap Memory on RHEL/Centos & Debian/Ubuntu KVM Server

    811
    0

    Swap memory is obviously very important in a VPS or Cloud Server environment. Especially if you have a virtual private server with low memory (RAM), 1024MB for example. However, not only for VPS and cloud servers, swap memory is equally important for dedicated servers. Well, the role of swap memory becomes very important and can help a server when it runs out of RAM.

    To create swap memory on RHEL/Centos and Debian/Ubuntu servers is very easy. As long as your VPS or cloud server is running on KVM and XEN virtualization. Keep in mind that you cannot create swap memory in OpenVZ or Virtuozzo VPS. Generally when you buy OpenVZ or Virtuozzo VPS (a commercial version of OpenVZ virtualization technology) it includes swap memory. However, there are also several OpenVZ / Virtuozzo VPS providers that don’t equip their VPS with swap memorys. As we know that OpenVZ / Virtuozzo VPS uses the same Linux kernel as their server node and it’s designed to be overselling on the market.

    Unlike KVM (Kernel-Based Virtual Machine) VPS or better known as semi-dedicated server, which has server resources (including RAM, CPU and Storage) completely isolated per user. So, I strongly recommend you to buy a VPS / Cloud Server with KVM virtualization technology.

    READ:  How To Reset MySQL Root Password on Debian/Ubuntu & CentOS

    Now, to create swap memory in KVM VPS please follow the tutorial below. This guide can be used for almost all KVM VPS from leading cloud server providers like Digital Ocean, Vultr, Linode, OVH, Ramnode, etc.

    1.) Login to your server with root privileges and run the following command to see if there is a swap memory that was previously installed on your server.

    $ swapon -s

    2.) Then check the remaining free storage on your server

    $ df

    3.) OK… let’s create a swap memory file.

    $ sudo dd if=/dev/zero of=/swapfile bs=1024 count=512k
    $ sudo dd if=/dev/zero of=/swapfile bs=1024 count=1024k
    $ sudo dd if=/dev/zero of=/swapfile bs=1024 count=4096k
    $ sudo dd if=/dev/zero of=/swapfile bs=1024 count=8192k

    PLEASE NOTE:

    * 512k stand for 512MB
    * 1024k stand for 1GB
    * 4096k stand for 4GB
    * 8192k stand for 8GB

    For example, if you have a KVM VPS with 4GB of RAM (RAM), I recommend you create a 4GB or 8GB swap memory. This is to create a stable and balanced system environment on your Linux server.

    4.) Let’s enable swap file on your system.

    $ sudo mkswap /swapfile
    
    $ sudo swapon /swapfile
    
    $ swapon -s

    4.) Open your nano or vim editor and add a swap rule to the /etc/fstab file, like this:

    $ nano /etc/fstab
    $ /swapfile swap swap defaults 0 0

    5.) After that, we need to set the correct permissions to the swap memory file that we just created.

    $ chown root:root /swapfile
    $ chmod 0600 /swapfile

    6.) Next, let’s configure the swappiness rule for swap file:

    $ cat /proc/sys/vm/swappiness
    $ sysctl vm.swappiness=75
    $ cat /proc/sys/vm/swappiness

    7.) Finally, we also need to add a swap memory rule in sysctl.conf, so swap memory can work since VPS start (booting).

    $ nano /etc/sysctl.conf

    Add the following rules:

    $ vm.swappiness=75

    Then run the following command in your SSH terminal:

    $ sysctl -p

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here