Page 1 of 1

[HowTo] Create and Add Swap On CentOS7 VestaCP

Posted: Tue Sep 11, 2018 6:14 pm
by xorro
In this Article we’re going to add Swap space to Linux OS (RHEL, CentOS, Ubuntu) to prevent memory full issues. Swap is important when your server frequently going out of memory and got hanged (OOM) this can cause MySQL database corruption and many issues with the server. Before we get started I like to inform that this tutorial is only valid for KVM type servers and Dedicated servers. For Openvz and other container based servers this tutorial will not work.

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory.
  • Protection against OOM (out of memory) errors, crashes, memory-related system unpredictability/instability.
  • Increases available memory to the system and allows more programs to be run concurrently & more safely
  • Prevent server non responsiveness/ hangs
Step 1 :

Follow this commands one by one :

Code: Select all

cd /var
touch swap.img
chmod 600 swap.img
STEP 2 :

We’ll create 1GB (1024MB) of Swap :

Code: Select all

dd if=/dev/zero of=/var/swap.img bs=1024k count=1000
result will look like this :

Code: Select all

[root@srv1 var]# dd if=/dev/zero of=/var/swap.img bs=1024k count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 3.30777 s, 317 MB/s
STEP 3 :

Creating swap patition :

Code: Select all

mkswap /var/swap.img
result will look like this :

Code: Select all

[root@srv1 var]# mkswap /var/swap.img
Setting up swapspace version 1, size = 1023996 KiB
no label, UUID=5813e8e7-1034-4700-84c2-c06905e26535
STEP 4 :

Enabling Swap :

Code: Select all

swapon /var/swap.img
Checking Swap :

Code: Select all

free -h

Code: Select all

[root@srv1 var]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.9G        260M        144M         27M        1.6G        1.5G
Swap:          999M          0B        999M
Step 5 :

making it Enable during server boot, run the below command which will add entry in /etc/fstab :

Code: Select all

echo "/var/swap.img    none    swap    sw    0    0" >> /etc/fstab
All done, you just enabled Swap partition on your server. let me know how it goes as i tried to make the tutorial as simple as could be.

Re: [HowTo] Create and Add Swap On CentOS7 VestaCP

Posted: Tue Oct 23, 2018 7:42 pm
by popers
Thanks, it works.

Re: [HowTo] Create and Add Swap On CentOS7 VestaCP

Posted: Thu May 23, 2019 10:23 am
by adamjedgar
Shouldnt this be owned by root?

What about the following step...

# chown root:root /swap