Skip to content
  • About
  • Contact
  • Docs
  • Features
  • Home

Configurations

5
  • Setup RAID Level 6
  • Setup RAID Level 5
  • How To Add Swap on RHEL or Centos based system
  • Website Migration to Non-Panel OpenLiteSpeed Server
  • Linux Server Performance Tuning

CyberPanel

9
  • Mounting /tmp on a Separate File
  • Updating CyberPanel Main VirtualHost Configuration
  • Updating CyberPanel vHosts Configuration
  • How to Change CLI PHP on CyberPanel
  • How to Update PHP Version to 8.1 in CyberPanel (From 7.3, 7.4, or 8.0)
  • CyberPanel Server Cleanup: Logs, Dumps, Backups
  • CyberPanel Full LSPHP Installation PHP 7.4 – 8.3
  • CyberPanel / OpenLiteSpeed: Clean LSPHP Session Files
  • SSL Certificate Management in CyberPanel via SSH

Hestia Control Panel

6
  • Fixing Email Bounce Back Issue in Hestia (Exim4 Blacklist Rejection)
  • Managing Fail2Ban in HestiaCP Server
  • Installing and Configuring PHP Versions in HestiaCP
  • phpMyAdmin Not Found in HestiaCP
  • Setting Up a Reverse Proxy for any Port in HestiaCP
  • How to Install Let’s Encrypt SSL in Hestia CP (Hostname, Admin Panel, and Email Server)

WHM/cPanel

6
  • Setting Up a Reverse Proxy on cPanel/WHM for Port 8081
  • How to increase the size of the cPanel-generated /tmp filesystem
  • Installing Old PHP Versions on a cPanel/WHM Server
  • Fixing “550: Your Country is Not Allowed to Connect to This Server” Error in Exim (cPanel)
  • Enable WP-CLI in CageFS on CloudLinux Servers (WHM/cPanel)
  • Securing /tmp and /var/tmp with a Dedicated Loopback Filesystem

Nagios

5
  • Service Checks
  • Uptime Checks
  • Disk Space Checks
  • Load Checks
  • Email Related Checks

WordPress

3
  • WordPress Core Reinstallation Guide
  • Managing WordPress Users via WP-CLI
  • Website Duplication, Migration, or Domain Change

Operating System

1
  • Server Reboot (RHEL or Centos based system)

AWS

1
  • How to Resize EBS Volumes on AWS

Databases

2
  • MySQL
    • Optimizing MySQL Performance
    • Recovering MySQL in Plesk When InnoDB Crashes

CloudLinux

4
  • Moving cagefs-skeleton directory
  • Enable WP-CLI in CageFS on CloudLinux Servers (WHM/cPanel)
  • Enable bc in CageFS on CloudLinux Servers(WHM/cPanel)
  • How to Upgrade MySQL 5.x to MySQL 8.0 on CloudLinux (WHM server) with MySQL Governor

Cloudflare

1
  • Configuring Security Headers in Cloudflare

ISPmanager

1
  • Install Free SSL (Let’s Encrypt) for domain.tld on ISPmanager with NGINX

Plesk

1
  • Plesk Admin Login Blocked – IP Restriction Recovery Guide

VPN

4
  • Install OpenVPN Open Source in Linux CentOS Ubuntu Debian Servers
  • Protected: OpenVPN Installation & Fix Wiki (CloudLinux / CentOS 7 & 8)
  • Outline VPN Installation & Management Guide
  • Installing AdGuard Home on Debian/Ubuntu (Docker)
View Categories
  • Home
  • Docs
  • Configurations
  • How To Add Swap on RHEL or Centos based system

How To Add Swap on RHEL or Centos based system

introduction

Swap is a space on a disk drive that is used when the amount of physical RAM memory is full. When a Linux system runs out of RAM, it moves the inactive pages from the RAM to the swap space.

Swap space can take the form of either a dedicated swap partition, a swap file, or a combinaton of partitions and files. Generally, when running Ubuntu on a virtual machine, a swap partition is not present, and the only option is to create a swap file.

This article will guide you through the steps of adding a swap file to servers

Before You Begin

Swap is not a replacement for physical memory. Since swap space is a section of the drive, it has a slower access time than physical memory. If your system constantly runs out of memory, you should add more RAM.

In most cases, the size of the swap file depends on the physical RAM the Linux system has:

Systems with less than 2 GB RAM – 2 times the amount of RAM.
Systems with 2 to 8 GB RAM – the same size as the amount of RAM.
Systems with more than 8 GB RAM – at least 4 GB of Swap.

Only root or users with sudo privileges can activate the swap file.

Check If Swap Is Already Enabled

Before you begin, it’s always a good idea to check if you already have a swap on your system. You can do that by using the following command:

sudo swapon --show

If no output is shown, it means swap is not configured. You can also check using:

free -h

Example output:

                  total     used      free       shared         buff/cache      available
Mem:      4.0G       800M   2.9Gi      0.0                 300M                3.2G
Swap:     0                 0           0

Since `Swap` is `0B`, we need to add swap space.

Create a Swap File

In this guide, we will create a 4 GB swap file. If you need to add more or less swap, replace 4G with the required swap space size.

Before creating the file, ensure you have enough free disk space to complete this process successfully. You can get a detailed report on the system’s disk space usage with the df command :

df -h

Example output:

Filesystem                Size     Used      Avail    Use%        Mounted on
/dev/sda1                   50G      10G       38G     20%                 /

Since there is enough space(38G), proceed with creating a 4GB swap file:

sudo fallocate -l 4G /swapfile

for Ubuntu based system use:

sudo fallocate -l 4G /swap.img

If the fallocate utility is not present on your system, or you get an error message saying fallocate failed: Operation not supported, use the following command to create the swap file:

sudo dd if=/dev/zero of=/swapfile bs=1M count=4096

In this `dd` command:

sudo dd if=/dev/zero of=/swapfile bs=1M count=4096

bs=1M
Sets the block size to 1 megabyte (MB) (1M = 1024 * 1024 = 1,048,576 bytes).
Each read/write operation will process 1 MB at a time.
count=4096
Specifies the number of blocks to write.
It tells `dd` to copy 4,096 blocks of 1 MB  each.

Total File Size Calculation:  Total size  =  [bs]   x   [count]
=  [1M]  x    [4096]
                                         = 4096 MB = 4GB
So, this command creates a 4 GB swap file (`/swapfile`) filled with zeros.
This is a more efficient way than using `bs=1024 count=4194304` because larger block sizes reduce the number of operations, making the process faster.

Set File Permissions

Once the file is created, set the file permissions to 600 to prevent regular users to write and read the file:

sudo chmod 600 /swapfile
Convert the File into Swap Space

Format the file as swap space:

sudo mkswap /swapfile

Output:

Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label,  UUID=xxxxxxx

Enable the Swap File

Activate the swap file by running the following command:

sudo swapon /swapfile

Verify that the swap is active by using either the swapon or the free command, as shown below:

sudo swapon --show
free -h

Output:

                    total     used      free       shared         buff/cache      available
Mem:         4.0G       800M     2.9G      0.0                 300M                3.2G
Swap:        4.0G            0        4.0G

Make Swap Permanent

Make the change permanent by opening the /etc/fstab file:

sudo nano /etc/fstab

and pasting the following line:

/swapfile swap swap defaults 0 0
Adjust Swappiness Value

The swappiness parameter controls how aggressively the system uses swap (0-100). A lower value minimizes swap usage. Check the current value:

cat /proc/sys/vm/swappiness

Output:

30

default swappiness value on CentOS  is : 30
default swappiness value on Ubuntu  and Debian is : 60

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

sudo sysctl vm.swappiness=10

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

vm.swappiness=10

or

echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
Removing a Swap File (If Needed)

To deactivate and remove the swap file, perform the steps below:

1.First deactivate the swap space by typing:

sudo swapoff -v /swapfile

2.Next, remove the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.

3.Finally, delete the actual swapfile file:

sudo rm /swapfile
Conclusion

You’ve successfully added swap space to your Server. 🚀

This improves system performance, especially for low-RAM systems. If swap is used frequently, consider adding more RAM.

If you have a problem or feedback, leave a comment below.

Table of Contents
  • introduction
  • Before You Begin
  • Check If Swap Is Already Enabled
  • Create a Swap File
  • Set File Permissions
  • Convert the File into Swap Space
  • Enable the Swap File
  • Make Swap Permanent
  • Adjust Swappiness Value
  • Removing a Swap File (If Needed)
  • Conclusion

Share This Article :

  • Facebook
  • X
  • LinkedIn
  • Pinterest

Was it helpful ?

  • Happy
  • Normal
  • Sad
  • About
  • Contact
  • Docs
  • Features
  • Home

© 2026 Panel Web Hosting

  • About
  • Contact
  • Docs
  • Features
  • Home