YouTube Icon

Code Playground.

How to Add Swap Space on CentOS 7


How to Add Swap Space on CentOS 7

Trade is a space on a plate that is utilized when the measure of actual RAM memory is full. At the point when a Linux framework runs out of RAM, inert pages are moved from the RAM to the trade space. 

Trade space can appear as either a devoted trade segment or a trade record. By and large when running CentOS on a virtual machine a trade segment is absent so the main alternative is to make a trade record. 

This instructional exercise discloses how to add a trade record on CentOS 7 frameworks. 

Also Read:-How to Install Apache Cassandra on CentOS 8

Before You Begin

Prior to continuing with this instructional exercise, check if your CentOS establishment as of now has trade empowered by composing: 

sudo swapon --show

In the event that the yield is vacant, it implies that your framework doesn't have trade space empowered. 

Something else on the off chance that you get something like beneath, you as of now have trade empowered on your machine. 

/dev/dm-1 partition 1.5G   0B   -1

Albeit conceivable, rarely to have different trade spaces on a solitary machine. 

Creating a Swap File

The client you are signed in as must have sudo advantages to have the option to enact trade. In this guide, we will add 1G of trade, on the off chance that you need to add more trade, supplant 1G with the size of the trade space you need. 

Also Read:-How to make a POST request with cURL

Follow the means beneath to add trade space on a CentOS 7 framework. 

To begin with, make a document which will be utilized as trade space: 

sudo fallocate -l 1G /swapfile

On the off chance that the fallocate utility isn't accessible on your framework or you get a blunder message saying fallocate fizzled: Operation not upheld, utilize the accompanying order to make the trade document: 

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576

Guarantee that solitary the root client can peruse and compose the trade record by setting the right consents : 

sudo chmod 600 /swapfile
Also Read:-How to Install Gradle on CentOS 8

Next, set up a Linux trade region on the record: 

sudo mkswap /swapfile

Run the accompanying order to enact the trade: 

sudo swapon /swapfile

Roll out the improvement perpetual by opening the/and so forth/fstab document: 

sudo nano /etc/fstab

what's more, sticking the accompanying line: 


/swapfile swap swap defaults 0 0
Also Read:-How to Setup FTP Server with VSFTPD on CentOS 8

Confirm that the trade is dynamic by utilizing either the swapon or the free order as demonstrated as follows: 

sudo swapon --show
/swapfile file 1024M 507.4M   -1
sudo free -h
              total        used        free      shared  buff/cache   available
Mem:           488M        158M         83M        2.3M        246M        217M
Swap:          1.0G        506M        517M

Adjusting the Swappiness Value

Swappiness is a Linux part property that characterizes how frequently the framework will utilize the trade space. Swappiness can have an incentive somewhere in the range of 0 and 100. A low worth will cause the bit to attempt to try not to trade at whatever point conceivable while a higher worth will make the part to utilize the trade space all the more forcefully. 

Also Read:-How to Install and Configure Redmine on CentOS 8

The default swappiness esteem on CentOS 7 is 30. You can check the current swappiness esteem by composing the accompanying order: 

cat /proc/sys/vm/swappiness

While the swappiness estimation of 30 is OK for work area and improvement machines, for creation workers you may have to set a lower esteem. 

For instance, to set the swappiness incentive to 10, type: 

sudo sysctl vm.swappiness=10
Also Read:-How to Install Elasticsearch on CentOS 8

To make this boundary relentless across reboots attach the accompanying line to the/and so forth/sysctl.conf document: 



The ideal swappiness esteem relies upon your framework outstanding task at hand and how the memory is being utilized. You ought to change this boundary in little additions to locate an ideal worth. 

Removing a Swap File

To deactivate and eliminate the trade record, follow these means: 

Start by deactivating the trade space by composing: 

sudo swapoff -v /swapfile

Next, eliminate the trade record section/swapfile trade defaults 0 from the/and so forth/fstab document. 

At long last, erase the real swapfile record with rm : 

sudo rm /swapfile

Also Read:-How to Install Yarn on CentOS 8


You have figured out how to make a trade record and enact and design trade space on your CentOS 7 framework. 

On the off chance that you hit an issue or have criticism, leave a remark beneath.