How to Add Swap Space on CentOS 8

Swap is a space on a disk that is used when the amount of physical RAM memory is full. When a Linux system runs out of RAM, inactive pages are moved from the RAM to the swap space.
Swap space can take the form of either a dedicated swap partition or a swap file. Typically, when running CentOS on a virtual machine, a swap partition is not present, so the only option is to create a swap file.
This article covers the steps for adding a swap file on CentOS 8 systems.
Creating and Activating a Swap File
Perform the following steps as root or user with sudo privileges to add swap space on a CentOS 8 system.
Start by creating a file that will serve as swap space:
Terminalsudo fallocate -l 1G /swapfileIn this example, we are creating a swap file with a size of 1G. If you need more swap, replace
1Gwith the desired size.If the
fallocateutility is not available on your system or you get an error message sayingfallocate failed: Operation not supported, use theddcommand to create the swap file:Terminalsudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576Set the file permissions so that only the root user can read and write the swap file:
Terminalsudo chmod 600 /swapfileNext, set up a Linux swap area on the file:
Terminalsudo mkswap /swapfileoutputSetting up swapspace version 1, size = 1024 MiB (1073737728 bytes) no label, UUID=0abdb8ba-57d6-4435-8fd8-5db9fc705045Activate the swap by executing the following command:
Terminalsudo swapon /swapfileVerify that the swap is active by using either the
swaponor thefreecommand, as shown below:Terminalsudo swapon --showoutputNAME TYPE SIZE USED PRIO /swapfile file 1024M 507.4M -1Terminalsudo free -houtputtotal used free shared buff/cache available Mem: 488M 158M 83M 2.3M 246M 217M Swap: 1.0G 506M 517MMake the change permanent by adding a swap entry in the
/etc/fstabfile:Terminalsudo nano /etc/fstabPaste the following line:
/etc/fstabini/swapfile swap swap defaults 0 0
Adjusting the Swappiness Value
Swappiness is a Linux kernel property that defines how often the system will use the swap space. Swappiness can have a value between 0 and 100. A low value will make the kernel to try to avoid swapping whenever possible, while a higher value will make the kernel to use the swap space more aggressively.
The default swappiness value on CentOS 8 is 30. You can check the current swappiness value by typing the following command:
cat /proc/sys/vm/swappiness30While the swappiness value of 30 is OK for desktop and development machines, for production servers, you may need to set a lower value.
For example, to set the swappiness value to 10, type:
sudo sysctl vm.swappiness=10To make this parameter persistent across reboots append the following line to the /etc/sysctl.conf file:
vm.swappiness=10The optimal swappiness value depends on your system workload and how the memory is being used. You should adjust this parameter in small increments to find an optimal value. For recommended values and persistent configuration, see How to Change the Swappiness Value in Linux .
Removing a Swap File
To deactivate and remove the swap file, follow these steps:
Deactivate the swap space by typing:
Terminalsudo swapoff -v /swapfileRemove the swap entry
/swapfile swap swap defaults 0 0from the/etc/fstabfile.Delete the actual swapfile file with
rm:Terminalsudo rm /swapfile
Conclusion
We have shown you how to create a swap file and activate and configure swap space on your CentOS 8 system.
If you hit a problem or have feedback, leave a comment below.
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page