• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Add Swap Space on CentOS 8

Written by Admin, Updated On March 2, 2020
centos, ram, swap
Add Swap Space on CentOS 8

When the physical RAM memory is full and Linux system runs out of RAM then it uses some area of hard disk space to store data that area is called Swap space. When the operating system no longer can put data on RAM then inactive pages are moved from the RAM to the swap space. In this tutorial, we will learn how to add swap space on CentOS 8 system.

Prerequisites#

Before you start, you must have login with root or user account with sudo privileges.

After that, you should check either swap space is already enabled or not on you system by below command:

sudo swapon --show

It will show as below:

NAME      TYPE       SIZE USED PRIO
/dev/sda7 partition  2.3G   0B   -2

If it will not display any output in terminal means swap space is disabled on your system. Following are the steps for adding a swap file on CentOS 8 systems.

Step 1 : Create a Swap File#

In this example, we will create swap file with size of 512 MB to the system. You can add more space just by replacing 512 MB with another number. Run the following command:

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

Step 2 : Set Permission#

Next, set the file permissions to allow only the root user to read and write the swap file:

sudo chmod 600 /swapfile

Step 3 : Set up swap space#

Run below command to set up swap space on your system using mkswap:

sudo mkswap /swapfile

Step 4 : Activate the swap#

Activate the created swap file as swap space using the following command:

sudo swapon /swapfile

Step 5 : Verify Swap Space#

To confirm and verify swap space run below command:

sudo swapon --show
NAME      TYPE  SIZE   USED    PRIO
/swapfile file  512M   10.9M   -1

You can also verify using free command, as shown below:

sudo free -h
           total      used        free       shared     buff/cache   available
 Mem:           2048M      657M        283M       102.3M     246M         1010M
 Swap:          512G       100M        256M

Make the swap file permanent#

When your system will reboot the server will not remember the above changes permanently. To make the above changes permanent add a swap entry in /etc/fstab file:

sudo nano /etc/fstab

Paste the following line:

/swapfile swap swap defaults 0 0

Change Swappiness Value#

Swappiness is a Linux kernel property that used to configure how often the swap space should be used. Value of swappiness can have between 0 and 100. System will avoid using swap space if value is set to lower while bigger value use swap space much often. The default swappiness value on the CentOS system is 30. To check swapiness value run following command:

cat /proc/sys/vm/swappiness

The swappiness value of 30 is OK for desktop and development machines, You should change for production servers, you may need to set a lower value.

For example, to set the swappiness value to 15, type:

sudo systemctl vm.swappiness=15

The above swappiness value will not persist after a reboot. So to make the changes permanent edit /etc/systemctl.conf file:

sudo nano /etc/systemctl.conf

Then add the following line to the file:

vm.swappiness=15

Remove a Swap File#

Perform the following steps to remove swap file.

Run below command to deactivate the swap space:

sudo swapoff -v /swapfile

Remove the swap entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.

Now delete the physical swap file:

sudo rm /swapfile

Conclusion#

You successfully learned how to create a swap file and activate and configure swap space on your CentOS 8 system.

If you have any queries about swap file, leave comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install PHP on Debian 10 Linux
Next Article   How to Install MariaDB on CentOS 8

Related Posts

  • How to Install Php 8 on CentOS 8

    How to Install PHP 8 on CentOS 8

    January 27, 2021
  • How to Install Python 3.9 on CentOS 8

    How to Install Python 3.9 on CentOS 8

    December 31, 2020
  • How to Install GIMP 2.10 on CentOS 8

    How to Install GIMP 2.10 on CentOS 8

    December 30, 2020

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install SSH Keys on Ubuntu 22.04
    How to Set up SSH Keys on Ubuntu 22.04 January 7, 2023
  • How to Install Mongodb on Debian 11
    How to Install MongoDB on Debian 11 Linux January 11, 2023
  • How to Install Puppet Agent on Ubuntu 22.04
    How to Install Puppet Agent on Ubuntu 22.04 January 22, 2023
  • How to Install Python 3.11 on Debian 11
    How to Install Python on Debian 11 January 25, 2023
  • How to Change-Hostname Ubuntu 22.04
    How to Change Hostname on Ubuntu 22.04 January 19, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook