How To Add Swap File on CentOS

CentOS,InfrastructureCentOS,Swap


Create Swap File

Here, as an example, create a 400MB swap file “/swpfile".

dd if=/dev/zero of=/swapfile bs=1M count=1048
mkswap /swapfile
chmod 600 /swapfile

After the swap file “/swpfile" is created, enable the file.

swapon /swapfile

You can check the status with the “free -m" command

free -m
             total       used       free     shared    buffers     cached
Mem:          1875       1757        117          0         26        206
-/+ buffers/cache:       1524        351
Swap:         1024          1       1023


Edit /etc/fstab

In order to set the swap file permanent, add “/swpfile" to the file “/etc/fstab".

Example:

# /etc/fstab
# Add the following line

swap                    /swapfile                swap    defaults        0 0