How to add swap space on CentOS 8

Swap is disk space that is used when the amount of physical RAM is full. When a Linux system runs out of RAM, inactive pages are moved from RAM to paging space.

Paging space can take the form of a dedicated paging partition or paging file. Typically, when running CentOS in a virtual machine, there is no swap partition, so the only option is to create a swap file.

This article walks through the steps of adding a swap file on CentOS 8 systems.

Follow these steps as root user or user with sudo privileges to add swap space on CentOS 8 system.

  1. Let’s start by creating a file that will serve as swap space:
    sudo fallocate -l 1G /swapfile

    In this example we are creating a 1G swap file. If you need more swap, replace 1G with the size you want.

    If the fallocate utility is not available on your system, or you receive a fallocate failed: Operation not supported error, then use the dd command to create a swap file:

    sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
  2. Set the file permissions so that only the root user can read and write the swap file:
    sudo chmod 600 /swapfile
  3. Then set up the Linux swap space for the file:
    sudo mkswap /swapfile

    Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
    no label, UUID=0abdb8ba-57d6-4435-8fd8-5db9fc705045
  4. Activate the exchange by running the following command:
    sudo swapon /swapfile
  5. Make sure swap is active using the swapon or free command as shown below:
    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
  6. Make the change permanent by adding a swap entry to the / etc / fstab file:
    sudo nano /etc/fstab

    Insert the following line: / etc / fstab

    /swapfile swap swap defaults 0 0

Swappiness is a property of the Linux kernel that controls how often the system uses swap space. The paging value can range from 0 to 100. A low value will cause the kernel to try to avoid paging whenever possible, while a higher value will cause the kernel to use paging space more aggressively.

The default swap value in CentOS 8 is 30. You can check the current swap value by entering the following command:

cat /proc/sys/vm/swappiness

While a swappiness value of 30 is appropriate for desktops and development computers, production servers may need to be set to a lower value.

For example, to set the swappiness value to 10, enter:

sudo sysctl vm.swappiness=10

To make this parameter persistent across reboots, add the following line to your /etc/sysctl.conf file: /etc/sysctl.conf


The optimal amount of swap depends on your system’s workload and how memory is used. You must adjust this parameter in small increments to find the optimal value.

To deactivate and remove the paging file, follow these steps:

  1. Disable swap space by typing:
    sudo swapoff -v /swapfile
  2. Remove the swap entry / swapfile swap swap defaults 0 0 from the / etc / fstab file.
  3. Remove the actual swap file with rm:
    sudo rm /swapfile

We showed you how to create a swap file and activate and configure swap space on your CentOS 8 system.

If you run into an issue or have feedback, please leave a comment below.