If you have a low RAM memory the swap partitions are the best solutions for you. swap is like  virtual memory  used when RAM is above 100% in use

In this tutorial we will talk about Swap partition, swap volume and swap file

My Settings :

Centos  7 with 2   disks /dev/sda  and  /dev/sdb


you  can  see my System is running with 2 GB of  Swap .   and  have 2  disks

1. Adding swap partition

Using our familiar command fdisk we will create swap partition.


2. Now  we can mkswap command to  the device/partition created earlier using fdisk/  and with  Optiona  -L can be used to set LABEL on the swap partition  ( to make  different  label   from the  original Swap  created on  system) .



3. Edit the /etc/fstab file to mount the new swap device on boot.

vi /etc/fstab


4. Please  enable swap devices listed in the /etc/fstab  file with swapmon -a   and verify the added swap using “swap -s”.

2. Adding swap volume

Please   follow  the  bellow command  carefully . we  will use the  same  situation    that   /dev/sdb  exist

First  Create   Physcial  volume from  /dev/sdb

Create volume group  Vol001 with  /dev/sdb

Create  logicalvolume  lv_swap

Now   the device  is fully  created

2. Run the mkswap command  on  the volume and optionally label it using the -L option.

4. To make the swap entry permanent  across reboots, make an entry into the /etc/fstab file.

5. Finally, please  enable  the swap using the swapon command which in turn reads the /etc/fstab file for swap entries.

3. Adding swap file

1. if you system still have  anough  free Disk space   you can use this option to crate Swap file  .


2. I’ve created the swap file in /opt  directory to make it more secure as normal user can’t enter that directory. To make is more secure apply appropriate permissions to the file.

3. Run mkswap command to convert the file to a swap file.

4. Modify the /etc/fstab file to add the new swap file.

5. Run the swapon command to enable all swap devices listed in the /etc/fstab file.

Removing swap

In case you want to remove added swap, you can do it with swapoff command. Make sure you remove the respective swap entry from the /etc/fstab file after disabling the swap.

# swapoff -a    ### this will remove all swap(s) mentioned in .etc.fstab
# swapoff /dev/sdb1    ### to remove individual swap

How to check swap usage

To check swap usage, you can use any one of the utilities below:
1. free

2. /proc/meminfo

3. top

4. vmstat

Please  enjoy  This Article  and don’t  hesitate to contact  us if u have any doubts


Please enter your comment!
Please enter your name here