Thursday, December 3, 2020

How to add swap space on CentOS 7 Operating System

Must read

How to Setup a Firewall with UFW on Debian 10 Linux

Nowadays, a Firewall is an essential utility and property of any system for security; by default Debian Operating system having a firewall...

How to Install Eclipse IDE on Debian 10 Linux

Eclipse is the most famous and widely used Java integrated development environment (IDE). It supports many plugins to enhance the capabilities to...

How to Install Ruby on Debian 10 Linux

Nowadays, Ruby is the most popular language, especially for SaaS application development. It has a perfect and elegant syntax structure, and it...

How to Install MongoDB on Debian 9 Linux

MongoDB is an open-source and free NoSQL document based database system. Nowadays, it is prevalent as most application developers are using MongoDB,...
SATISH KUMAR
I am Satish Kumar, Founder of LinuxConcept. Linux and F.O.S.S enthusiast, love to work on open source platform and technologies.
- Advertisement -

The swap space is a unique space on the disk that is used by the system when Physical RAM is full. When a Linux machine runout the RAM. It use swap space to move inactive pages from RAM.

To add swap space on CentOS 7 Operating System you can use folowing two ways, one we can create a dedicated partition for swap or another one is created a swap file and use it as swap space.

- Advertisement -

Usually, when we are creating VPS with any provider like Digital Ocean, Linode, etc., we don’t have the swap partition into the server, in this case, we need to create swap file to use as swap space in server.

In this tutorial, we have explained all necessary steps to add swap space on CentOS 7 machine.

Prerequisites

Before beginning with this tutorial, make sure you have CentOS 7 machine and a user to login into the system with sudo privileges.

Before creating a swap file, it is good to know swap file or swap partition is available into your system or not.

To check the swap space, you can use below command:

- Advertisement -
$ sudo swapon --show

If the output of the above command is empty, it means your system doesn’t have swap space.

If your system has already configured swap space you will get output like below:

It is also possible to configure more than one swap space.

Swap File Creation

In this tutorial, we will add 1GB of swap space, if you want to create bigger swap space you can create by replacing 1G with any other size what you want.

- Advertisement -

To create a swap file, follow the below steps:

Step 1 – Create a file for swap

To create a file for swap space, use the fallocate command. Here we are creating 1GB swap file at /swapfile file. As shown below:

$ sudo fallocate -l 4G /swapfile

If in your system fallocate is not installed or you get a message like fallocate failed: Operation not supported, you can use below command to create swap file:

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

Step 2 – Change Permission

The swap file should only access by the root user; you can execute the below command to configure correct permission for /swapfile.

$ sudo chmod 600 /swapfile

Step 3 – Create swap space

To create swap space, use the mkswap command, as shown below:

$ sudo mkswap /swapfile

Step 4 – Activate swap space

To activate swap file use the swapon command, as shown below:

$ sudo swapon /swapfile

To activate swap space permanent by configuring file /etc/fstab:

$ sudo vim /etc/fstab

Add following line into the file /etc/fstab:

/swapfile 	swap 	swap 	defaults 	0 	0

Step 5 – Verify Swap space

To check the swap space is active or not, you can use swapon or free command, as shown below:

$ sudo swapon --show
$ sudo free –h

Configure Swappiness Value

Swappiness is the essential swap properties of Linux kernel, which is defined when the system will use swap space. Swappiness can have the value between 0 to 100, which is represent the percentage of RAM utilization, for example, if you set the value 20 for swappiness, it means the Linux kernel will use swap space when RAM is only 20 % available.

So, we can say the lower value of swappiness is to try to avoid the use of swap space.

The default value of swappiness is 30, which you can check by using the following command:

$ cat /proc/sys/vm/swappiness

The default value (30) of swappiness is ok for the desktop system, but when you run the production system, try to set the lower value for production servers.

You can set the swappiness value by using sysctl command, for example, if you want to set the value of swappiness is 20 you should execute below command:

$ sudo sysctl vm.swappiness=20

To change the value of swappiness permanently, you should change the parameter “vm.swappiness” into the /etc/sysctl.conf file and reboot the system.

vm.swappiness=10

Remove Swap File

To deactivate and remove the active swap space, follow the below steps:

Step 1 – Deactivate Swap Space

To deactivate swap space, you can use the swapoff command, as shown below:

$ sudo swapoff –v /swapfile

Step 2 – Remove from /etc/fstab

Edit the /etc/fstab file and remove the line /swapfile swap swap defaults 0 0 from the file and save

Step 3 – Delete swapfile file

You can delete the /swapfile file by using the rm command, as shown below:

$ sudo rm /swapfile

Conclusion

Now you have learned how to add a swap space on CentOS 7 machine if it not configured or you want to create another swap space.

- Advertisement -
- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Latest article

How to Setup a Firewall with UFW on Debian 10 Linux

Nowadays, a Firewall is an essential utility and property of any system for security; by default Debian Operating system having a firewall...

How to Install Eclipse IDE on Debian 10 Linux

Eclipse is the most famous and widely used Java integrated development environment (IDE). It supports many plugins to enhance the capabilities to...

How to Install Ruby on Debian 10 Linux

Nowadays, Ruby is the most popular language, especially for SaaS application development. It has a perfect and elegant syntax structure, and it...

How to Install MongoDB on Debian 9 Linux

MongoDB is an open-source and free NoSQL document based database system. Nowadays, it is prevalent as most application developers are using MongoDB,...

How to Install Python 3 on Ubuntu 20.04 Linux

Python is one of the most popular and easy to code programming language nowadays, because of its simplicity and easy to code....