How to create Swap Space on Centos

How to create Swap Space on Centos

 

What is SWAP space

Swap space/memory is the virtual space in the system,which is pre-configured as swap partition/file. Swap space in Linux is used when the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space.
To clear the swap memory on your system, you simply need to cycle off the swap. This moves all data from swap memory back to RAM.

Check the swap information

We can see if the system has any configured swap by using swapon
swapon -s
If nothing is returned by the command, then the summary was empty and no swap file exists. Another way of checking for swap space is with the free command. If there is know swap , it will show SWAP is 0

Create a Swap File

The fastest and easiest way to create a swap file is by using fallocate.

 sudo fallocate -l 2G /swapfile 
If the file system is XFS use dd instead of fallocate.
 sudo dd if=/dev/zero of=/swapfile count=2048 bs=1MiB 

Enable a Swap File

sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
To verify that the procedure was successful, we can check whether our system reports swap space now:swapon -s

Make the Swap File Permanent

Edit the fstab file sudo nano /etc/fstab add the following entry
/swapfile swap swap sw 0 0
When you are finished adding the line, you can save and close the file. The server will check this file on each bootup, so the swap file will be ready for use from now on.


Technology Python Programming Entertainment Python3 Linux Web-development Linux-mint Centos 7 Book
ziabs @ 12 Jul 2020